-1

I have a string "ABC DEF" and I would like to add "\s" in between the words to make it "ABC\sDEF" for XPath matches.

String label = "ABC DEF";
String[] arrLabel = label.split("\\s+");
String matches = "\\s";
label=arrLabel[0];
for(int i = 1; i < arrLabel.length; i++){
    label = label + matches + arrLabel[i]; 
    System.out.println("label = " + label); 
}

But I keep getting label printed as ABC\\sDEF. I really want it to be ABC\sDEF.

How should I go about doing this ?

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
sumiati
  • 9
  • 1

1 Answers1

0

why not replace the space directly into \s? try this

String label = "ABC DEF ";
label = label.replaceAll("\\s+(?!$)", "\\\\s");
System.out.println(label);

if your label won't end with spaces use this instead

label = label.replaceAll("\\s+", "\\\\s");
Jin Tim
  • 28
  • 5