-3

I have situation where i need to remove "||" and insert a backslash from a string.

String s = "stack||overflow";

I need to replace and the resultant string should be "stack\overflow".

I tried using replaceAll function but didn't work.

Any help is appreciated.

Thanks, Sreekanth

Srikanth Sridhar
  • 2,317
  • 7
  • 30
  • 50
  • 2
    what about using the non `regex` version `String#replace` if you want to do a simple replacement... – SomeJavaGuy Aug 24 '16 at 13:18
  • 2
    And the point in using `replaceAll` is? Using `replace` instead would be much easier. – Tom Aug 24 '16 at 13:18
  • 2
    If you have code that doesn't work, and you have a question about it, then **post the code**. That shows that you have made some effort, and it also allows us to see the actual problem that you're having. – Erwin Bolwidt Aug 24 '16 at 13:25

1 Answers1

2

This is already described in Oracle tutorial, part Replacing Characters and Substrings into a String:

String replace(CharSequence target, CharSequence replacement) 

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

For example:

String s = "stack||overflow.";
System.out.println(s.replace("||","\\"));
Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
mayha
  • 603
  • 6
  • 16