How to check a string is a number or letter [duplicate] - ios

This question already has answers here:
Check that a input to UITextField is numeric only
(22 answers)
iOS - how do I check if a string is numeric or not? [duplicate]
(1 answer)
Closed 9 years ago.
I want to check a string is a number or letter in ios. eg "1" or "error" can anybody tell me how to do this
Thanks

You can use NSScanner:
NSScanner *scan = [NSScanner scannerWithString:yourString];
if (![scan scanFloat:NULL] || ![scan isAtEnd])
{
NSlog (#"%#", #"Is a string");
}
else
{
NSlog (#"%#", #"Is a number");
}

BOOL valid;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:String];
valid = [alphaNums isSupersetOfSet:inStringSet];
if (!valid) // Not numeric

Related

Appending NSString with Zeros depending upon length [duplicate]

This question already has answers here:
objective-c right padding
(3 answers)
Closed 5 years ago.
I have a string, length may vary but I have to check if its length is less then 12 and if yes append those many zeros to it.
Like:
str = Test123, output should be Test12300000
str = Test123456, output should be Test12345600
Only 12 Char is allowed in string.
Tried below code but not getting generic result. There must be a better and easy way to do.
- (NSString *)formatValue:(NSString *)str forDigits:(NSString *)zeros {
return [NSString stringWithFormat:#"%#%#", str ,zeros];
}
how about something like this:
- (NSString *)stringToFormat:(NSString *)str {
while (str.length <12) {
str = [NSString stringWithFormat:#"%#0", str];
}
return str;
}
This is fast and simple, I think.
- (NSString *)stringToFormat:(NSString *)str {
return [str stringByAppendingString:[#"000000000000" substringFromIndex:str.length]];
}
Just make sure the string is never more than 12 characters.

How can I extract the first 3 characters from an NSString? [duplicate]

This question already has answers here:
how to get first three characters of an NSString?
(3 answers)
Closed 6 years ago.
How can I extract the first 3 characters from a NSString?
For example if I have a string "1234820" How can I extract the numbers 123 from the string and store the result in a new string with the format "1.23 Million" ?
I am counting the the number of characters in the string by using
-(NSString *)returnFormattedString:(NSString *)stringToFormat{
NSString *formatedString;
NSUInteger characterCount = [stringToFormat length];
if (characterCount > 6) {
//???? How do I extract and add a decimal
stringWithThreeCharactersAndDecimal = ????;
NSString *string = [NSString stringWithString:stringWithThreeCharactersAndDecimal];
formatedString = [string stringByAppendingString:#"Million"];
}
return formatedString;
}
do like
assume that is your String
yourString = #"1234820";
// use substringToIndex for fetch First Three Character
yourString=[yourString substringToIndex:3];
// finally convert string to as like 1.23
NSString *finalStr = [NSString stringWithFormat:#"%.2f Million", [yourString floatValue]];
Choice -2
as per Droppys short and good answer is
NSString *finalStr = [NSString stringWithFormat:#"%.2f Million", [yourString floatValue] / 100.0f];

Extract integer from a string in objective c [duplicate]

This question already has answers here:
Objective-C: Find numbers in string
(7 answers)
Closed 7 years ago.
i have a string which has both numeric and character value.
Like: string = abc1234
Now I want to get only the integer part from it:
i.e. 1234
How can I do this? I have tried the following with no luck:
NSString *str = #"abc123";
int s = [str intValue];
Use this function
- (NSString *)extractNumberFromText:(NSString *)text
{
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:#""];
}
It will help you.Thankyou

How to get particular string from my response? [duplicate]

This question already has answers here:
Objective-C Split()?
(5 answers)
Closed 8 years ago.
I have received below string response
USA : United states of america
i need to get before ":" data's. Below I have posted sample what i need?
USA
Hi i think you need to check if the ":" char is exists first because it will crash.
here is example for how to do it:
NSString *yourString = #"USA : bla bla bal";
NSRange range = [yourString rangeOfString:#":"];
if(range.location != NSNotFound) // string contains
{
int indexOf = range.location;
NSString *prefix = [yourString substringWithRange:NSMakeRange(0, indexOf)];
// prefix = "USA "
}
if you want you can trim the prefix string and remove all the empty spaces like this
NSString *trimmedString = [prefix stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
Try like this:-
NSString*yourStr=#"USA : United states of america";
NSString *str=[yourStr componentsSeparatedByString:#":"][0];
NSLog(#"%#",str);

NSString is a number? [duplicate]

This question already has answers here:
Check that a input to UITextField is numeric only
(22 answers)
Closed 8 years ago.
I have a TextField and wanted to know if the user just pressed numbers
eg::
_tfNumber.text only has numbers?
is there any function on NSString for this?
This will let you know if all of the characters are numbers:
NSString *originalString = #"1234";
NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharacterSet];
NSString * trimmedString = [originalString stringByTrimmingCharactersInSet:numberSet];
if ((trimmedString.length == 0) && (originalString.length > 0)) {
NSLog(#"Original string was all numbers.");
}
Note that this ensures it won't give a false positive for the empty string, which technically also doesn't contain any non-numbers.
try this:
NSCharacterSet *_NumericOnly = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *myStringSet = [NSCharacterSet characterSetWithCharactersInString:mystring];
if ([_NumericOnly isSupersetOfSet: myStringSet]) {
NSLog(#"String has only numbers");
}
I got it from: http://i-software-developers.com/2013/07/01/check-if-nsstring-contains-only-numbers/
You can use this method in your UITextField's delegate method textField:shouldChangeCharactersInRange:replacementString: and do the verification while the user is typing.
No, but it should be easy to write:
- (BOOL)justContainsNumbers:(NSString *)str {
if ([str length] == 0)
return NO;
for (NSUInteger i = 0; i < [str length]; i++)
if (!isdigit([str characterAtIndex:i]))
return NO;
return YES;
}
Let's try regular Expression,
NSString * numberReg = #"[0-9]";
NSPredicate * numberCheck = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", numberReg];
if ([numberCheck evaluateWithObject:textField.text])
NSLog (#"Number");
No. NSString is not an NSNumber and any values you get from a UITextField will be an NSString. See THIS SO answer for converting that entered NSString value into an NSNumber.

Resources