Check if NSString "000001XX"is a number with intValue - nsstring

Now I am trying to add a front end check on my app to detect if user input only number in the textfield.
I use:
- (IBAction)checkID:(UITextField *)sender {
if ([sender.text isEqualToString:#""]) {
sender.text = #"This information is required";
sender.backgroundColor =[UIColor redColor];
}else if (![sender.text intValue]) {
sender.text = [sender.text stringByAppendingString:#" is not valid number"];
sender.backgroundColor =[UIColor redColor];
}
NSLog(#"send.text is %#, intValue is %d",sender.text,[sender.text intValue]);
}
But I found it text begins with number and ends with string, its intValue is still the number.
In my text, text is "00001aa", but the intValue is 1.
Is there any other way to filter out this "00001aa" text?
Thanks in advance.

Yes, you can use NSRegularExpression, or NSCharacterSet (works for positive numbers).
For regular expressions, use ^[-+]?[0-9]+$.
NSRegularExpression *numEx = [NSRegularExpression
regularExpressionWithPattern:#"^[-+]?[0-9]+$" options:0 error:nil
];
NSLog(#"%ld", [numEx numberOfMatchesInString:#"-200" options:0 range:NSMakeRange(0, 4)]);
NSLog(#"%ld", [numEx numberOfMatchesInString:#"001A" options:0 range:NSMakeRange(0, 4)]);
For character set, use [NSCharacterSet decimalDigitCharacterSet].
BOOL isNum = [[NSCharacterSet decimalDigitCharacterSet]
isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:#"001AA"]
];

Related

NSPredicate Character and/or Letter Substitutions

I've seen some, somewhat, similar questions about filtering out special characters in a predicate - but nothing suitable for my needs. I'm not really sure if what I want to do is even possible but I'll put it to the community instead:
I'm working with strings of pronunciations for a dictionary that contain a lot of IPA characters for instance:
ȵ
ɕ
ŋ
Ẓ
What I would like to do is set up some substitutions for these characters, something like:
n=ȵ
x=ɕ
ng=ŋ
r=Ẓ
in a search predicate so that a search for n* would result with anything beginning in ȵ, etc, etc...
Even other IPA 'letters' like "v" would be best resembled by a "w" if there is a simple way to switch in SEARCH letters for other STRING letters that would be great...
It seems like this should be done outside the predicate. Create a dictionary where the keys are the strings to be replaced and the values are the replacements. Then, have a loop which iterates over the dictionary and performs each replacement on the string that you will be searching. The resulting string after replacement is the string that you use in the predicate.
got the answer from here:
stackoverflow.com/questions/21852502/nspredicate-ignore-numbers-in-string-pinyin
an other question to which I asked...
this is the code [incase this is helpful for anyone else later] I used for string "pro" in class Words
- (NSString *)searchableStringValueTwo {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"ȵ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"n"];
return searchString;
}
- (NSString *)searchableStringValueThree {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"ɕ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"x"];
return searchString;
}
- (NSString *)searchableStringValueFour {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"ŋ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"ng"];
return searchString;
}
- (NSString *)searchableStringValueFive {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"Ẓ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"r"];
return searchString;
}
- (NSString *)searchableStringValueSix {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"v"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"w"];
return searchString;
}

sqlite get single row

NSString *cityInput = cityField.text;
NSString *code = #"";
NSString *query = #"SELECT code FROM country WHERE cityname = UPPER(?)";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String],-1, &statement, nil) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [cityInput UTF8String], -1, SQLITE_STATIC);
while(sqlite3_step(statement) == SQLITE_ROW) {
code = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
}
}
if(sqlite3_step(statement) != SQLITE_DONE){
NSLog(#"DB: query KO");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"ALERT" message:#"City not found" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
return;
}
sqlite3_close(database);
Two questions:
1) for get single row i must use while loop?
2) if there isn't result in the query how alert "city not found"
Obviously you will get the last row,,
code
should be an array u might declare it like this
NSMutableArray *code=[NSMutableArray array];
then in the while loop use
[code addobject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
I hope this is clear enough:)
Using while gives you the last row and it is not needed. Add limit 1 clause to your query, or simply take the first row returned;
Decide whether to show alert by checking return code.
Maybe two answers,
1) No, just an if clause will do
2) Do an assignement and then run an if clause on it
int result = sqlite3_step(statement)
if (result == SQLITE_ROW){
//processline
}
else{
//show alert
}

NSTimeInterval from text input

I got a question for anyone with a spare moment and an answer...
I need to change an NSString value to input into the AVAudioPlayer start offset
eg. player.currentTime = 0; I want to change the '0' to a readable value or an NSString to a numeral value.....
NSString has a integerValue function. You can also create an NSString using it's format class function and pass it a format string like this: "%d" and then your player.currentTime.
int timeInput = [[textField text] integerValue];
label.text = [NSString stringWithFormat:#"%d", timeInput];

How can I add leading whitespace in front of the NSString?

I need the String always have length 5...
for example, if
I pass hello", I get "hello"
I pass "xml", I get " xml" (two spaces before "xml");
I pass "i", I get " i" (four spaces before "i").
What is the most efficient way to do it? Thank you.
I suppose this might help:
int maxLength = 5;
NSString *text = #"xml";
NSString *result = [text stringByPaddingToLength:(maxLength-[text length]) withString:#" " startingAtIndex:0];
NSMutableString *yourString;//your string
NSMutableString *string;
if([string length]<5){
NSInteger length = 5-[string length];
for(int i=0;i<length;i++){
[string appendFormat:#" "];
}
[yourString appendString:string];
}
//now you can use yourString .it is formatted.

Building an NSstring out of an NSMutableArray

My eyes hurt from hours of trying to figure this one - and i have looked for an answer for quite a while on-line (it will be embarrassing to tell how much...).
all i am trying to do is to enumerate using a for-in loop on anExpression which is a NSMutableArray that holds NSNumbers and NSStrings.
my NSLog print for the variable ans returns an empty string.
What am i doing wrong?
NSString *ans = #"";
for (id obj in anExpression)
{
if ([obj isKindOfClass:[NSString class]])
[ans stringByAppendingString:(NSString *)obj];
if ([obj isKindOfClass:[NSNumber class]])
[ans stringByAppendingString:(NSString *)[obj stringValue]];
NSLog(#"String so far: %# ", ans);
}
I think you mean
ans = [ans stringByAppendingString:(NSString *)obj];
not just
[ans stringByAppendingString:(NSString *)obj];
NSStrings are immutable -- you can't append to them. -stringByAppendingString: returns a new string (which you could then assign to ans).
Alternatively, you might use an NSMutableString and the -appendString: method.
Hey, sorry for the bad coding format, posting it again ...
NSString *ans = #"";
for (id obj in anExpression)
{
if ([obj isKindOfClass:[NSString class]])
[ans stringByAppendingString:(NSString *)obj];
if ([obj isKindOfClass:[NSNumber class]])
[ans stringByAppendingString:(NSString *)[obj stringValue]];
NSLog(#"String so far: %# ", ans);
}
[ans autorelease];
NSLog(#"final string is: %# ", ans);
return ans;
the method stringByAppendingString: returns a new string made by appending the given string to the receiver.
so you want ans = [ans stringByAppendingString:obj];

Resources