public static String stripNonAlphaCharsFromString(String str) {
StringBuffer sb = new StringBuffer();
for (int i=0; i<str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch))
sb.append(ch);
}
return sb.toString();
}
If this is homework or an assignment for a class etc it's probably a good idea to come clean now...
As a starter you can loop over the characters in the string testing each character using charAt to get the character at an individual index then turn that into a Character object and use either isLetter or isLetterOrDigit (depends if you want to allow letters). Use a StringBuffer to build a new result string and convert to String at the end.
Edit: Doh! Beaten by Gekko who also gave you the solution so you don't even need to learn!
If (s)he's old enough to learn programming I figure (s)he's old enough to take responsibility for his/her own learning.Edit: Doh! Beaten by Gekko who also gave you the solution so you don't even need to learn!
Not really relevant here, but you could remember it in the future.
When manipulating characters from Strings in Java, if the String is longer than about 300 characters, getting an array using toCharArray and iterating over the array is faster than using charAt. charAt is slightly faster than toCharArray with shorter Strings. (That's since JDK1.4 - with earlier versions the limit was about 15 characters)
New solution: Since ill put all chars in the string to lower case i can just convert each char in the string to an int and check if it is between (including) 97 and 122.
If (s)he's old enough to learn programming I figure (s)he's old enough to take responsibility for his/her own learning.
Don't do that! You are assuming that the string is only using English characters in ASCII or one of the Unicode encodings that is ASCII compatible. The isLetter method should allow for accented letters and so on and is the "correct" way of doing this...
String someString = "abc jalskdf ,.m/;ppodif";
String newString = someString.replaceAll("[^A-Za-z]", "");
Alternatively you can use the String class' replaceAll method with a regular expression. Obviously, its more restrictive than gekko513's solutions, but more simple.
Code:String someString = "abc jalskdf ,.m/;ppodif"; String newString = someString.replaceAll("[^A-Za-z]", "");
public class untitled {
public static void main(String[] args){
String test = " Test this, bitch.! ";
test = test.toLowerCase().trim();
for (int i = 0 ; i < test.length() ; i++){
int aux = test.charAt(i);
if (aux > 122 || aux < 97){
System.out.println (aux); //debugging reasons
String toReplace = "" +test.charAt(i);
test = test.replaceAll(toReplace, "");
System.out.println(test);
}
}
}
Thanks i lot i was looking for something like that, ill just check to see how the "[^A-Za-z]" thing works.
public class TestString {
public static void main(String[] args) {
String someString = "Test -- this, *&%you _(@*#$(*stinky beo!!!tch!";
System.out.println(someString.replaceAll("[^A-Za-z]", ""));
}
}
I just need english characters so dont worry.
I think your attitude is extraordinarily naïve.
"Dont use a canon to kill bee"
U dont even no what the program is supposed to do why argue with me?
Alternatively you can use the String class' replaceAll method with a regular expression. Obviously, its more restrictive than gekko513's solutions, but more simple.
Code:String someString = "abc jalskdf ,.m/;ppodif"; String newString = someString.replaceAll("[^A-Za-z]", "");
someString.replaceAll("[^\\p{L}]", "");
You see, it only took you about twenty seconds of your time to convince me that I would never want you working as a software developer anywhere near me.
And if your code only has to handle English characters, then it certainly doesn't. There are more characters in English than 24 as you said.
My feeling is that if you cheat by copying someone else's code, while it may help you out on a single assignment, it's going to come back to bite you eventually. You're going to need to understand the concepts behind the code and if you don't you'll be in big trouble.If (s)he's old enough to learn programming I figure (s)he's old enough to take responsibility for his/her own learning.
You're right, so if you do copy code, you must make sure you understand what it's doing and why it's done that way. That's what I mean by taking responsibility for own learning.My feeling is that if you cheat by copying someone else's code, while it may help you out on a single assignment, it's going to come back to bite you eventually. You're going to need to understand the concepts behind the code and if you don't you'll be in big trouble.
It took you only 20 seconds to convince me that you probably aren't a very good person to work with in the first place. A know it all who's brash and insulting. Wow!
Exactly why did you bother posting here?
Alternatively you can use the String class' replaceAll method with a regular expression. Obviously, its more restrictive than gekko513's solutions, but more simple.
Code:String someString = "abc jalskdf ,.m/;ppodif"; String newString = someString.replaceAll("[^A-Za-z]", "");
Don't do that! You are assuming that the string is only using English characters in ASCII or one of the Unicode encodings that is ASCII compatible. The isLetter method should allow for accented letters and so on and is the "correct" way of doing this...
I just need english characters so dont worry.
For example :
amajjjfgy, torsrfewf to amajjjfgytorsrfewf
Fair enough. My parents are both teachers and I think I must have inherited the annoying teachers habit of giving enough information so as I think you should be able to solve the problem without actually solving the problem for you
My feeling is that if you cheat by copying someone else's code, while it may help you out on a single assignment, it's going to come back to bite you eventually. You're going to need to understand the concepts behind the code and if you don't you'll be in big trouble.