Majority of the times, we just use the trim method for removing spaces. We never stop and think is there may be a better way to suit our need? Sure, trim() works well for most of the cases, but there are many different methods in java. Each having its own advantages and disadvantages. How do we decide which method suits us best?
Well, in this blog we shall cover the different methods in detail.
Different ways to remove spaces from string in java
1. trim() : Removing leading and trailing spaces from the string
2. strip() : Removes spaces at the beginning and end of the string. Strip method is Unicode charset aware
3. trim vs strip : Differences between trim and strip method
4. stripLeading() : Removes white spaces only from the beginning of the string
5. stripTrailing() : Removes white spaces only from ending of the string
6. replace() : Replaces all target characters with new character
7. replaceAll() : Replaces all matched characters with new character. This method takes regular expression as input to identify target substring that needs to be replaced
8. replaceAll vs replace : Differences between replace and replaceAll method
9. replaceFirst() : Replaces only first occurrence of target substring with new replacement string
Most important point to note is that in java a string object is immutable. It means we cannot modify a string, hence all the methods returns new string with all the transformations.
trim() method in java
trim() is the most commonly used method by java developers for removing leading and trailing spaces. For trim method space character means any character whose ASCII value is less than or equal to 32 (‘U+0020’).
Example of trim method to remove spaces:
public class StringTrimTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before trim: \"" + string +"\"");
System.out.println("After trim: \"" + string.trim() +"\"");
}
}
Output:
Before trim: " String with space "
After trim: "String with space"
strip() method Java 11
In the release of Java 11 new strip() method was added to remove leading and trailing spaces from String.
This method was added as there are various space characters according to Unicode standards having ASCII value more than 32(‘U+0020’). Ex: 8193(U+2001).
To identify these space characters, new method isWhitespace(int) was added from Java 1.5 in Character class. This method uses unicode to identify space characters. You can read more about unicode space characters here.
The strip method uses this Character.isWhitespace(int) method to cover wide range of white space characters and remove them.
Example of strip():
public class StringStripTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before strip: \"" + string+"\"");
System.out.println("After strip: \"" + string.strip()+"\"");
}
}
Output:
Before strip: " String with space "
After strip: "String with space"
Difference between trim and strip method in java
trim() | strip() |
From Java 1 | From Java 11 |
Uses codepoint(ASCII) value | Uses Unicode value |
Removes leading and trailing character(space) | Removes leading and trailing character(space) |
Removes characters having ASCII value less than or equal to ‘U+0020’ or ’32’ | Removes all space characters according to unicode |
Let’s look at the example where we will use white space character higher than 32 (‘U+0020’) unicode.
public class StringTrimVsStripTest {
public static void main(String[] args) {
String string = '\u2001'+"String with space"+ '\u2001';
System.out.println("Before: \"" + string+"\"");
System.out.println("After trim: \"" + string.trim()+"\"");
System.out.println("After strip: \"" + string.strip()+"\"");
}
}
Output:
Before: " String with space "
After trim: " String with space "
After strip: "String with space"
In the above example we can see that trim method is unable to remove space character added by ‘\u2001’ unicode character.
stripLeading() method Java 11
Added in Java 11, stripLeading() method removes all leading spaces from a String.
Similar to strip method stripLeading also uses Character.isWhitespace(int) for identifyng white spaces.
public class StringStripLeadingTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before: \"" + string+"\"");
System.out.println("After : \"" + string.stripLeading()+"\"");
}
}
Output:
Before: " String with space "
After : "String with space "
stripTrailing() method Java 11
Added in Java 11, stripTrailing() method removes all ending spaces from a String.
Similar to strip method stripTrailing also uses Character.isWhitespace(int) for identifying white spaces.
public class StringStripTrailingTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before: \"" + string+"\"");
System.out.println("After : \"" + string.stripTrailing()+"\"");
}
}
Output:
Before:" String with space "
After :" String with space"
replace(CharSequence target, CharSequence replacement):
Added from java 1.5, This method is used to replace each target substring with the specified replacement string.
This method replaces all matching target elements.
Note: One more method replace(char oldChar, char newChar) is present in java string class. The only differences is that this method takes single character as target and replacement. We cannot use this method to remove space, because we cannot have empty character as replacement.
Example to remove all spaces from string
public class StringReplaceTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before : \"" + string + "\"");
System.out.println("Replace: \"" + string.replace(" ", "") + "\"");
}
}
Output:
Before : " String with space "
Replace : "Stringwithspace"
replaceAll (String regex, String replacement)
Added in java 1.4, this is one of the most powerful method for string manipulation. We can use this method for many purposes.
Using replaceAll() method we can replace each matching regular expression substring with the given replacement string. For example for removing all spaces, removing leading spaces, removing trailing spaces and so on.
We just need to create correct regular expression with correct replacement parameter. Some regular expression examples as below:
\s+ : Find all space
^\s+ : Find all spaces at line beginning
\s+$ : Find all spaces at line ending
Example to replacing spaces in string,
NOTE: In java to add ‘/’ we have to use escape character so for “\s+” we have to use “\\s+”
public class StringReplaceAllTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before replaceAll : \"" + string+"\"");
System.out.println("Replace all space : \"" + string.replaceAll(" ", "") + "\"");
System.out.println("Replace all regex : \"" + string.replaceAll("\\s+", "") + "\"");
System.out.println("Replace Leading : \"" + string.replaceAll("^\\s+", "") + "\"");
System.out.println("Replace trailing : \"" + string.replaceAll("\\s+$", "") + "\"");
}
}
Output:
Before replaceAll : " String with space "
Replace all space : "Stringwithspace"
Replace all regex : "Stringwithspace"
Replace Leading : "String with space "
Replace trailing : " String with space"
As we can see that replaceAll() is pretty powerful method if we use it with proper regular expression.
Difference between replaceAll and replace method
replaceAll() | replace() |
From Java 1.4 | From Java 1.5 |
Accepts regular expression for target identification | Accepts string for target identification |
Used for fix or dynamic string replacement | Used for fix string replacement |
Removes characters having ASCII value less than or equal to ‘U+0020’ or ’32’ | Removes all space characters according to unicode |
replaceFirst(String regex, String replacement)
Added in java 1.4, replaceFirst method replaces only the first match of given regular expression with replacement string.
This method can be very useful if you just need to replace only one first occurence. For example if we just need to remove leading spaces we can use “\\s+” or “^\\s+”.
We can also use this method to remove trailing spaces by using “\\s+$” regular expression. As this expression will only match the last spaces in line. So last spaces are considered as the first match for this method.
Let’s take an example for removing leading and trailing spaces from string
public class StringReplaceFistTest {
public static void main(String[] args) {
String string = " String with space ";
System.out.println("Before : \"" + string+"\"");
System.out.println("Replace : \"" + string.replaceFirst("space", "update") + "\"");
System.out.println("Leading : \"" + string.replaceFirst("\\s+", "") + "\"");
System.out.println("Trailing : \"" + string.replaceFirst("\\s+$", "") + "\""); }
}
Output
Before : " String with space "
Replace : " String with update "
Leading : "String with space "
Trailing : " String with space"
0 comments:
Post a Comment