Results 1 to 6 of 6
- 10-13-2009, 03:15 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Search string for non-alphabetic characters
Is there an easier way to search a string for non-alphabetic characters than this??
x = 0;
while (x != firstName.length()){
if ( firstName.charAt(x) == 'a' || firstName.charAt(x) == 'b' || firstName.charAt(x) == 'c' || firstName.charAt(x) == 'd' || firstName.charAt(x) == 'e' ||
firstName.charAt(x) == 'f' || firstName.charAt(x) == 'g' || firstName.charAt(x) == 'h' || firstName.charAt(x) == 'i' || firstName.charAt(x) == 'j' ||
firstName.charAt(x) == 'k' || firstName.charAt(x) == 'l' || firstName.charAt(x) == 'm' || firstName.charAt(x) == 'n' || firstName.charAt(x) == 'o' ||
firstName.charAt(x) == 'p' || firstName.charAt(x) == 'q' || firstName.charAt(x) == 'r' || firstName.charAt(x) == 's' || firstName.charAt(x) == 't' ||
firstName.charAt(x) == 'u' || firstName.charAt(x) == 'v' || firstName.charAt(x) == 'w' || firstName.charAt(x) == 'x' || firstName.charAt(x) == 'y' ||
firstName.charAt(x) == 'z' || firstName.charAt(x) == 'A' || firstName.charAt(x) == 'B' || firstName.charAt(x) == 'C' || firstName.charAt(x) == 'D' ||
firstName.charAt(x) == 'E' || firstName.charAt(x) == 'F' || firstName.charAt(x) == 'G' || firstName.charAt(x) == 'H' || firstName.charAt(x) == 'I' ||
firstName.charAt(x) == 'J' || firstName.charAt(x) == 'K' || firstName.charAt(x) == 'L' || firstName.charAt(x) == 'M' || firstName.charAt(x) == 'N' ||
firstName.charAt(x) == 'O' || firstName.charAt(x) == 'P' || firstName.charAt(x) == 'Q' || firstName.charAt(x) == 'R' || firstName.charAt(x) == 'S' ||
firstName.charAt(x) == 'T' || firstName.charAt(x) == 'U' || firstName.charAt(x) == 'V' || firstName.charAt(x) == 'W' || firstName.charAt(x) == 'X' ||
firstName.charAt(x) == 'Y' || firstName.charAt(x) == 'Z'){
}else { //
throw new nonAlphabeticException("Must only be alphabetic characters");
-
yikes!
Even that won't work. You could use regular expressions to solve this, but I have a feeling that this is for an intro course. Otherwise, you could simply have a testString
String testString = "abcdefghijklmnopqrstuvwxyz";
and test to see if each char in your string, changed to lower case, is contained in the testString. See the String API for more details on the String#contains(....) method.
- 10-13-2009, 03:25 AM #3
Java Code:String remainingJunk = firstName.replaceAll("[a-zA-Z]","");My Hobby Project: LegacyClone
- 10-15-2009, 06:01 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Thanks for your help!!
- 10-16-2009, 01:19 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
While checking every character against the contents of the string will work, it's a rather brute force method and horribly inefficient. Consider in the worst case comparing "zzzzzz"
against "ABCD ..Zabcd...z"). You have to make 52 comparisons for every character.
An "is_alpha(char unknown_char) is easy to write. In english ascii, characters are coded in sequence [Cap letters], [Numbers and some others],[Low case letters]. IIRC correctly,
A is X40 and a is X60. You can check that yourself.
code
boolean IsAlpha(char a){
return ((a = 'A' && a <= 'Z') ||
(a >= 'a' && a = 'z')
}
Realizing that we're encouraged to not provide explicitly coded answers, it is so much easier to write the function in code than to explain in english. I think the pedegogy is the same if the OP reads the code. Besides, there are deliberate bugs that are easy to find if the code is understood (edit: and one that wasn't intentional). Can be extended to isAlphaNumeric(), isControlCharacter(), et al. You can revise the function to "IsNotAlpha", or simply use !IsAlpha(a)
This is also C code and I'm not being careful about using production code. In C, char would probably be an int in production code, but char is a java primitive, so this should be pretty close.
Anyway, there are only 4 comparisons instead of 52.Last edited by rdtindsm; 10-16-2009 at 01:25 AM.
-
Similar Threads
-
characters from a string into an integer
By 2potatocakes in forum New To JavaReplies: 7Last Post: 05-08-2012, 12:31 PM -
deleting characters from a String
By Hayzam in forum New To JavaReplies: 4Last Post: 08-29-2008, 12:14 PM -
how to get the characters one by one from a String?
By Somitesh Chakraborty in forum New To JavaReplies: 3Last Post: 08-20-2008, 08:56 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
Getting all characters in a String
By Alayna in forum New To JavaReplies: 2Last Post: 05-20-2007, 11:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks