你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Objective-C中字符串NSString的經常使用操作辦法總結

Objective-C中字符串NSString的經常使用操作辦法總結

編輯:IOS開發綜合

1.創立字符串

(1)常量字符串

NSString *string = @"i am an IOSDevTip!";

(2)經常使用創立辦法

NSString *string = [[NSString alloc] init];

string = @"i am an IOSDevTip too!";

(3)用initWithString創立字符串

NSString *string = [[NSString alloc] initWithString:@"IOSDevTip is here!"];

2.格局化創立字符串

(1)int格局化字符串

int age = 20;
NSString *personAge = [NSString stringWithFormat:@"this person age is %d",age];

既然int格局化字符串,那末float、double等,也能夠格局化字符串。

(2)NSString格局化字符串

NSString *name = @"iOSDevTip";
NSString *personName = [NSString stringWithFormat:@"this person name is %@",name];

3.字符串比擬

(1)isEqualToString辦法比擬

NSString *stingOne = @"This is an iOSDevTip!";
NSString *stringTwo = @"This is an iOSDevTip!";
BOOL result = [stingOne isEqualToString:stringTwo];

(2)compare辦法比擬

BOOL result = [stingOne compare:stringTwo] == NSOrderedSame;

compare:辦法前往值類型為NSComparisonResult。而NSComparisonResult有上面幾個列舉值。

typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};

4.字符串年夜小寫轉換

(1)小寫轉年夜寫

 NSString *string = @"This is an iOSDevTip!";
[string lowercaseString];

(2)年夜寫轉小寫

NSString *string = @"This is an iOSDevTip!";
[string uppercaseString];

(3)首字母年夜寫

NSString *string = @"ligang";
NSLog(@"string: %@",[string capitalizedString]);

打印:

2015-07-16 23:06:11.652 iOSStrongDemo[10279:3062010] string: Ligang

5.截取字符串

(1)substringToIndex截取字符串

NSString *string = @"This is a operation string!";
NSString *subToString = [string substringToIndex:6];

(2)截取的subToString為This i

substringFromIndex截取字符串

NSString *subFromString = [string substringFromIndex:6];

(3)截取的subFromString為s a operation string!

substringWithRange截取字符串

NSString *rangeString = [string substringWithRange:NSMakeRange(6, 3)];

截取的rangeString為s a!

6.斷定字符串能否包括另外一個字符串

(1)rangeOfString斷定

NSString *string1 = @"This is a iOSDevTip";
NSString *string2 = @"iOSDevTip";
NSRange range = [string1 rangeOfString:string2];
NSInteger location = range.location;
NSInteger leight = range.length;
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%ld,Leight:%ld",location,leight]];
NSLog(@"logString:%@",logString);

打印出來:

iOSStrongDemo[8837:2221170] logString:Location:10,Leight:9

(2)斷定能否包括前後綴

NSString *string = @"I love China";
BOOL isHasI = [string hasprefix:@"I"];
BOOL isHasChina = [string hasSuffix:@"China"];

7.朋分字符串

NSString *string = @"This is a iOSDevTip";
NSArray *array = [string componentsSeparatedByString:@"a"];
NSString *string1 = [array objectAtIndex:0];
NSString *string2 = [array objectAtIndex:1];
NSLog(@"string1:%@  string2:%@",string1,string2);

打印:

2015-07-16 22:40:39.559 iOSStrongDemo[10165:3055448] string1:This is  string2: iOSDevTip

8.拔出字符串

NSMutableString *string = [[NSMutableString alloc] initWithString:@"I China"];
[string insertString:@"Love " atIndex:2];
NSLog(@"string: %@",string);

打印:
2015-07-16 22:44:10.706 iOSStrongDemo[10206:3057014] string: I Love China
(1)追加字符串

NSMutableString *string = [[NSMutableString alloc] initWithString:@"I Love "];
[string appendString:@"China"];
NSLog(@"string:%@",string);

打印:

2015-07-16 22:42:32.305 iOSStrongDemo[10189:3056410] string:I Love China

9.刪除字符串

NSMutableString *string = [[NSMutableString alloc] initWithString:@"I love China"];
[string deleteCharactersInRange:NSMakeRange(2, 4)];
NSLog(@"String1: %@",string);

打印:

2015-07-16 22:46:58.437 iOSStrongDemo[10219:3057749] String1: I China

10.調換字符串

NSString *string = @"I love China";
NSString *replaceString = [string stringByReplacingOccurrencesOfString:@"love" withString:@"like"];
NSLog(@"replaceString:  %@",replaceString);

打印:

2015-07-16 22:56:07.405 iOSStrongDemo[10236:3059503] replaceString: I like China

11.去除字符串首尾的空格和換行符

NSString *string = @" I love China ";
NSString *text = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"text:%@",text);

打印:

2015-07-16 23:00:47.845 iOSStrongDemo[10265:3061013] text:I love China

【Objective-C中字符串NSString的經常使用操作辦法總結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved