I have a string like this
NSString *string = @"开发者_StackOverflowfeng2zhong3"
and I want to split this string to feng2
and zhong3
, how to do that?
Try:
NSString* feng = [string substringToIndex:4]
NSString* zhong = [string substringFromIndex:5]
EDIT:
Now that you have made it clear what your input data is like, one option is to use regexes to do the matches like this:
NSString* string = @"feng4shui5";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\D+\d+" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSRange firstHalfRange = [match rangeAtIndex:1];
NSRange secondHalfRange = [match rangeAtIndex:2];
NSLog([userinput substringWithRange:matchRange]);
}
where string is the string that contains your input.
You could use +[NSString rangeOfCharacterFromSet:options:range:]
with a +[NSCharacterSet decimalDigitCharacterSet]
and get the substrings via the resulting ranges.
Or you could use NSScanner
by using:
NSCharacterSet *digitSet = [NSCharacterSet decimalDigitCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:inputString];
... and successive calls of:
success = [scanner scanUpToCharactersFromSet:digitSet intoString:&namePart];
// ...
success = [scanner scanCharactersFromSet:digitSet intoString:&digitPart];
// ...
use substringFromIndex and substringToIndex methods
the below code is use full for you.it is working fine for your requirements
NSMutableString *s = @"fig2sine3";
NSCharacterSet *removeCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
array=[s componentsSeparatedByCharactersInSet:removeCharSet];
nslog(@"the devided strings %@",array);
This is absolutely useful for your requirement.
精彩评论