Removing every " " front of the string.

Discussion in 'Spigot Plugin Development' started by Settler, May 18, 2015.

  1. Hello. (Sorry for bad english)
    How to remove every " " front of the string.
    I need to remove this.
    Code (Text):
    String string = "Hello                   my name is bla bla."
  2. Replace
    Code (Java):
    "  "
    Code (Java):
    " "
    (So two spaces with one)
  3. NathanWolf


    Won't that remove the space entirely if there are an even number of spaces?
  4. No, it wouldn't, but the solution he posted also wouldn't work entirely. It'll end up replacing half of the spacing inside the string, so now you end up with half of the original amount.

    @OP There's no real way to do that with one line; unless you knew the amount of spaces beforehand, but then you wouldn't need to remove them. There are two ways I suggest you going about this. The easy one, use the one guys solution but loop it every time the original string isn't equal to the new string. The slightly harder but more efficient, iterate through the chars and ignore multiple spaces if a space character was the previous char, while placing them into some type of buffer to later build the string into.
  5. Give me a second. I have a code snip-it that can accomplish this somewhere.

    EDIT: found it
    Code (Text):
    String test = "yes   yes";
    test = test.trim().replaceAll(" +", " ");
    Code (Text):
    yes   yes
    yes yes
    #5 are0planes, May 18, 2015
    Last edited: May 18, 2015
    • Agree Agree x 2