一、NSString的用法api
//一、建立常量字符串。 NSString *string1 = @"這是一個NSString對象"; //二、建立空字符串,給予賦值。實例化一個對象 NSString *string2 = [[NSString alloc] init]; NSString *string = [NSString string]; //三、綜合一、2方法,提高性能:initWithString方法 NSString *string3 = [[NSString alloc] initWithString:@"This is a string3"]; NSString *string = [NSString stringWithString:@"建立一個字符串"]; // 用這種方法xcode會讓你修改成建立一個字符串常量的結構 //四、用標準C語言字符串建立OC字符串:initWithCString方法(注意編碼方式參數的選擇) char *string4c = "This is a C String!中文也支持"; NSString *string4 = [[NSString alloc] initWithCString:string4c encoding:NSUTF8StringEncoding]; // 這種建立方式在從網上獲取數據出現「亂碼」時,進行轉碼的時候將會用到。這時,string4c指明編碼類型(NSUTF8StringEncoding) //五、建立格式化字符串:佔位符(由一個%加一個字符組成) int i = 1; int j = 2; NSString *string5 = [[NSString alloc] initWithFormat:@"這裏輸入你想要輸入的內容%d",i]; //除了int,float,其餘數據類型也能夠
從文件讀取字符串:initWithContentsOfFile方法 xcode
NSString *path6 = @"string8.text"; //@裏出現的是想要訪問的文件的路徑 app
NSString *string6 = [[NSString alloc] initWithContentsOfFile:path6 encoding:NSUTF8StringEncoding error:nil]; 函數
寫字符串到文件:writeToFile方法 性能
NSString *string7 = [[NSString alloc] initWithString:@"This is a string7!"]; this
NSString *path7 = @"string7.text"; 編碼
[string7 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; atom
用C語言庫函數實現比較:strcmp函數 spa
char stringc1[] = "string!",stringc2[] = "string!"; code
if(strcmp(stringc1, stringc2) == 0)
NSLog(@"yes");
oc中使用NSString:isEqualToString方法
NSString *astringoc1 = @"This is a String!";
NSString *astringoc2 = @"This is a String!";
BOOL result = [astringoc1 isEqualToString:astringoc2];
NSLog(@"result:%d",result);
compare方法(comparer返回的三種值)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result1 = [astring01 compare:astring02] == NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedSame 判斷二者內容是否相同
BOOL result2 = [astring01 compare:astring02]==NSOrderedAscending;
NSLog(@"result:%d",result2);
//NSOrderedAscending 判斷兩對象值的大小(按字母順序進行比較,astring02大於astring01爲真)
BOOL result3 = [astring01 compare:astring02] == NSOrderedDescending;
NSLog(@"result:%d",result3);
//NSOrderedDescending 判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01爲真)
不考慮大小寫比較字符串(caseInsensitiveCompare)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame;
NSLog(@"result:%d",result);
(NSCaseInsensitiveSearch:不區分大小寫比較 NSLiteralSearch:進行徹底比較,區分大小寫 NSNumericSearch:比較字符串的字符個數,而不是字符值。)
改變字符串的大小寫( uppercaseString,lowercaseString,capitalizedString)
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大寫
NSLog(@"string2:%@",[string2 lowercaseString]);//小寫
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大寫
在串中搜索子串(rangeOfString)
NSString *string1 = @"This is a string string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
NSInteger location = range.location;
NSInteger leight = range.length;
//range 中包含了 location (出現的位置) 和length (長度)的屬性
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%ld,Leight:%ld",location,leight]];
NSLog(@"astring:%@",astring);
抽取子串 (substringToIndex,substringFromIndex)
//-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)];
NSLog(@"string2:%@",string2);
擴展路徑
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
文件擴展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
二、NSMutableString 的用法
給字符串分配容量( stringWithCapacity)
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];
在已有字符串後面添加字符(appendString,appendFormat)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 appendString:@", I will be adding some character"];
在已有字符串中按照所給出範圍和長度刪除字符 (deleteCharactersInRange)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
在已有字符串後面在所指定的位置中插入給出的字符串(-insertString: atIndex)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
將已有的非空符串換成其它的字符串(-setString:)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
按照所給出的範圍,和字符串替換的原有的字符(replaceCharactersInRange)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
判斷字符串內是否還包含別的字符串(前綴,後綴)
//01: 檢查字符串是否以另外一個字符串開頭- (BOOL) hasPrefix: (NSString *) aString,hasSuffix以什麼字符串結尾
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] == 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] == 1 ? NSLog(@"YES") : NSLog(@"NO");
//02: 查找字符串某處是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,這一點前面在串中搜索子串用到過;