「iOS開発」文字列操作方法まとめ(例componentsSeparatedByString()、stringrangeOfString())
NSString *cftAA = @"234″;
NSString *cftBB = @"678″;
1.文字列連結
NSString *newString = [NSString stringWithFormat:@"%@%@",cftAA,cftBB];
2.文字列->int
int intString = [newString intValue];
3.int->文字列
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
4.文字列->float
float floatString = [newString floatValue];
5.float->文字列
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
6.文字列分割
SString*string =@"startnews24 change";
string = [string substringToIndex:7];
NSLog(@"substring value :%@",string);
[string substringFromIndex:2];
NSLog(@"substring value :%@",string);
7.stringrangeOfString()正規表現
NSString*string =@"startnews24 pchange";
NSRangerange = [stringrangeOfString:@"f"];
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];
NSLog(@"after value:%@",string);
8.componentsSeparatedByString()文字列分割
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"];
NSLog(@"array:%@",array);