• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Replace string value with a char

Hello... I have thie simple text "Hello Boys & Girls & Parent"..

I want to replace inside that string all & by the Hexa code "0x08".

Thanks...
0
jabcoco
Asked:
jabcoco
  • 5
  • 2
  • 2
2 Solutions
 
CEHJCommented:
s = s.replaceAll("&", "0x08");
0
 
CEHJCommented:
If you mean the actual values 0x08:

String s = "Hello Boys & Girls & Parent".replace('&',(char)8);
0
 
aozarovCommented:
Or you can use s.replace("&", "0x08") if you are using Java 1.5 (will not use regexp in that case and be a bit faster).
Also, if you need it a generic logic to replace a character with its hex value in a string then you can do something like:
char chr = '&';
s = s.replace(new Character(chr).toString(), "0x" + Integer.toString(chr, 16)); // or replaceAll if not 1.5
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jabcocoAuthor Commented:
ok... and how can i replace the "Hello you\r\nWhat?"

how can i replace the \r\n in one step by a simple (char)6 ..

i dont want to replae \r first by (char)6 and then \n by (char)6 because this will give a bad result..
0
 
CEHJCommented:
"Hello you\r\nWhat?".replaceAll("\r\n", "" + (char)6)
0
 
CEHJCommented:
That is

String s =  "Hello you\r\nWhat?";
s = s.replaceAll("\r\n", "" + (char)6);
0
 
jabcocoAuthor Commented:
Great thanks to CEHJ..
nice try aozarov...
0
 
CEHJCommented:
:-)
0
 
aozarovCommented:
Just trying to help :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now