Want to do left and right cropping in Java String

I have a Java string that contains spaces on the right and left. I want to remove the empty space on both sides.

The code I tried ...

public class Test { public static void main(String args[]) { String abc = "  Amebiasis "; System.out.println(abc +" length "+abc.length()); System.out.println(rtrim(abc)+" length "+rtrim(abc).length()); System.out.println(ltrim(abc)+" length "+ltrim(abc).length()); String ltrim = abc.replaceAll("^\\s+",""); String rtrim = abc.replaceAll("\\s+$",""); System.out.println("ltrim"+ltrim); System.out.println("rtrim"+rtrim); } public static String rtrim(String s) { int i = s.length()-1; while (i >= 0 && Character.isWhitespace(s.charAt(i))) { i--; } return s.substring(0,i+1); } public static String ltrim(String s) { int i = 0; while (i < s.length() && Character.isWhitespace(s.charAt(i))) { System.out.println("s.charAt(i) "+s.charAt(i)); i++; } return s.substring(i); } } 

The result I got ...

  Amebiasis length 13  Amebiasis length 11  Amebiasis length 13 ltrim  Amebiasis rtrim  Amebiasis 

Somehow it does not remove the empty space. What is wrong with my code, please help me with this.

+6
source share
7 answers

Available by default trim ()

 String abc = " Amebiasis "; System.out.println(""+abc.trim()); 
+12
source

I tried both of your functions, ltrim and rtrim , and they work fine. If I understand you correctly, you expect these functions to change the contents of the string in place. This is not possible because strings in Java are immutable . If you want to trim spaces from your string and then start working with the cropped version of the string, you need to assign a value that rtrim or ltrim returns to your abc variable:

 String abc = " Amebiasis "; abc = rtrim(abc); // abc now equals to " Amebiasis" abc = ltrim(abc); // abc now equals to "Amebiasis" 
+10
source

Use regex to remove all spaces on both sides:

 return str.replaceAll("\\s*$", "").replaceAll("^\\s*", ""); 
+4
source

Using the Characters class, you can easily remove / remove any string for any given character (-class):

 Characters.valueOf('x').trim( ... ) Characters.valueOf('x').leftTrim( ... ) Characters.valueOf('x').rightTrim( ... ) 

If you want to trim spaces, there is a predefined character class:

 Characters.WHITESPACE.trim( ... ) Characters.WHITESPACE.leftTrim( ... ) Characters.WHITESPACE.rightTrim( ... ) 

The class also performs other types of symbol operations, such as condense , replace or split .

+4
source

Your code works, the output in your question is incorrect, the actual output of your code:

  Amebiasis length 13 Amebiasis length 11 s.charAt(i) s.charAt(i) s.charAt(i) s.charAt(i) Amebiasis length 11 ltrimAmebiasis rtrim Amebiasis 

If you only want to crop both sides, just do

 String abc = abc.trim(); 
+1
source

You just need to assign the trimmed string object to the new line, because the lines are immutable, and trim () cannot edit this line if there is the same address. In this way,

 String abc = " Amebiasis "; String abcTrimmed = abc.trim(); 

must work.

0
source

You can simplify using \\S*

Example:

 String name = "Hello :: World"; abc.replaceAll("\\s*::\\s*", ""); 

the conclusion will be

 HelloWorld 
0
source

Source: https://habr.com/ru/post/955074/


All Articles