Nikhil Gopal
Nikhil Gopal

Reputation: 107

How do I remove spaces in a String (sentence)

I need to remove all spaces from it.

Eg: This is my code O/P : Thisismycode

This is my code so far.

import java.util.Scanner;
public class nospace{

public static void main(String args[]){

    Scanner s=new Scanner(System.in);

    System.out.println("Enter a String");
    String N=s.nextLine();
    N=N.trim();
    N=N+" ";
    int l=N.length();
    int a=0;
    for(int i=0;i<=l-1;i++){
        if(N.charAt(i)==32){
    System.out.println(N.substring(a,i+1));
    }
    a=i;

    }

}

}

All it does it print the last letters of each word. Any help is appreciated.

Upvotes: 0

Views: 10453

Answers (4)

Deb
Deb

Reputation: 2972

If you want to do something with Strings in Java, Commons Lang StringUtils is a great place to look.

String newString = StringUtils.deleteWhitespace(oldStr);

Upvotes: 1

fiddle
fiddle

Reputation: 1164

Try this:

public class RemoveExtraspaces {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str="   Hello and welcome !    let's start    coding    ";
        char prev;
        char current;
        String punc="?,.!";
        String result = "";
        for(int i=1;i<str.length();i++){
            current=str.charAt(i);
            prev=str.charAt(i-1);
            if(current==' ' && prev==' ' && punc.indexOf(current)<0){
                prev=current;
            }
            else if(punc.indexOf(current)>0){
                if(prev==' ')
                    result=result.substring(0,result.length()-1);
                    result=result+current;
            }
            else {
                result=result+current;
            }
        }
        System.out.println(result);

    }


}

Upvotes: 0

aashnisshah
aashnisshah

Reputation: 476

I would recommend using Java's string replaceAll function. You give it the regex that you're looking to replace, and what you want to replace it with:

string.replaceAll("\\s+", "");

So your code should look like this:

import java.util.Scanner;
public class nospace{

public static void main(String args[]){

    Scanner s=new Scanner(System.in);

    System.out.println("Enter a String");
    String input=s.nextLine();
    input = input.replaceAll("\\s+", "");
    System.out.println(input);

}

}

I changed your input variable from N to input - in Java you shouldn't use capital letters to start the name of a variable. Additionally, I removed some of the other lines you had, such as adding a space to the end of your input string, and creating the l and a variables.

Upvotes: 2

Endrik
Endrik

Reputation: 2258

you can try to us this piece of code

N.replaceAll("\\s+","")

This states that you replace all white spaces inside a string object with an empty string (""). The part "\\s+" is called a regex and in combination with replaceAll removes all whitespaces and non visible characters such as tab. Also notice that st.replaceAll("\\s","") produces the same result. The second regex is faster than the first one. But we increase the number consecutive spaces, first one performs better than the second one

Upvotes: 5

Related Questions