不積跬步,無以至千裡;不積小流,無以成江海.
NSString的建立
建立常量字元串,注意使用“@“符号
NSString *astring = @"This is a String!";
建立空字元串,給予指派。
NSString *s = [[NSString alloc] init];
s=@"quanzi";
建立格式化字元串:占位符(由一個%加一個字元組成)
[NSString stringWithFormat:@"quanzi%i歲了", 30];
字元串比較
NSString *str1 = @"quanzi";
NSString *str2 =@"quanzi";
if (str1 == str2) {
NSLog(@"字元串位址一樣");
}
if ([str1 isEqualToString:str2]) {
NSLog(@"字元串内容一樣");
}
(NSComparisonResult)compare:(NSString *)string
- 這個方法可以用來比較兩個字元串内容的大小
- 比較方法: 逐個字元地進行比較ASCII值,傳回NSComparisonResult作為比較結果
- NSComparisonResult是一個枚舉,有3個值:
- 如果左側> 右側,傳回NSOrderedDescending,
- 如果左側< 右側,傳回NSOrderedAscending,
- 如果左側== 右側傳回NSOrderedSame
NSString *str1 = @"abc";
NSString *str2 = @"abd";
switch ([str1 compare:str2]) {
case NSOrderedAscending:
NSLog(@"後面一個字元串大于前面一個");
break;
case NSOrderedDescending:
NSLog(@"後面一個字元串小于前面一個");
break;
case NSOrderedSame:
NSLog(@"兩個字元串一樣");
break;
}
輸出結果: 後面一個字元串大于前面一個
(NSComparisonResult) caseInsensitiveCompare:(NSString *)string;
- 忽略大小寫進行比較,傳回值與compare:一緻
NSString *str1 = @"abc";
NSString *str2 = @"ABC";
switch ([str1 caseInsensitiveCompare:str2]) {
case NSOrderedAscending:
NSLog(@"後面一個字元串大于前面一個");
break;
case NSOrderedDescending:
NSLog(@"後面一個字元串小于前面一個");
break;
case NSOrderedSame:
NSLog(@"兩個字元串一樣");
break;
}
輸出結果:兩個字元串一樣
字元串搜尋
(BOOL)hasPrefix:(NSString *)aString;
是否以aString開頭
NSString *str1 = @"abcdef";
NSString *str2 =@"ab";
bool b = [str1 hasPrefix:str2];
NSLog(@"%i",b);
(BOOL)hasSuffix:(NSString *)aString;
是否以aString結尾
NSString *str3=@"ef";
bool b2 = [str1 hasSuffix:str3];
NSLog(@"%i",b2);
(NSRange)rangeOfString:(NSString *)aString;
1.用來檢查字元串内容中是否包含了aString
2.如果包含, 就傳回aString的範圍
3.如果不包含, NSRange的location為NSNotFound, length為0
NSRange r = [str1 rangeOfString:str2];
NSLog(@"長度是=%i,位置是=%i",r.length,r.location);//長度是=2,位置是=0
字元串截取
(NSString *)substringFromIndex:(NSUInteger)from;
從指定位置from開始(包括指定位置的字元)到尾部
NSString *str1 = @"fakjhfakjnfjahfj";
str1 = [str1 substringFromIndex:7];
NSLog(@"str = %@", str1); //kjnfjahfj
(NSString *)substringToIndex:(NSUInteger)to;
從字元串的開頭一直截取到指定的位置to,但不包括該位置的字元
NSString *str2 = @"fakjhfakjnfjahfj";
str2 = [str2 substringToIndex:10];
NSLog(@"str = %@", str2); //fakjhfakjn
(NSString *)substringWithRange:(NSRange)range;
按照所給出的NSRange從字元串中截取子串
NSString *str = @"<head>quanzi</head>";
NSRange range;
range.location = [str rangeOfString:@">"].location + 1;
range.length = [str rangeOfString:@"</"].location - range.location;
NSString *res = [str substringWithRange:range];
NSLog(@"res = %@", res);//quanzi
字元串替換
(NSString)stringByReplacingOccurrencesOfString:(NSString )target withString:(NSString *)replacement;
用replacement替換target
NSString *str1 = @"http:**quanzi.com*img*info";
NSString *newStr1 = [str1 stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"newStr = %@", newStr1);//newStr1 = http://quanzi.com/img/info
(NSString )stringByTrimmingCharactersInSet:(NSCharacterSet )set;
去除首尾
NSString *str2 = @" http://quanzi.com/img/info ";
NSString *newStr2 = [str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"newStr =|%@|", newStr2);//newStr2 =|http://quanzi.com/img/info|
NSString *str3 = @"***http://quanzi.com/img/info***";
NSString *newStr3 = [str3 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];
NSLog(@"newStr =%@", newStr3);//newStr =http://quanzi.com/img/info
字元串轉換
轉為基本資料類型
(double)doubleValue;
(float)floatValue;
(int)intValue;
NSString *str1 = @"110";
NSString *str2 = @"10";
int res = str1.intValue + str2.intValue;
NSLog(@"res = %i", res); //120
NSString *str1 = @"110";
NSString *str2 = @"10.1";
double res = str1.doubleValue + str2.doubleValue;
NSLog(@"res = %f", res);//120.100000
轉為C語言中的字元串
(char *)UTF8String;
NSString *str = @"abc";
const char *cStr = [str UTF8String];
NSLog(@"cStr = %s", cStr);//cStr = abc
char *cStr = "abc";
NSString *str = [NSString stringWithUTF8String:cStr];
NSLog(@"str = %@", str);//str = abc
字元串大小寫轉換
全部字元轉為大寫字母
(NSString *)uppercaseString;
NSString *str = @"abc";
str = [str uppercaseString];
NSLog(@"str = %@", str);//str = ABC
全部字元轉為小寫字母
(NSString *)lowercaseString
NSString *str = @"ABC";
str = [str lowercaseString];
NSLog(@"str = %@", str);//str = abc
首字母變大寫,其他字母都變小寫
(NSString *)capitalizedString
NSString *str = @"abdcfaDFJdfjh";
str = [str capitalizedString];
NSLog(@"str = %@", str);//str = Abdcfadfjdfjh
字元串路徑的判斷
(BOOL)isAbsolutePath;
是否為絕對路徑
//其實就是判斷是否以/開頭//
NSString *str = @"Users/quanzi/Desktop/info.txt";//不是絕對路徑
if ([str isAbsolutePath]) {
NSLog(@"是絕對路徑");
}else
{
NSLog(@"不是絕對路徑");
}
(NSString *)lastPathComponent;
獲得最後一個目錄
// 截取最後一個/後面的内容
NSString *str = @"/Users/quanzi/Desktop/info.txt";
NSString *component = [str lastPathComponent];
NSLog(@"component = %@", component);//info.txt
(NSString *)stringByDeletingLastPathComponent;
删除最後一個目錄
// 其實就是上次最後一個/和之後的内容
NSString *str = @"/Users/quanzi/Desktop/info.txt";
NSString *newStr = [str stringByDeletingLastPathComponent];
NSLog(@"newStr = %@", newStr);///Users/quanzi/Desktop
(NSString )stringByAppendingPathComponent:(NSString )str;
在路徑的後面拼接一個目錄 (也可以使用stringByAppendingString:或者stringByAppendingFormat:拼接字元串内容)
// 其實就是在最後面加上/和要拼接得内容
// 注意會判斷後面有沒有/有就不添加了, 沒有就添加, 并且如果有多個會替換為1個
NSString *str = @"/Users/quanzi/Desktop/";
NSString *newStr = [str stringByAppendingPathComponent:@"info.txt"];
NSLog(@"newStr = %@", newStr);///Users/quanzi/Desktop/info.txt
(NSString *)pathExtension;
獲得拓展名
// 其實就是從最後面開始截取.之後的内容
NSString *str = @"info.txt";
NSString *str = @"abc.info.txt";
NSString *extension = [str pathExtension];
NSLog(@"extension = %@", extension);//extension = txt
(NSString *)stringByDeletingPathExtension;
删除尾部的拓展名
NSString *str = @"abc.info.txt";
NSString *newStr = [str stringByDeletingPathExtension];
NSLog(@"newStr = %@", newStr);//newStr = abc.lnj
(NSString )stringByAppendingPathExtension:(NSString )str;
在尾部添加一個拓展名
// 其實就是在最後面拼接上.和指定的内容
NSString *str = @"info";
NSString *newStr = [str stringByAppendingPathExtension:@"gif"];
NSLog(@"newStr = %@", newStr);//newStr = info.gif
本文都是個人的一部分總結,以後都會不斷的更新和總結.有什麼寫的不對的地方,歡迎大神指導.