// 字元初始化
void initStaticString(){
NSString *s1 = @"abc";//[NSString stringWithString:@"abc"];
NSString *ss1 = [[NSString alloc] initWithString:s1];
NSString *s2 = [NSString stringWithFormat:@"abcd"];
NSString *s3 = [NSString stringWithUTF8String:"abcde"];
NSString *s4 = [NSString stringWithCString:"abc123" encoding:NSUTF8StringEncoding];
NSString *s5 = [NSString stringWithContentsOfURL:
[NSURL URLWithString:@"www.baidu.com"]
encoding:NSUTF8StringEncoding error:nil];
}
// 字元串截取
void mysubString(){
NSString *str=@"abcdefg123456";
//計算字元串長度
unsigned long n = [str length];
NSLog(@"%ld",n);
//從第3個位置開始截取,索引從3開始
NSLog(@"%@",[str substringFromIndex:3]); //Output:defg123456
//截取到第3個位置,索引到3-1,不包括3
NSLog(@"%@",[str substringToIndex:3]); //Output:abc
//截取一段字元串,從2開始的5個字元串
NSRange range = NSMakeRange(2, 5); //聲明結構體
NSLog(@"%@",[str substringWithRange:range]); //Output:cdefg
//
NSLog(@"%@",[str substringWithRange:NSMakeRange(2, 5)]); //Output:cdefg
NSString *str2=@"1,2,3,a,b,c";
//字元串分割
NSArray *array=[str2 componentsSeparatedByString:@","]; //Output:(1,2,3,a,b,c)
NSLog(@"%@",array);
NSRange rangeSub = [str rangeOfString:@"defg"];
//查找子串首字母在父串中的索引位置
NSLog(@"range.location=%ld",rangeSub.location); //Output:3
//查找子串在父串中占的長度,大于0表示找到了,等于0表示沒找到
NSLog(@"range.length=%ld",rangeSub.length); //Output:4
}
//可變字元串 NSMutableString
void multalbeString(){
NSMutableString *mStr = [[NSMutableString alloc] initWithCapacity:0];
[mStr appendString:@"This is MutableString!"];
NSLog(@"%@",mStr); //Output:This is MutableString!
//在原字元串後面添加格式化字元串
[mStr appendFormat:@"My age is %d\n",23];
NSLog(@"%@",mStr); //Output:This is MutableString!My age is 23
//在原字元串指定位置開始插入字元串
[mStr insertString:@"AAA"atIndex: 8];
NSLog(@"%@",mStr); //Output:This is AAAMutableString!My age is 23
//删除指定範圍内的一個子字元串
[mStr deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"%@",mStr); //Output:is AAAMutableString!My age is 23
//替換指定範圍内的一個子字元串
[mStr replaceCharactersInRange:NSMakeRange(3, 3) withString:@"BBB"];
NSLog(@"%@",mStr); //Output:is BBBMutableString!My age is 23
}
void stringOther(){
NSMutableString *mStr = [[NSMutableString alloc] initWithCapacity:0];
[mStr appendString:@"abc abc abc WO WO WO!"];
//轉換為大寫,傳回一個大寫的新字元串
NSString* up = [mStr uppercaseString];
NSLog(@"up=%@",up);
NSLog(@"mstr=%@",mStr);
//轉換為小寫,傳回一個小寫的新字元串
NSString* low = [mStr lowercaseString];
NSLog(@"low=%@",low);
//傳回一個首字母轉換成大寫的字元串
NSString* Abc = [mStr capitalizedString];
NSLog(@"Abc=%@",Abc);
}
//判斷字首字尾
void stringPreSuf(){
NSString* strURL = @"http://www.music.com/10/love.mp3";
BOOL isHasPrefix = [strURL hasPrefix:@"http://"];
BOOL hasSuffix = [strURL hasSuffix:@".mp3"];
NSLog(@"%d",isHasPrefix);
NSLog(@"%d",hasSuffix);
}
//字元串比較
void stringComare(){
NSString* s1 = @"abc";
NSString* ss1 = @"abc"; //此處ss1 s1 指向的是同一個位址
NSString* s2 = [[NSString alloc]initWithFormat:@"abc"];
if ([s1 isEqualToString:s2]) { //比較字元串的值
NSLog(@"OK!");
}
if (s1 == s2) { //比較字元串的位址,s1==s2 為假 s1==ss1 為真
NSLog(@"OK!");
}
int n = [s1 compare:s2]; //1,0,-1
NSLog(@"%d",n);
}