網上寫的關於字符串常用方法的博客很多,這里我簡單做了下總結!不喜勿噴哦!
一.創建字符串
#import <Foundation/Foundation.h>
//NSString
//創建不可變字符串對象的類
//NSMutableString
//創建可變字符串對象的類
//OC語言完全兼容C語言
//OC字符串與C語言字符串區別
//1.OC字符串是一個字符串對象,字符串常量需要用@""包含
//2.C語言字符串用""包含
//3.C語言的字符串以字符的ASCII碼形式存儲
//4.OC中的字符串以uicode編碼(萬國碼)形式存儲
//UTF-8(多字節編碼)
//5.打印OC字符串用%@,打印C語言字符串用%s
int main(int argc,constchar * argv[]) {
@autoreleasepool {
NSString *str1 =@"hello world";//@"hello world"是一個常量字符串對象,存儲常量區,不可以被修改
NSLog(@"str1 = %@", str1);
//格式化創建字符串對象
//- (instancetype)initWithFormat:(NSString *)format, ...
NSString *str2 = [[NSString alloc] initWithFormat:@"%s%d%@","hello",123,@"world" ];
NSLog(@"str2 = %@", str2);
//用格式化的類方法創建字符串對象
//+ (instancetype)stringWithFormat:(NSString *)format, ...
NSString *str3 = [NSString stringWithFormat:@"%s%d%@","qiafdn",456,@"ffds"];
NSLog(@"str3 = %@", str3);
//用給定的字符串對象創建字符串對象
NSString *str4=@"中國教育";
NSString *str5 = [[NSString alloc] initWithString:str4];
NSLog(@"str5 = %@", str5);
//用C的字符串創建OC的字符串對象
NSString *str6 = [[NSString alloc] initWithUTF8String:"qifdfdg中國jiaoyu"];
NSLog(@"str6 = %@", str6);
NSString *str7 = [[NSString alloc] initWithCString:"我的qifdfg" encoding:NSUTF8StringEncoding];
NSLog(@"str7 = %@", str7);
//創建一個空的字符串對象 @""
NSString *str8 = [NSString string];
NSLog(@"str8 = %@", str8);
NSString *str9 = [[NSString alloc] init];
NSLog(@"str9 = %@", str9);
//跟initWithString相對應
NSString *str10 = [NSString stringWithString:str7];
NSLog(@"str10 = %@", str10);
//跟initWithUTF8String相對應
NSString *str11 = [NSString stringWithUTF8String:"hello world中國"];
NSLog(@"str11 = %@", str11);
//跟initWithCString相對應
NSString *str12 = [NSString stringWithCString:"zhongguo" encoding:NSUTF8StringEncoding];
NSLog(@"str12 = %@", str12);
}
return0;
}
二.NSString的常用方法
#import <Foundation/Foundation.h>
//NSString
int main(int argc,constchar * argv[]) {
@autoreleasepool {
NSString *str1 =@"hello world中國";
//求字符串長度
NSUInteger len = [str1 length];
NSLog(@"len = %li", len);
//獲取字符串指定位置的字符
unichar ch = [str1 characterAtIndex:13];
NSLog(@"ch = %C", ch);//%C打印unichar字符 %c打印ASCII字符
//字符串提取
//從傳入下標位置提取子串到字符串結束
NSString *subStr1 = [str1 substringFromIndex:4];
NSLog(@"subStr1 = %@", subStr1);
//提取子串到指定位置(不包含下標位置字符)
NSString *subStr2 = [str1 substringToIndex:7];
NSLog(@"subStr2 = %@",subStr2);
//提取指定范圍內的字符串
NSRange range = {6,5};
NSString *subStr3 = [str1 substringWithRange:range];
NSLog(@"subStr3 = %@", subStr3);
//NSMakeRange();//構建NSRange變量
NSString *subStr4 = [str1 substringWithRange:NSMakeRange(2,6)];
NSLog(@"subStr4 = %@", subStr4);
//字符串比較
NSString *str2 = [NSString stringWithCString:"hallo world中國" encoding:NSUTF8StringEncoding];
NSString *str3 = [NSString stringWithUTF8String:"hello world中國"];
NSComparisonResult result = [str2 compare:str3];
if (result == NSOrderedAscending) {//遞增
NSLog(@"str2 < str3");
}
elseif(result == NSOrderedDescending)//遞減
{
NSLog(@"str2 > str3");
}
else
{
NSLog(@"str2 == str3");
}
//以大小寫不敏感方式比較字符串
//[str2 caseInsensitiveCompare:str3];
//判斷兩個字符串是否相等
//- (BOOL)isEqualToString:(NSString *)aString;
BOOL ret = [str2 isEqualTo:str3];
if (ret==YES) {
NSLog(@"str2 == str3");
}
else
{
NSLog(@"str2 != str3");
}
//判斷前綴子串
//- (BOOL)hasPrefix:(NSString *)aString;
BOOL ret1 = [@"www.baidu.com" hasPrefix:@"www."];
NSLog(@"ret1 = %d", ret1);
//判斷后綴子串
//- (BOOL)hasSuffix:(NSString *)aString;
BOOL ret2 = [@"www.hao123.com" hasSuffix:@"com"];
NSLog(@"ret2 = %d", ret2);
//判斷是否包含子串(10.10macos)
BOOL ret3 = [@"hao123" containsString:@"hao"];
NSLog(@"ret3 = %d", ret3);
//查找子串
NSString *str4 = [[NSString alloc] initWithFormat:@"%s","hello world qidfafddnworldfedffsng"];
NSRange range1 =[str4 rangeOfString:@"world"];
if (range1.location == NSNotFound) {//不能查找對應的子串,返回long類型最大值
NSLog(@"沒有查找到字串 notfound = %lu", NSNotFound);
}
else
{
NSLog(@"location = %lu length = %lu", range1.location, range1.length);
}
//倒序查找子串
NSRange range2 = [str4 rangeOfString:@"world" options:NSBackwardsSearch];
NSLog(@"location = %li length = %li", range2.location, range2.length);
//字符串追加
//並不是直接在原字符串的末尾追加字符串,而是利用傳入的字符串及原字符串創建一個新的字符串
NSString *str5 =@"hello";
NSLog(@"%p", str5);
str5 = [str5 stringByAppendingString:@"world"];
NSLog(@"str5 = %@", str5);
NSLog(@"%p", str5);
//格式化追加字符串
NSString *str6 =@"qfdfdng";
str6 = [str6 stringByAppendingFormat:@"%d%s",123,"helloworld"];
NSLog(@"str6 = %@", str6);
//把字符換串對象轉換成整型浮點型
int a = [@"12345" intValue];
float f = [@"3.14" floatValue];
NSLog(@"a = %d f = %.2f", a, f);
//返回公共前綴子串
NSString *str7 = [@"www.baidu.com" commonPrefixWithString:@"www.hao123.com"options:NSLiteralSearch];
NSLog(@"str7 = %@", str7);
//大小寫轉換
//把小寫字母轉換成大寫字母
NSString *str8 = [@"baidu中國" uppercaseString];
NSLog(@"str8 = %@", str8);
//把大寫字母轉換成小寫字母
NSString *str9 = [@"BaiDU" lowercaseString];
NSLog(@"str9 = %@", str9);
//把每個單詞的首字母大寫
NSString *str10= [@"bai du qian feng" capitalizedString];
NSLog(@"str10 = %@", str10);
//字符串替換
//- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
NSString *str11=@"hello world qiafdfnfdg hello world hello hell qdfdfnfdg";
str11 = [str11 stringByReplacingOccurrencesOfString:@"hello" withString:@"welcome"];
NSLog(@"str11 = %@", str11);
//替換指定范圍內的字符
//- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement
NSString *str12 =@"hello world qianfeng";
str12 = [str12 stringByReplacingCharactersInRange:NSMakeRange(12,8) withString:@"welcome"];
NSLog(@"str12 = %@", str12);
//把OC的字符串對象轉換成C字符串
NSLog(@"%s", [@"hello world" UTF8String]);
//用網址的內容生成OC字符串對像
//- (instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
//+ (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
NSURL *url= [[NSURL alloc] initWithString:@"http://www.baidu.com"];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];//nil空指針
NSLog(@"urlContent = %@", urlContent);
//用文件的內容生成字符串
//- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
//+ (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
NSString *fileContent = [NSString stringWithContentsOfFile:@"/Users/zhangxueming/Desktop/json.txt"encoding:NSUTF8StringEncoding error:nil];
NSLog(@"fileContent = %@", fileContent);
}
return0;
}
三.NSSMUtableString的常用方法
#import <Foundation/Foundation.h>
//NSMutableString 繼承與NSString
//所有NSString類的方法NSMutableString都可以使用
int main(int argc,constchar * argv[]) {
@autoreleasepool {
//創建指定容量大小的可變字符串對象
//+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity;
NSMutableString *mulStr1 = [[NSMutableString alloc] initWithCapacity:20];
NSLog(@"mulStr1 = %@", mulStr1);
//替換指定范圍內的字符
//- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
NSMutableString *mulStr2 = [[NSMutableString alloc] initWithString:@"hello world qianfdfsfefdg"];
[mulStr2 replaceCharactersInRange:NSMakeRange(6,5) withString:@"welcome"];
NSLog(@"mulStr2 = %@", mulStr2);
//在指定位置增加字符串
NSMutableString *mulStr3 = [[NSMutableString alloc] initWithFormat:@"夢想中國"];
[mulStr3 insertString:@"hello world" atIndex:2];
NSLog(@"mulStr3 = %@", mulStr3);
//刪除指定范圍內的字符
NSMutableString *mulStr4 = [NSMutableString stringWithUTF8String:"熱愛hello world中國"];
[mulStr4 deleteCharactersInRange:NSMakeRange(2,11)];
NSLog(@"mulStr4 = %@", mulStr4);
//追加字符串
NSMutableString *mulStr5 = [NSMutableString stringWithString:@"helloworld"];
[mulStr5 appendString:@"qidffdf"];
NSLog(@"mulStr5 = %@", mulStr5);
//格式化追加字符串
NSMutableString *mulStr6 = [NSMutableString stringWithFormat:@"%s%d","hello",12345];
[mulStr6 appendFormat:@"%.2f%@",3.14,@"world"];
NSLog(@"mulStr6 = %@", mulStr6);
//修改字符串
NSMutableString *mulStr7 = [[NSMutableString alloc] initWithString:@"hello world"];
[mulStr7 setString:@"qifdfdng"];
NSLog(@"mulStr7 = %@", mulStr7);
}
return0;
}
