Pankaj-Str / JAVA-SE-Tutorial-codeswithpankaj

Pankaj-Str's GitHub, 'JAVA-SE-Tutorial-codeswithpankaj,' is a concise compendium of Java SE tutorials. Ideal for developers and learners, it offers clear and insightful code snippets, providing an efficient pathway to enhance Java programming skills. A valuable resource for mastering essential concepts
https://codeswithpankaj.com
32 stars 15 forks source link

How do you remove spaces from a string in Java? #4

Closed Pankaj-Str closed 11 months ago

Advaittt24 commented 11 months ago
// Importing required libraries
import java.io.*;
import java.util.*;

// Class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        String str = " Geeks     for Geeks   ";
        String op = "";

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            // Checking whether is white space or not
            if (!Character.isWhitespace(ch)) {
                op += ch;
            }
        }
        System.out.println(op);
    }
}