-2

I am testing out the replaceAll() method of the String class and I am having problems with it.

I do not understand why my code does not replace whitespaces with an empty string.

Here's my code:

public static void main(String[] args) {
    String str = " I like pie!@!@!      It's one of my favorite things !1!!!1111";
    str = str.toLowerCase();
    str = str.replaceAll("\\p{Punct}", "");
    str = str.replaceAll("[^a-zA-Z]", "");
    str = str.replaceAll("\\s+", " ");
    System.out.print(str);

}

Output:

ilikepieitsoneofmyfavoritethings
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I may have been a little unclear, I want to replace whitespaces with a " ". I also actually screwed up one of my pictures. Even if I added the little space between the quotes, there still isn't a space. – 1ndianSp1ces Feb 24 '22 at 20:13
  • Welcome to Stack Overflow. This site is not a discussion forum or programming tutorial site. Please take the [tour], visit the [help] and read [Ask] to learn how to use this site effectively. Please do not post images of code, copy/paste the code and messages into your question. Also read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Feb 24 '22 at 20:15
  • Please edit your question to also include the *expected* output. – Bohemian Feb 24 '22 at 20:34

1 Answers1

3

The problem is there are no whitespaces in your String after this:

str = str.replaceAll("[^a-zA-Z]", "");

which replaces all characters that are not letters, which includes whitespaces, with a blank (effectively deleting it).

Add whitespace to that character class so they don't get nuked:

str = str.replaceAll("[^a-zA-Z\\s]", "");

And this line may be deleted:

str = str.replaceAll("\\p{Punct}", "");

because it's redundant.

Final code:

String str = " I like pie!@!@!      It's one of my favorite things !1!!!1111";
str = str.toLowerCase();
str = str.replaceAll("[^a-zA-Z\\s]", "");
str = str.replaceAll("\\s+", " ");
System.out.print(str);

Output:

 i like pie its one of my favorite things 

You may want to add str = str.trim(); to remove the leading space.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Or, do the whitespace compaction before this one. – Jim Garrison Feb 24 '22 at 20:25
  • @Jim No `\p{Punct}` does not include whitespace. – Bohemian Feb 24 '22 at 20:26
  • Wow, I never knew that. There is one problem here. My output is now: i like pie its one of my favorite things. There is still a huge gap between the word pie and its. The gap doesn't exactly show when I type it in the comments. My main intention here is to replace all the whitespaces with just a single space. – 1ndianSp1ces Feb 24 '22 at 20:33
  • @1ndianSp1ces that doesn't make sense: If `str = str.replaceAll("\\s+", " ");` is the last operation there will only be single spaces between words. btw, if you wrap text in backticks in comments it will render using a fixed width font and therefore show multiple whitespaces. – Bohemian Feb 24 '22 at 20:37
  • @Bohemian I'm really not lying about this. Or should I repost my code? – 1ndianSp1ces Feb 24 '22 at 20:40
  • @1ndianSp1ces I've run the code and it works. I edited my answer to show the final code and included its output. – Bohemian Feb 24 '22 at 20:45
  • @1ndianSp1ces all I can suggest is that you copy-paste my code into your IDE and run it. – Bohemian Feb 24 '22 at 20:49
  • Strange, the whitespace is still outputted. Maybe it's something wrong with my text editor? For now, I think it's best to close the discussion. Also, I apologize for all I trouble I caused. – 1ndianSp1ces Feb 24 '22 at 20:50
  • @1ndianSp1ces I think you might not be recompiling your code, because the code block in my answer does not produce the output you claim. If you are not using an IDE, use one. I recommend Intellij. Copy paste *all* of the code block after "Final code" in my answer and run it and you will see there is not more than one space between words. – Bohemian Feb 24 '22 at 20:55
  • @Bohemian I copied the code and somehow the whitespace is gone and replaced with the space I wanted to put in there. I saw no visible change in the code itself, so maybe the problem was how I wrote the string? Edit: I noticed you added a plus sign and my code didn't have that. So that was the problem. – 1ndianSp1ces Feb 24 '22 at 21:03
  • @1ndianSp1ces the plus sign is in your code in your question – Bohemian Feb 24 '22 at 22:09