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 usingString::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
Java Reflection: How to Get the Name of a Variable
What's the Reason I Can't Create Generic Array Types in Java
How to Update a Jfreechart'S Appearance After It's Been Made Visible
Initialization of an Arraylist in One Line
How to Use Different Certificates on Specific Connections
How to Handle Cors Using Jax-Rs With Jersey
How Can an App Use Files Inside the Jar For Read and Write
How to Add an Image to a Jpanel
What's Wrong With Using == to Compare Floats in Java
Selenium - Basic Authentication Via Url
Downloading a File from Spring Controllers
"Missing Return Statement" Within If/For/While
Are Arrays Passed by Value or Passed by Reference in Java