English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Doing this in Java. I want to search a string to see if it contains single quote marks. If it does I want them taken out. If not, I want the string left alone. I have tried using StringTokenizer but I am runninginto a problem if the string does not contain single quote " ' ". Please be verbouse on the answer make it clear ..I am stupid.

2006-10-17 11:09:28 · 3 answers · asked by Ryan300x 2 in Computers & Internet Programming & Design

3 answers

Presuming that you are using the String class then you just need to use the replaceAll(x,y) method which will find all substrings of x, and replace them with y.

So if your string is called myString, then put in the code:
myString.replaceAll("'","")

That's a single quotes (inside double quotes) as the first argument, and then the empty string. Hope that helps.

2006-10-17 12:28:39 · answer #1 · answered by inthecrossfire 2 · 0 0

Step through the characters of the string one by one (with a for loop), and check each one as you go. Add the allowable characters to a second, initially empty string, and ignore all others.

I don't know Java so I can't give code, but the algorithm is pretty self-evident.

Rawlyn.

2006-10-17 18:18:03 · answer #2 · answered by Anonymous · 1 0

As Rawlyn says, you want a loop to step through the characters in the string.
In Java you would be well advised to store the String into a StringBuffer as this will alow you to delete the characters you want to be rid of.

2006-10-17 19:00:51 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers