Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Ios determines whether a string is a pure number.
Ios determines whether a string is a pure number.
The first method is to use NSScanner:

1. plasticity judgment

-(BOOL)isPureInt:(ns string *)string {

ns scanner * scan =[ns scanner scannerWithString:string];

int val

return[scan scan int:& amp; val]& amp; & amp[ scan isattend];

}

2. Floating-point shape judgment:

-(BOOL)isPureFloat:(ns string *)string {

ns scanner * scan =[ns scanner scannerWithString:string];

Floating value;

Return to [scan scan float:&; val]& amp; & amp[ scan isattend];

}

The second method is to use circular judgment.

-(bool) ispurenumandcharacters: (nsstring *) text

{

for(int I = 0; I < [text length]; ++i) {

int a =[text character ating index:I];

if ([self isNum:a]){

Continue;

} Otherwise {

Return no;

}

}

Return YES

}

It is also a common way of C language.

-(BOOL)isAllNum:(ns string *)string {

unichar c;

for(int I = 0; I< string. Length; i++) {

C =[ string character index: I];

If (! isdigit(c)) {

Return no;

}

}

Return YES

}

The third method is to use trimming method of NSString.

-(bool) ispurenumandcharacters: (nsstring *) string

{

string =[string stringByTrimmingCharactersInSet; [NSCharacterSet decimalDigitCharacterSet]];

if(string . length & gt; 0)

{

Return no;

}

Return YES

}

The above three functions can help to judge whether it is a number. There is no direct method to judge whether it is a number in iOS, so you can only add your own method to realize it.