Forums Register Login
Java Problem
Hi guys
I am trying that if a string has ', i want to get it replaced with ''.
so if i say dee's it should be replace with dee''s. I can't use replace method because it is used only with char, is there any API or the way to do this.
plz help
In JDK 1.4, there's the "replaceAll()" method. Otherwise, there's the good old "loop over the chars and append each one, possibly with changes, to a StringBuffer" technique we all use to use in the past.
I am not using jdk1.4 , can you explain the old technique little bit more, with an example, if i have multiple single quote then how you do it.
if you have de'e's, then how u do it.
plz help.
Nothing to it, really. In pseudocode:
Sounds like the beginner's forum Let's be nice and specific:

This approach is probably more efficient (and easier to understand). Here... You got more then you asked for
Your approach is fine but i have to intialize single quote and double quote
char sqote =' ' ';
char dquote =' '' ' ;
but i can't intilize like this, i have t use String to intilize both of these. if i use String i can't use charAt method, what other approach i can use then.
Huh? I was trying to let you figure it out for yourself, but Leslie gave you the precise code to do exactly what you want. Pass '\'' and '"' as arguments, and it will work just as advertised. Don't know what you're complaining about.
Indeed, this thread should be moved to the beginner's forum.
Ok, lets elaborate. When you say something, like:

You have a type (char in this case) and a variable name (sqote) that you initialize immediately with some value.
Now, the type char holds one character, but what exactly is a character?
The following is from the JLS

The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double.

Concerning char and in English, this means a char holds some numeric value that (because it's a char) refers to one ASCII (character) value.
In fact, given the ASCII Chart we can say:

and sqote will have a single-quote value. Put more simply, char and int are interchangeable.
Another way (your way) to assign a value to a char, is with using character literals like this.
The surrounding single quotes are marking the begin/end of literal. Now, what if you need a single quote as the value? Doing something like, ''' will give you a complier error since there's some dangling quote there!
What's the solution? The great escape:

The backslash advices Mr. compiler: Hey the next character should be taken literally and not as the end of literal. Mr. compiler then takes the next character, what ever it is, in literal fashion.
In your case, there's another problem with the spaces inside the quotes, which is not allowed and would fail even with dquote.
Also, for consistency I would use escape for dquote even if (in this case) it's not necessary:

Hope this helps
[ December 04, 2003: Message edited by: Leslie Chaim ]

This thread has been viewed 617 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 14, 2018 14:12:51.