3

I have a string with \r\n, \r, \n or \" characters in it. How can I replace them faster?

What I already have is:

String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replace("\\r\\n", "\n").replace("\\r", "").replace("\\n", "").replace("\\", ""));

But my code does not look beautiful enough. I found on the Internet something like:

replace("\\r\\n|\\r|\\n|\\", "")

I tried that, but it didn't work.

khelwood
  • 55,782
  • 14
  • 81
  • 108
sausagerus
  • 188
  • 2
  • 14

4 Answers4

1

You can wrap it in a method, put /r/n, /n and /r in a list. iterate the list and replace all such characters and return the modified string.

public String replaceMultipleSubstrings(String original, List<String> mylist){
    String tmp = original;
    for(String str: mylist){
        tmp = tmp.replace(str, "");
    }
    return tmp;
}

Test:

mylist.add("\\r");
mylist.add("\\r\\n");
mylist.add("\\n");
mylist.add("\\");  // add back slash

System.out.println("original:" + s);
String x = new Main().replaceMultipleSubstrings(s, mylist);
System.out.println("modified:" + x);

Output:

original:Kerner\r\n kyky\r hihi\n \"
modified:Kerner kyky hihi "
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
  • Thank you its works. Your method deleted all sign what i do not need. I recive 31000entries from DB and it is a little bit slower now, due for-loop what it goes 4 times for each entries. – sausagerus Feb 08 '17 at 21:33
0

I don't know if your current replacement logic be correct, but it says now that either \n, \r, or \r\n gets replaced with empty string, and backslash also gets replaced with empty string. If so, then you can try the following regex replace all:

String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\r|\\n|\\r\\n|\\\\", ""));

One problem I saw with your attempt is that you are calling replace(), not replaceAll(), so it would only do a single replacement and then stop.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

String.replaceAll() can be used, in your question you tried to use String.replace() which does not interpret regular expressions, only plain replacement strings...

You also need to escape the \\ again, i.e. \\\\ instead of \\

String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\\\r|\\\\n|\\\\\"", ""));

Output

Kerner kyky hihi

Note the differences between String.replaceAll() and String.replace()

String.replaceAll()

Replaces each substring of this string that matches the given regular expression with the given replacement.

String.replace()

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

Adam
  • 35,919
  • 9
  • 100
  • 137
  • Not my downvote, but `String#replace()` doesn't need to double escape the backslash, q.v. [here](http://stackoverflow.com/questions/1701839/string-replaceall-single-backslashes-with-double-backslashes). – Tim Biegeleisen Feb 08 '17 at 16:24
  • @TimBiegeleisen I get this without escaping... Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 12 \r\n|\r|\n|\ ^ – Adam Feb 08 '17 at 16:25
  • You don't get it, you're calling `replaceAll()`, the OP is calling `replace()`. – Tim Biegeleisen Feb 08 '17 at 16:26
  • @TimBiegeleisen I can see that, but he is calling replace() because he believes replace() interprets regex, but it does not... – Adam Feb 08 '17 at 16:27
  • Actually I think it does in some cases. I have countered the downvote on the grounds that your answer may be right, though with a wrong explanation. – Tim Biegeleisen Feb 08 '17 at 16:28
  • @Adam, I want to delete all \r\n carriage-return from my string. Your code deleted all "\" sign – sausagerus Feb 08 '17 at 17:14
  • @sausagerus Apologies, I misread your question... your original code contains .replace("\\", "") which does the same... it removes the \ characters...Answer updated – Adam Feb 08 '17 at 17:23
-1

Use a regular expression if you want to do all the replaces in one go. http://www.javamex.com/tutorials/regular_expressions/search_replace.shtml

odin
  • 392
  • 1
  • 3
  • 11