iOS開發之NSString的幾條實用技巧

2021-07-04 08:28:01 字數 4977 閱讀 4112

常量字串

nsstring *string = @"i am an iosdevtip!";
常用建立方法

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

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

用initwithstring建立字串

nsstring *string = [[nsstring alloc] initwithstring:@"iosdevtip is here!"];
int格式化字串

int age = 20;

nsstring *personage = [nsstring stringwithformat:@"this person age is %d",age];

既然int格式化字串,那麼float、double等,也可以格式化字串。

nsstring格式化字串

nsstring *name = @"iosdevtip";

nsstring *personname = [nsstring stringwithformat:@"this person name is %@",name];

isequaltostring方法比較

//比較字串

nsstring *stingone = @"this is an iosdevtip!";

nsstring *stringtwo = @"this is an iosdevtip!";

bool result = [stingone isequaltostring:stringtwo];

compare方法比較

bool result = [stingone compare:stringtwo] == nsorderedsame;
compare:方法返回值型別為nscomparisonresult。而nscomparisonresult有下面幾個列舉值。

typedef ns_enum(nsinteger, nscomparisonresult) ;
小寫轉大寫

nsstring *string = @"this is an iosdevtip!";

[string lowercasestring];

大寫轉小寫

nsstring *string = @"this is an iosdevtip!";

[string uppercasestring];

substringtoindex擷取字串

nsstring *string = @"this is a operation string!";

nsstring *subtostring = [string substringtoindex:6];

擷取的subtostring為this i

substringfromindex擷取字串

nsstring *subfromstring = [string substringfromindex:6];
擷取的subfromstring為s a operation string!

substringwithrange擷取字串

nsstring *rangestring = [string substringwithrange:nsmakerange(6, 3)];
擷取的rangestring為s a!

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
如果leight為0,說明不包含。還有更多關於nsstring的用法,大家一起探索吧。

**:

//首字母大寫

nsstring *string = @"ligang";

nslog(@"string: %@",[string capitalizedstring]);

列印:

2015-07-16 23:06:11.652 iosstrongdemo[10279:3062010] string: ligang
**:

//分割字串

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
**:

//追加字串

nsmutablestring *string = [[nsmutablestring alloc] initwithstring:@"i love "];

nslog(@"string:%@",string);

列印:

2015-07-16 22:42:32.305 iosstrongdemo[10189:3056410] string:i love china
**:

//插入字串

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
**:

//刪除字串

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
**:

//判斷是否包含前字尾

nsstring *string = @"i love china";

bool ishasi = [string hasprefix:@"i"];

bool ishaschina = [string hassuffix:@"china"];

**:

//替換字串

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
**:

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

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

IOS開發之NSString的使用大全

nsstring 不可變的字串 nsmutablestring 可變的字串下面是nsstring的使用 char s hello objective c c nsstring str hello oc oc,c字串型別轉換c oc nsstring str1 nsstring stringwithu...

iOS開發 判斷NSString是否包含某個字串

主要用到三種方法來判斷 rangeofstring 是否包含 hasprefix 是否在字首包含 hassuffix 是否在末尾包含 判斷字元是否包含某字串 nsstring string hello,shenzhen,martin 字條串是否包含有某字串 if string rangeofstri...

ios中NSString的bool型別

原文如下 eg nslog hello,objective c 表示應該當作nsstring字串來處理。nslog相當於c語言中的printf,常用於文字輸出 nslog輸出整合時間戳,日期戳,自動換行的功能。字首ns表示老喬被蘋果炒魷魚後建的公司next step,後被蘋果收買。輸出格式 物件 d...