NSString関連アクション
4988 ワード
//
NSString * str_1 = @"Hello"; //
int a = 123;
NSString * str_2 = [NSString stringWithFormat:@"%d",a];
// C OC
NSString * str_3 = [NSString stringWithUTF8String:"hello world"];
NSString * str_4 = [NSString stringWithFormat:@"%s","hello world"];
//
NSString* str_5 = @"123";
int b=[str_5 intValue];//
float c=[str_5 floatValue];// float
double d=[str_5 doubleValue];// double
// NSString Message(OC :typedef unsigned short unichar)
NSLog(@"%ld",[str_1 length]);//
NSLog(@"%c",[str_1 characterAtIndex:1]);//
BOOL ret0 = [str_1 isEqualToString:str_2];// YES(1) NO(0)
long ret1 = [str_1 compare:str_2];// str1 1 0 -1
long ret2 = [str_1 caseInsensitiveCompare:str_2];//
NSString * ptr = [str_1 uppercaseString];//
ptr = [str_1 lowercaseString];//
ptr = [str_3 capitalizedString];//
NSString * str_6 = @"hello world";
NSRange range = [str_6 rangeOfString:@"wr"];// NSNotFound location length
if (range.location != NSNotFound) {
NSLog(@"%ld %ld",range.location, range.length);
}
NSString * str_7 = @"helloworld";
NSString * ptr1 = [str_7 substringToIndex:4];// 0 4 4
NSString * ptr2 = [str_7 substringFromIndex:5];// 5 5
NSRange range1 = {4,3};//
NSString * ptr3 = [str_7 substringWithRange:range1];// range
NSString * ptr4 = [str_7 substringWithRange:NSMakeRange(4,3)];//NSMakeRange
NSString * str_8 = @"www.1000phone.com";
BOOL ret = [str_8 hasPrefix:@"www"];//
NSString * str_9 = @"1.txt";
BOOL ret4 = [str_9 hasSuffix:@".txt"];//
//NSMutableString ( ) NSString NSString
NSMutableString * str_10 = [[NSMutableString alloc]initWithString:@"hello"];//
[str_10 insertString:@"123" atIndex:1];// ( ) NSString
[str_10 appendString:@"123"];//
[str_10 deleteCharactersInRange:NSMakeRange(0, 2)];// length
[str_10 setString:@"qianfeng"];//
[str_10 replaceCharactersInRange:NSMakeRange(3, 1) withString:@"ios"];// length