02-21-2011, 08:32 PM
#1 (permalink )
Registered Member
Join Date: Feb 2011
Location: Chicago, IL
Posts: 15
if char in "=-)(*&^%$#..."
Hi all,
I was wondering if there was an easy was to check if there were special characters in a string..
In python programming language I know you can do:
for chars in someString:
if chars in "-)(*&^%^%$##@@;':,.":
do something...
It would basically be to check for special characters in a string and alert the user that they are not allowed...
Thanks
02-21-2011, 09:36 PM
#2 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
How about this?
Code:
-(NSString*) replaceBadWords:(NSString*)userText {
NSArray *badWordsArray= [NSArray arrayWithObjects:@"!", @"@", @"#", @"$", nil];
for(NSString* s in badWordsArray){
userText =[userText stringByReplacingOccurrencesOfString:s withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[userText length])];
}
return userText;
}
userText is the NSString
02-21-2011, 10:09 PM
#3 (permalink )
Registered Member
Join Date: Feb 2011
Location: Chicago, IL
Posts: 15
Hm, I just read about the for each loop and I thought of using it... But its just really code consuming to have to write an array with all the special characters in...
I might find a way using the ASCII code starting first special character and incrementing until the last one...
But I wished there was an easy function like in python...
Thanks for the help
02-21-2011, 10:12 PM
#4 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Take a look at NSCharacterSet.
02-22-2011, 07:35 AM
#5 (permalink )
Applesnaps
Join Date: Oct 2009
Location: Almere, The Netherlands
Posts: 7
Quote:
Originally Posted by
Gefthebest
Hi all,
I was wondering if there was an easy was to check if there were special characters in a string..
In python programming language I know you can do:
for chars in someString:
if chars in "-)(*&^%^%$##@@;':,.":
do something...
It would basically be to check for special characters in a string and alert the user that they are not allowed...
Thanks
For more advanced options use a
regular expression . Just drag and drop .h and .m file from
RegexKitLite or any other regex framework and of you go.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 383
17 members and 366 guests
Absentia , apatsufas , BinHex , cpsclicker , dre , Error404 , Gaz , gmarro , jeroenkeij , Kirkout , mottdog , Music Man , PavelMik , teebee74 , whitey99 , Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker