Forums Register Login

How to replace apostophes?

+Pie Number of slices to send: Send
I want to replace all apostrophes in myString with \'

myString = myString.replace(''', '\''); doesn't work. What's the solution?

Cheers,
James
+Pie Number of slices to send: Send
The backslash is an escape character in Java string literals. If you want to have a literal backslash in a string literal, you must write \\ instead of \.

Note also that you cannot put more than one character between single quotes (a char literal is exactly one character).
+Pie Number of slices to send: Send
Sorry Jesper, I wasn't very clear.

The code I want to use is:



but it has to be Java 1.1 compatible, so it must be two characters I pass to the replace method... Is there another solution?

Cheers,
James
+Pie Number of slices to send: Send
You cannot fit two characters: \' in a char literal, so you cannot use the replace() method that takes two chars.

If Java 1.1 doesn't have the replace() method that takes strings, then you'll have to program it yourself. Loop through the input string character by character, copy the characters to a StringBuffer, and when you encounter a ' in the input string, write \' to the StringBuffer. At the end call toString() on the StringBuffer to get the output string.
+Pie Number of slices to send: Send
 

Originally posted by James Hodgkiss:
Sorry Jesper, I wasn't very clear.

The code I want to use is:



but it has to be Java 1.1 compatible, so it must be two characters I pass to the replace method... Is there another solution?

Cheers,
James




In 1.1 looks like you can use":
StringBuffer, int charAt() , and insert(int, char) to do the same thing as replace.
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 913 times.
Similar Threads
problems with replaceAll and slash character
how to check whether a string is null or not
determine the input type of the value of the function parameter map
Int to String
how to replace eol and nl
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:47:54.