4

i already found similar topics but they were not really helpful.

I basically need to know how I can remove whitespaces ONLY at the end of a string.

For example: String a = "Good Morning it is a nice day ";

Should be: String a = "Good Morning it is a nice day";

You see there are like three whitespaces at the end or something. I got replaceAll() as a suggestion but as I said I only want to remove the remaining whitespaces at the end. How do I do that ? I already tried different things like going through the String like this:

for(int i = a.length(); i > a.length() - 3; i++){
     if(a.charAt(i) == ' '){
           <insert Solution here>
     }
}

With the code above I wanted to check the last three Chars of the string because it is sufficient. It is sufficient for the stuff I want to do. There wont be a case with 99+ whitespaces at the end (probably).

But as you see I still dont know which method to use or if this is actually the right way.

Any suggestions please ?

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
J.Doe
  • 125
  • 1
  • 1
  • 10

4 Answers4

6

You can either use the regular expression:

a = a.replaceAll("\\s+$", "");

to remove trailing whitespaces and store the result back into a

or using a for loop:

String a = "Good Morning it is a nice day    ";
StringBuilder temp = new StringBuilder(a);
for( int i = temp.length() - 1 ; i >= 0; i--){
     if(temp.charAt(i) == ' '){
          temp.deleteCharAt(i);
     }else{
          break;
     }
}

a = temp.toString();
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    You can alternatively just loop, decrementing a "length" variable, and then use `temp.setLength`. Or even use `temp.substring(0, length);`. Doubt there'd be much difference, though. – Andy Turner Jan 01 '18 at 20:32
6

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.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • @zlakad Thanks for the upvote. Regex is really helpful in a huge variety of cases that programmers deal with. Take some time to use it fluently, it's worth that :-) – Przemysław Moskal Jan 01 '18 at 22:11
  • Thank you, this worked as smoothly as it could lol. Removed all the trailing whitespaces while keeping the ones in between characters or at the beginning. Just as I wanted to. For anyone in the future reading this with similar problems, check this method out ;) – J.Doe Jan 02 '18 at 13:01
  • I'm glad that I could help you! – Przemysław Moskal Jan 02 '18 at 13:18
4
a = ("X" + a).trim().substring(1);
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Jonathan Rosenne
  • 2,159
  • 17
  • 27
  • So, if my String is " something ", you are adding "X" in front. My string becomes "X something "? substring becomes " something ", and trim removes LEADING spaces. It isn't a true answer – zlakad Jan 01 '18 at 20:28
  • 1
    1. Try it, it does work. 2. substring is executed after the trim. It just removes the superfluous "X". – Jonathan Rosenne Jan 01 '18 at 20:31
  • 1
    it does indeed accomplish what OP wants and +1 for that, although, in my opinion, it's not obvious to the eye what the code is doing unless you look at it the second time or so. – Ousmane D. Jan 01 '18 at 20:57
  • 2
    @zlakad it should work as Jonathan is using the string `X` as a guard to the leading spaces so that `trim` doesn't get rid of it and then removes the `X` with `substring` prior to assigning it back to `a`. – Ousmane D. Jan 01 '18 at 20:59
  • 1
    It works... Sorry – zlakad Jan 01 '18 at 20:59
0
int counter = 0;
String newWord = "";
for(int i = 0; i < word.length(); i++){
    if (word.charAt(i) == " "){
        counter++;
    }
    else {
        counter = 0;
    }

    if (counter > 1){
        newWord = word.substring(0, i);
        break;
    }
    else {
        newWord = word;
    }
}

The solution would look something like this.

Ryan
  • 133
  • 2
  • 13