Removing Whitespace from Strings in Java

How to remove a white space at the end of a string in java?

If you just want to trim whitespace at the end, use String#replaceAll() with an appropriate regex:

String input = " XXXX ";
String output = input.replaceAll("\\s+$", "");
System.out.println("***" + input + "***"); // *** XXXX ***
System.out.println("***" + output + "***"); // *** XXXX***

If you really want to replace just one whitespace character at the end, then replace on \s$ instead of \s+$.

Remove Whitespaces ONLY at the end of a String (java)

If your goal is to cut only the trailing white space characters and to save leading white space characters of your String, you can use String class' replaceFirst() method:

String yourString = "   my text. ";
String cutTrailingWhiteSpaceCharacters = yourString.replaceFirst("\\s++$", "");

\\s++ - one or more white space characters (use of possesive quantifiers (take a look at Pattern class in the Java API documentation, you have listed all of the special characters used in regułar expressions there)),

$ - end of string

You might also want to take a look at part of my answer before the edit:

If you don't care about the leading white space characters of your input String:
String class provides a trim() method. It might be quick solution in your case as it cuts leading and trailing whitespaces of the given String.

Removing whitespace from strings

try this

  /* Type your code here. */
String name;
String first;
String last;

while (true)
{
System.out.println("Enter input string:");
name = scnr.nextLine();

// get q case
if (name.equals("q"))
{
//You can avoid to use quit flag, you can break the loop
break;
}
else
{

//Separate the input with split by the comma character
string [] arr = name.split(",");

// if no comma, give error message in other words if you array is empty
if(arr.length==0){
System.out.println("Error: No comma in string.");
System.out.println();
}

//check if you have more than 2 words
if(arr.length>1){
//the trim function will remove all white spaces at the start and end of the string
first = arr[0].trim();
last = arr[1].trim();
System.out.println("First word: " + first);
System.out.println("Second word: " + last);
System.out.println();
}else{
first =arr[0].trim();
System.out.println("First word: " + first);
System.out.println();
}

}
}

How can Java remove leading whitespace?

Possible solutions

  • Use String::replaceFirst to keep only the part after a prefix ([WARNING]:) followed by whitespaces and the main part:

    public static String message(String logLine) {
    return logLine.replaceFirst("^\\S*\\s+(\\S+(\\s+\\S+)*)\\s+$", "$1");
    }
  • As the prefix ends with ':', a solution offered in the comment using String::substring + String::trim works too:

    public static String message(String logLine) {
    return logLine.substring(logLine.indexOf(":") + 1).trim();
    }

Unable to remove white space in String

Just add one more slash like below:

adressString.replaceAll("\\r","");

As "\" is a special character.



Related Topics



Leave a reply



Submit