-1

I have a string like

"age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' "

and i need to replace "||" by "OR". I tryied following code.

System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0'".replaceAll("||", "OR"));

but i got

"ORaORgOReOR OR=OR OR1OR8OR OR|OR|OR ORnORaORmOReOR OR=OR OR'ORMORiORsORtORiORcOR'OR OR|OR|OR ORcORiORvORiORlORsORtORaORtORuORsOR OR=OR OR'ORmORaORrORrORiOReORdOR'OR OR|OR|OR ORgOReORnORdOReORrOR OR=OR OR'OR0OR'OR"

What i need is

"age = 18 OR name = 'Mistic' OR civilstatus = 'married' OR gender = '0' "

How can i achieve this.

Edit I have read the question and answer of this question and it is not simiar. Because that question is about replacing string and my question is about getting unfamiliar result to my code.

Community
  • 1
  • 1
Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58
  • @ErwinBolwidt This question is not a duplicate with my question. – Lakmal Vithanage Jun 01 '16 at 08:52
  • Why is it not a duplicate? The OP of that question had the same confusion as you had and the answer is the same. You should also look at the highest voted answer to your own question. The answer to the duplicate explains better why you may be confused between `replaceAll` and `replace`. – Erwin Bolwidt Jun 01 '16 at 08:55
  • That is because `replaceAll` takes a regular expression, like `split`. Look at the linked question. – Tunaki Jun 01 '16 at 15:43

7 Answers7

10

Why are you using replaceAll when you are not wanting to replace by regex?

Try just using normal replace

System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' 
    || gender = '0'".replace("||", "OR"));

Output

age = 18 OR name = 'Mistic' OR civilstatus = 'married' OR gender = '0'

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
5

| has a special meaning in regex. You need to escape it.

public static void main(String[] args) {
    String s = "age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' ";
    System.out.println(s.replaceAll("\\|\\|", "OR"));
}

O/P :

age = 18 OR name = 'Mistic' OR civilstatus = 'married' OR gender = '0' 

PS : Alternatively, you can use Pattern.quote() to escape special characters.

String  st = Pattern.quote("||");
System.out.println(s.replaceAll(st, "OR"));
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2

You have to escape the "||"s since they are meta characters in regular expressions.

Use:

System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0'".replaceAll("\\|\\|", "OR"));

The reason why you are getting:

"ORaORgOReOR OR=OR OR1OR8OR OR|OR|OR ORnORaORmOReOR OR=OR OR'ORMORiORsORtORiORcOR'OR OR|OR|OR ORcORiORvORiORlORsORtORaORtORuORsOR OR=OR OR'ORmORaORrORrORiOReORdOR'OR OR|OR|OR ORgOReORnORdOReORrOR OR=OR OR'OR0OR'OR"

Is because the regular expression "||" says "replace an empty string OR an empty string OR an empty string" with "OR". In other words, replace all empty strings with "OR".

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
2

Use Pattern.quote

System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0'".replaceAll(Pattern.quote("||"), "OR"));
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
nitika
  • 303
  • 3
  • 12
2

you can use replace instead of replaceAll, try using the following..

    System.out.println("age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' ".replace("||", "OR"));
Harish Nayak
  • 348
  • 3
  • 18
1

You need to escape the special characters in the input string.

Jobin
  • 5,610
  • 5
  • 38
  • 53
1

You can use following code to replace all '||' with "OR"

String str = "age = 18 || name = 'Mistic' || civilstatus = 'married' || gender = '0' ";
System.out.println(str.replaceAll("\\|\\|", "OR"));
Question Warriors
  • 117
  • 1
  • 1
  • 12