0

I'm just trying to remove (replace with "") \r and \n from my JSON. Here is the method I'm currently testing which doesn't work.

    public static void testing(){
        String string = "\r\r\r\n\n\n";
        string.replace("\r", "");
        string.replace("\n", "");
    }

enter image description here

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
  • 3
    `String.replace` returns the modified string. You need `string = string.replace(...)` – khelwood Oct 08 '15 at 07:15
  • Possible duplicate of [Hints for java.lang.String.replace problem?](http://stackoverflow.com/questions/1166905/hints-for-java-lang-string-replace-problem) – Nitek Oct 08 '15 at 07:16
  • The awkward moment is that it works with normal strings but not working with jsonString. The testing method is fixed by you suggested, @khelwood. – Zin Win Htet Oct 08 '15 at 07:27
  • One should never manipulate JSON strings directly. Use a proper JSON library instead. – Eric Aya Aug 30 '21 at 11:54

9 Answers9

3

Try this regex (\\r\\n|\\n|\\r) and String#replaceAll, like:

string = string.replaceAll("(\\r\\n|\\n|\\r)", "");
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • This is juz my mistake. I'll post another question. My testing method works after assigning back but actual method I'm working doesn't work. – Zin Win Htet Oct 08 '15 at 07:35
2

After replacing you need to assign back to the original string. Because the string is immutable you cannot change the value of a string. You need to use

String string = "\r\r\r\n\n\n";
string  = string.replace("\r", "");
string  = string.replace("\n", "");

Or you can use any libraries like Apache StringUtils.If you are using these utils , no needs to assign back the value to String

dileep H
  • 355
  • 1
  • 3
  • 9
  • This is juz my mistake. I'll post another question. My testing method works after assigning back but actual method I'm working doesn't work. – Zin Win Htet Oct 08 '15 at 07:35
1

try this:

public static void testing(){
    String string = "\r\r\r\n\n\n";
    string  = string.replace("\r", "");
    string  = string.replace("\n", "");
}

because replace return another string(new String) because String is immutable so unable to modified directly

Pankaj Saboo
  • 1,125
  • 1
  • 8
  • 27
1

String.replace will return a string. It doesn't change its value.

public static void testing(){
    String str = "\r\r\r\n\n\n";
    str = str.replace("\r", "");
    str = str.replace("\n", "");
}
j4rey
  • 2,582
  • 20
  • 34
  • This is juz my mistake. I'll post another question. My testing method works after assigning back but actual method I'm working doesn't work. – Zin Win Htet Oct 08 '15 at 07:35
1
    String string = "\r\r\r\n\n\n";
    String newStr = string.replace("\r", "");
    newStr = newStr.replace("\n", "");
    System.out.println(newStr);

String will return new String Object.

Stan
  • 11
  • 2
0

try this:

    String string = "\rte\r\rs\nti\nn\ng";
    String newString = string.replaceAll("[^\\w]", "");
Chathura
  • 1
  • 1
0
String string = "\r\r\r\n\n\n";
string = string.replaceAll("\r", "");
string = string.replaceAll("\n", "");
Stan
  • 11
  • 2
0

Try this, string = string.replaceAll("[\r\n]", "");

0

Here is what I found:

data_json = data_json.replaceAll("\\\\r\\\\n", "");

Copied from OP's comment.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253