關於字符串的各類操做,總結一下以便之後複習查找。api
內容簡要:
app
1、建立常量字符串。 2、建立空字符串,給予賦值。3、在以上方法中,提高速度:initWithString方法函數
4、用標準c建立字符串:initWithCString方法。5、建立格式化字符串:佔位符(由一個%加一個字符組成)this
6、建立臨時字符串。七、判斷字符串爲空。9、是否以」test」開頭;是否以」.move」結尾。十、比較兩個字符串。atom
十一、聲明一個可變字符;長度是40個字符。十二、修改可變字符;先聲明一個可變字符 myFriend;長度30。url
13、在一個字符串後面附加一個新的字符串。14、字符串轉換整數值。spa
1五、從文件讀取字符串:initWithContentsOfFile方法。16、寫字符串到文件:writeToFile方法.net
17、改變字符串的大小寫。1八、在串中搜索子串。19、抽取子串。20、擴展路徑。21、文件擴展名。orm
22、在已有字符串後面添加字符。23、在已有字符串中按照所給出範圍和長度刪除字符。對象
2四、在已有字符串後面在所指定的位置中插入給出的字符串。25、將已有的空符串換成其它的字符串。
2六、按照所給出的範圍,和字符串替換的原有的字符。2七、判斷字符串內是否還包含別的字符串(前綴,後綴)。
原文地址: http://blog.csdn.net/dingkun520wy/article/details/7010270
-----------------------------------------------------------------------------------------------------------------------------------------------------
1、建立常量字符串。
NSString *astring = @"This is a String!";
2、建立空字符串,給予賦值。
NSString *astring = [[NSString alloc] init];
astring = @"This is a String!";
NSLog(@"astring:%@",astring);
[astring release];
3、在以上方法中,提高速度:initWithString方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
[astring release];
4、用標準c建立字符串:initWithCString方法
char *Cstring = "This is a String!";
NSString *astring = [[NSString alloc] initWithCString:Cstring];
NSLog(@"astring:%@",astring);
[astring release];
5、建立格式化字符串:佔位符(由一個%加一個字符組成)
int i = 1;
int j = 2;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
NSLog(@"astring:%@",astring);
[astring release];
6、建立臨時字符串
NSString *astring;
astring = [NSString stringWithCString:"This is a temporary string"];
NSLog(@"astring:%@",astring);
七、判斷字符串爲空
NSString *urlString = [urlInput stringValue];
if (!urlString) {
NSLog( @」NO INPUT.」 );
}
if ([urlString length] == 0 ) {
NSLog( @」NO INPUT.」 );
}
9、是否以」test」開頭;是否以」.move」結尾;
NSString *fileName = @」test.move」;
if ([fileName hasPrefix:@"test"]) {
NSLog(@」has Test String !」);
}else{
NSLog(@」don’t have Test」);
}
[fileName hasSuffix:@".move"]?NSLog(@」Yes it got a .Mov in its end」):NSLog(@」no it has no .mov string」);
十、比較兩個字符串:
strcmp函數
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0)
{
NSLog(@"1");
}
isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
compare方法(comparer返回的三種值)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
NSOrderedSame 判斷二者內容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;
NSLog(@"result:%d",result);
NSOrderedAscending 判斷兩對象值的大小(按字母順序進行比較,astring02大於astring01爲真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
NSLog(@"result:%d",result);
NSOrderedDescending 判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01爲真)
不考慮大 小寫比較字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小於astring01爲真)
不考慮大小寫比較字符串2
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
NSLog(@"result:%d",result);
NSCaseInsensitiveSearch:不區分大小寫比較 NSLiteralSearch:進行徹底比較,區分大小寫NSNumericSearch:比較字符串的字符個數,而不是字符值。
十一、聲明一個可變字符;長度是40個字符;
NSMutableString *myMutableString;
myMutableString = [NSMutableString stringWithCapacity:40];
NSString *myName = @」Leo」;
[myMutableString appendString:@"Hello ,there"];
[myMutableString appendFormat:@" i am %@",myName];
NSLog(@」this is NSMutableString: %@」,myMutableString);
//this is NSMutableString: Hello ,there i am Leo;
十二、修改可變字符;先聲明一個可變字符 myFriend;長度30;
NSMutableString *myGirlFriend;
myGirlFriend = [NSMutableString stringWithCapacity:30];
//而後給字符加入一些內容;
[myGirlFriend appendString:@"Here are my GF:Carol Sophia Ashley Helen and Yoyo"];
NSLog(@」%@」,myGirlFriend);
//聲名一個變更範圍(NSRange);
NSRange joneRange;
joneRange = [myGirlFriend rangeOfString:@"Helen "];
//下面:就是從myFriend字符中配對,若是有相等的內容就刪除了;
[myGirlFriend deleteCharactersInRange:joneRange];
NSLog(@」%@」,myGirlFriend);
13、在一個字符串後面附加一個新的字符串
NSString *a = @"a";
NSString *b = [a stringByAppendingString:@"b"];//b變量的值爲「ab」
14、字符串轉換整數值
NSString *age = @"36";
if([age intValue]>35){
}
1五、從文件讀取字符串:initWithContentsOfFile方法
NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
16、寫字符串到文件:writeToFile方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";
[astring writeToFile: path atomically: YES];
[astring release];
17、改變字符串的大小寫
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大寫
NSLog(@"string2:%@",[string2 lowercaseString]);//小寫
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
1八、在串中搜索子串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
19、抽取子串
//-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);
20、擴展路徑
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
21、文件擴展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
22、在已有字符串後面添加字符
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding some character"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
23、在已有字符串中按照所給出範圍和長度刪除字符
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"String1:%@",String1);
2四、在已有字符串後面在所指定的位置中插入給出的字符串
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
25、將已有的空符串換成其它的字符串
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
2六、按照所給出的範圍,和字符串替換的原有的字符
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
NSLog(@"String1:%@",String1);
2七、判斷字符串內是否還包含別的字符串(前綴,後綴)
//01:檢查字符串是否以另外一個字符串開頭- (BOOL) hasPrefix: (NSString *) aString;
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,這一點前面在串中搜索子串用到過;
-----------------------------------------------------------------------------------------------------------------------------------------------------