-
String replace with "\"
I just want to replace in a string all occurrences of the ' caracter by the double caracters "\\\"", so for example:
Hello 'big' world
should become
Hello \"big\" world
But the replace method of String looses my backslach caracter in the replacement string.
Look:
public static void main(String[] args) {
String string="Hello 'big' world";
System.out.println("Before:" + string + " length=" + string.length());
String rpstring = "\\\"";
string=string.replaceAll("'", rpstring);
System.out.println("After:" + string + " length=" + string.length());
System.out.println("repalce string:" + rpstring + " length=" + rpstring.length());
}
That gives:
Before:Hello 'big' world length=17
After:Hello "big" world length=17
repalce string:\" length=2
How can I do ?
Thanks,
Bernard
-
Hi Bernard,
I don't know whats ur need. If its just to convert all the single quote character to double quote character, than ur code 100% correct. This is because ur getting
hello "big" world after replacing which means ur string variable value is "hello \"Big\" world".
If ur requirement is something else than make the following change.....
String rpstring = "\\\\\\\"";
And this will work.
Kalai Selvan T.
-
Hi Kalai,
String rpstring = "\\\\\\\"";
thats's what I needed,
thanks a lot.
BP.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks