標籤:blog http com string os set
/--------操作字串--NSString(靜態字串)---------------------
NSString * a = @"a+b+c+a+b+d";
NSArray * m = [a componentsSeparatedByString:@"+"];//字串根據某個字串拆分
NSRange x = [a rangeOfString:@"b"];//尋找子字串在總字串的範圍
NSLog(@"%lu, %lu", x.location , x.length);
NSString * r = [a stringByReplacingOccurrencesOfString:@"+" withString:@"-"];//把某個字串替換成另一個字串
NSLog(@"%@", r);
NSString * str = @"wo shi xiao hong jun!";
NSString * result = [str uppercaseString];//小寫字母變成大寫
NSLog(@"%@", result);
NSString * result1 = [str capitalizedString];//第一個單詞變成大寫
NSLog(@"%@", result1);
NSString * result2 = [result lowercaseString];//大寫字母改為小寫
NSLog(@"%@", result2);
NSString * Beijing= @"河南"; //字串的聲明
NSString * [email protected]"河南歡迎您a"; //[NSString stringWithFormat:@"I am ‘%@‘", Beijing]; //字串格式化
NSString * zhui = [Beijing stringByAppendingString:@"啦啦啦"]; //字串追加
bool b=[Beijing isEqualToString:log]; //字串比較
NSString * hh = @"http://www.cnblog.com";
if([hh hasPrefix:@"http"]){ //尋找以http開頭的字串
NSLog(@"含有http");
}else{
NSLog(@"沒有http");
}
NSString * ss = @"123";
int a = [ss intValue]; //字串轉int型
double dd = [ss doubleValue]; //字串轉double型
NSLog(@"%g", dd);
//字串轉數組
NSString * zifuchuan [email protected]"one, two, three, four";
NSLog(@"string:%@", zifuchuan);
NSArray * array = [zifuchuan componentsSeparatedByString:@","];//用,把字元分成數字元素
// NSLog(@"array:%@", array); //輸出整個數組中所有元素
NSString * value = [array objectAtIndex:0]; //取出第0個元素
NSLog(@"value:%@", value);
//數組轉字串
NSString * zifuchuan2 = [array componentsJoinedByString:@","];//用,把各個數字組成字串
NSLog(@"zifuchuan2:%@", zifuchuan2);
//-substringToIndex: 從字串的開頭一直截取到指定的位置,但不包括該位置的字元
NSString * string1 = @"This is a string";
NSString * string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//-substringFromIndex: 以指定位置開始(包括指定位置的字元),並包括之後的全部字元
NSString * string1 = @"This is a string";
NSString * string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//-substringWithRange: //按照所給出的位置,長度,任意地從字串中截取子串
NSString * string1 = @"This is a string";
NSString * string2 = [string1 substringWithRange:NSMakeRange(0, 4)];//0帶表下標,4表示長度
NSLog(@"string2:%@",string2);
//--------操作動態字串--NSMutableString----------------------------------------------------
NSMutableString * mstr = [[NSMutableString alloc] init];
NSString * str1 = @"This is a example.";
//建立可變字串
mstr = [NSMutableString stringWithString:str1];
//插入字元
[mstr insertString:@"very easy " atIndex:10];
//刪除一些字元
[mstr deleteCharactersInRange:NSMakeRange(10,5)];
//尋找並刪除
NSRange substr = [mstr rangeOfString:@"example"]; //字串尋找,可以判斷字串中是否有
if (substr.location != NSNotFound) {
[mstr deleteCharactersInRange:substr];
}
//重新設定字串
[mstr setString:@"This is string AAA"];
//替換字串
[mstr replaceCharactersInRange:NSMakeRange(15, 2) withString:@"BBB"]; //從第15個字串處替換掉後2個字串
//尋找第一個並替換
NSString * search = @"This is";
NSString * replace = @"An example of";
substr = [mstr rangeOfString:search];
if (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace]; //把第1個遇到的substr替換為replace
NSLog(@"%@", mstr);
}
//尋找全部匹配的,並替換
search = @"a";
replace = @"X";
substr = [mstr rangeOfString:search];
while (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];
substr = [mstr rangeOfString:search];
}
NSLog(@"%@", mstr);