java strings letters only

How can I take a String and make it letters only (without punctuation, spaces, etc)
[90 byte] By [hellpmeeeea] at [2007-10-2 0:55:24]
# 1
please help me
hellpmeeeea at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 2

Sure!!! Just make a for cycle and use the .charAt function o fthe String class to add to a new String everithing that is not a space or a punctuation sign, which could be easily done with the indexOf function. All you have to do is a little investigation about how this functions work. I won't tell you becouse it would be to give you the whole solutions without you to make any effort... sorry.

Metallana at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks
hellpmeeeea at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 4

String aString = "This is the string you are editing.";

StringBuffer sb = new StringBuffer(aString);

for (int i = 0; i < sb.length(); i++) {

if (Character.isLetter(sb.charAt(i))) {

continue;

} else {

sb.deleteCharAt(i);

i--;

}

}

// Change original to the new edited version.

aString = sb.toString();

kablaira at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 5
Man, you're telling him EVERITHING!!! Let him do some research, it could be a homework or something...
Metallana at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 6

> Sure!!! Just make a for cycle and use the .charAt

> function o fthe String class to add to a new String

> everithing that is not a space or a punctuation sign,

> which could be easily done with the indexOf function.

> All you have to do is a little investigation about

> how this functions work. I won't tell you becouse it

> would be to give you the whole solutions without you

> to make any effort... sorry.

In a word: no.

That would require you to either hard code editing out everything you don't consider a letter by searching for it, or hard code what you do consider a letter. What happens if you miss a punctuation character in your code? Oops, rewrite, recompile, redistribute. What happens if you want to use it in another language you didn't hard code it for? Oops. Rewrite, recompile, restribute. Java provides functions like Character.isLetter() for a reason.

To the original author: using Character.isLetter(int) would be preferable to Character.isLetter(char) as the latter can't handle supplementary characters.I used it to keep it easier to understand, but if your program is faced with the prospect of handling supplemental characters you should look at the Character API and change it to use the other.

kablaira at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 7

> Man, you're telling him EVERITHING!!! Let him do some

> research, it could be a homework or something...

I thouhgt I'd tell something more...

String.replaceAll() would be easier.

String str = "Test String !@#$% to see if it works!!!";

System.out.println(str.replaceAll("\\W", ""));

Annie.a at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 8

> Man, you're telling him EVERITHING!!! Let him do some

> research, it could be a homework or something...

I would prefer to offer a solution with an OO approach so they can learn. Yes, I could spend a great deal more time explaining to them how to go about it without showing him the actual code, but that quickly becomes so confusing as to be worthless. I'd prefer to think that they'll analyze the code and understand it, which is the real value of any assignment. If they just copy and paste it will catch up to them one way or another and it will be their problem to deal with when it does.

kablaira at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...
# 9

All I say is that it is a very simple program, like he's a starting programmer doing his homework, I don't agree in giving him the whole code, but giving him hoices instead. I know it's just my opinion, but I think It would have been better it you just said "intead of indexOf you can use the Character.isLetter" and don't give him the complete code for solution. I don't want to start a fight, I'm just trying to make you see my point. In a more complex program I agree in giving anyone the code it needs, but in a program this simple, it's better to just help people figuring out their own solutions...

Metallana at 2007-7-15 18:15:20 > top of Java-index,Java Essentials,Java Programming...