- 創(chuàng)建常量字符串
- 創(chuàng)建空字符串
- initWithString方法
- 用標準C創(chuàng)建字符串
- 創(chuàng)建格式化字符串
- 創(chuàng)建臨時字符串
- 從文件創(chuàng)建字符串
- 用字符串創(chuàng)建字符串何乎,并寫入到文件
- 用C比較:strcmp函數
- isEqualToString方法
- compare方法
- 不考慮大小寫比較字符串
- 輸出大寫或者小寫字符串
- rangeOfString:方法
- substringToIndex:方法
- substringFromIndex:
- substringWithRange:
- stringWithCapacity:
- appendString:和appendFormat:
- insertString:atIndex:方法
- setString: 方法
- replaceCharactersInRange: withString:
- hasPrefix: 和hasSuffix:
1拔疚、從文件創(chuàng)建字符串
創(chuàng)建常量字符串
NSString*aString = @”This is a String”;
NSLog(@”string = %@”,aString);
打恿圮健:sring =ThisisaString
創(chuàng)建空字符串领曼,給予賦值
NSString *aString = [[NSString alloc] init];
aString = @”This is a String”;
NSLog(@”string = %@”,aString);
打幽ㄊ!:sring =ThisisaString
initWithString方法
作用:相對上面的方法可提升速度
NSString *astring = [[NSString alloc] initWithString:@"This is aString!"];
用標準C創(chuàng)建字符串
方法:initWithCString
char *cString = ”This is a String”;
NSString *astring = [[NSString alloc] initWithCString:Cstring];
NSLog(@”string = %@”,aString);
打铀颈А:sring =ThisisaString
創(chuàng)建格式化字符串
占位符:由一個%加一個字符組成蓖康。
inti = 1;
intj = 2;
NSString *aString= [[NSString alloc] initWithString:[NSString stringWithFormat:@“i is %d, j is %d”,i,j]];
NSLog(@”string = %@”,aString);
打宇盱拧:string=i is 1, j is 2
創(chuàng)建臨時字符串
NSString *aString;
aString= [NSString stringWithCString:“Thisis a temporary string”];
NSLog(@”aString = %@”,aString);
打印:aString = This is a temporary string
從文件創(chuàng)建字符串
獲取文件路徑
NSString*path = [[NSBundle mainBundle] pathForResource:@“aString.text”ofType:nil];
NSString*aString= [[NSString alloc] initWithContentsOfFile:path];
2蒜焊、用字符串創(chuàng)建字符串倒信,并寫入到文件
用字符串創(chuàng)建字符串
NSString*aString = [[NSString alloc] initWithString:@“Thisis a String!”];
保存到的文件
NSString*path = @“astring.text”;
寫入文件
[aString writeToFile: path atomically: YES];
3、用C比較:strcmp函數
char string1[] = “string!”;
char string2[] =“string!”;
if(strcmp(string1, string2) == 0) {
NSLog(@“1”);
}
說明:strcmp()用來比較字符串(區(qū)分大小寫)泳梆,其原型為: intstrcmp(constchar *s1,constchar *s2);
4鳖悠、isEqualToString方法
比較兩個字符串是否相等唆迁,返回布爾類型BOOL
NSString *aString01= @“This is a String!”;
NSString *aString02= @“This is a String!”;
BOOL result = [aString01 isEqualToString:aString02];
5、compare方法
comparer返回的三種值:
NSOrderedSame判斷兩者內容是否相同
NSOrderedAscending判斷兩對象值的大小(按字母升序進行比較竞穷,aString02大于aString01為真)
NSOrderedDescending判斷兩對象值的大小(按字母將序進行比較唐责,aString02小于aString01為真)
NSOrderedSame判斷兩者內容是否相同
NSString *aString01= @“This is a String!”;
NSString*aString02= @“This is a String!”;
BOOL result = [aString01 compare:aString02] ==NSOrderedSame;
if (result){
//為真
NSLog(@”真”);
}
NSOrderedAscending判斷兩對象值的大小(按字母順序進行比較,aString02大于aString01為真)
NSString *aString01= @"This is aString!";
NSString*aString02= @"this is aString!";
BOOL result = [aString01 compare:aString02] ==NSOrderedAscending;
if (result){
//為真
NSLog(@”真”);
}
NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較瘾带,aString02小于aString01為真)
NSString *aString01= @"this is a String!";
NSString *aString02= @"This is aString!";
BOOL result = [aString01 compare:aString02] ==NSOrderedDescending;
if (result){
//為真
NSLog(@”真”);
}
6鼠哥、不考慮大小寫比較字符串
NSString *astring01 = @"this is a String!";
NSString*astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] ==NSOrderedSame;
//NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小于astring01為真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02options:NSCaseInsensitiveSearch |NSNumericSearch] ==NSOrderedSame;
//NSCaseInsensitiveSearch:不區(qū)分大小寫比較NSLiteralSearch:進行完全比較看政,區(qū)分大小寫NSNumericSearch:比較字符串的字符個數朴恳,而不是字符值。
7允蚣、輸出大寫或者小寫字符串
NSString *string1 = @"AString";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大寫
打佑谟薄:string1: A STRING
NSLog(@"string2:%@",[string2 lowercaseString]);//小寫
打印:string2: string
NSLog(@“string2:%@”,[string2 capitalizedString]);//首字母大寫
打尤峦谩:String
#### 8森渐、rangeOfString:方法
查找字符串某處是否包含其它字符串。
NSString string1 =@“This is astring”;
NSStringstring2 =@“string”;
NSRange range = [string1 rangeOfString:string2];
intlocation = range.location;//字符串所在位置
NSString*aString= [[NSString alloc] initWithString:[NSString stringWithFormat:@“Location:%i,Leight:%i”,location,leight]];
NSLog(@”aString:%@",aString);
打用拔:aString:Location:10,Leight:6
#### 9同衣、substringToIndex:方法
從字符串的開頭一直截取到指定的位置,但不包括該位置的字符壶运。
NSString string1 =@“This is astring”;
NSStringstring2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
打幽推搿:string2: Thi
#### 10、substringFromIndex:
以指定位置開始(包括指定位置的字符)蒋情,并包括之后的全部字符埠况。
NSString *string1 =@“This is astring”;
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@“string2:%@”,string2);
打印:string2: s is a string
#### 11棵癣、substringWithRange:
按照所給出的位置辕翰,長度,任意地從字符串中截取子串浙巫。
NSString string1 =@“This is astring”;
NSStringstring2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@“string2:%@”,string2);
#### 12金蜀、stringWithCapacity:
按照固定長度生成空字符串刷后。
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];
這是初始化一個可變的字符串
#### 13的畴、appendString:和appendFormat:
把一個字符串接在另一個字符串的末尾。
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 appendString:@", Yes"];
[String1 appendFormat:[NSString stringWithFormat:@",it is"]];
NSLog(@"String1:%@",String1);
打映⒌ā:String1: :This isaNSMutableString, Yes, it is
#### 14丧裁、insertString:atIndex:方法
在指定位置插入字符串。
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@“This is a NSMutableString”];
[String1 insertString:@“Hi!” atIndex:0];//在第0個位置插入
NSLog(@“String1:%@”,String1);
打雍巍:String1: Hi! This is a NSMutableString
#### 15煎娇、setString: 方法
替換原來的值二庵。
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@“String1:%@”,String1);
打印:String1: Hello Word!
#### 16缓呛、replaceCharactersInRange: withString:
用指定字符串替換字符串中某指定位置催享、長度的字符串。
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@“This is a NSMutableString”];
[String1 replaceCharactersInRange:NSMakeRange(0, 4)withString:@“That”]; //從第0個位置開始長度是4(This)用That來替換
NSLog(@“String1:%@”,String1);
打佑窗怼:String1: That is a NSMutableString
#### 17因妙、hasPrefix: 和hasSuffix:
>檢查字符串是否以另一個字符串開頭和是否以一個字符串結尾。
NSString *String1 =@“NSStringInformation.txt”;
[String1 hasPrefix:@“NSString”] == 1 ? NSLog(@“YES”): NSLog(@“NO”);
打悠彼琛:YES
[String1 hasSuffix:@“.txt”]= = 1 ? NSLog(@“YES”): NSLog(@“NO”);
打优屎:YES
說明:用到的“?:” 是三目運算符