NSString *str1 = @ "Beijing"; NSString *str2 = @ "Beijing"; Convert all to uppercase NSLog (@ "%@", [str1 uppercasestring]); All converted to lowercase NSLog (@ "%@", [str1 lowercasestring]); Initial Capital NSLog (@ "%@", [str1 capitalizedstring]); Compares two string contents whether the same BOOL b =[str1 ISEQUALTOSTRING:STR2]; Two string content comparison//nsorderedascending right > left//nsorderedsame content same//nsordereddescending left > right nscomparisonresult resul t = [str1 COMPARE:STR2]; if (result = = nsorderedascending) {NSLog (@ "right > Left");} else if (result = = Nsorderedsame) {NSLog (@ "content is the same");} else if (result = = nsordereddescending) {NSLog (@ "left > Right");}//Ignore case for comparison, return value as compare as result = [str1 Caseinsensitivec OMPARE:STR2]; if (result = = nsorderedascending) {NSLog (@ "right > Left");} else if (result = = Nsorderedsame) {NSLog (@ "content is the same");} else if (result = = nsordereddescending) {NSLog (@ "left > Right")}//Determines whether the string starts with the specified string [str1 hasprefix:@ "AAA"]; Determines whether the string ends with the specified string [str1 hassuffix:@ "AAA"]; Determines whether the string contains the specified string, returns the position and length nsrange range = [@ "123456" rangeofstring:@ "456"]; NSLog (@ "%@", NSStringFromrange (range)); Reverse Search range = [@ "123456456qweasasd456" rangeofstring:@ "456" options:nsbackwardssearch]; NSLog (@ "%@", Nsstringfromrange (range)); Specify a range to search range = Nsmakerange (0, 9); range = [@ "123456456qweasasd456" rangeofstring:@ "456" Options:nsbackwardssearch Range:range]; NSLog (@ "%@", Nsstringfromrange (range));
[CPP]View Plaincopyprint?
- //intercept of strings
- NSString *str = @"123456789";
- NSLog (@"%@", [str substringfromindex:3]);
- NSLog (@"%@", [str substringtoindex:6]);
- NSLog (@"%@", [str substringwithrange:nsmakerange (3, 3)]);
- //Split the string with the specified string, returning an array
- Nsarray *array = [@"1,2,3,4,5,6" componentsseparatedbystring:@","];
- NSLog (@"%@", array);
- //Combine strings in an array into one file path
- Nsmutablearray *components = [Nsmutablearray array];
- [Components addobject:@"Users"];
- [Components addobject:@"Centralperk"];
- [Components addobject:@"Desktop"];
- NSString *path = [NSString pathwithcomponents:components];
- NSLog (@"%@", Path); //users/centralperk/desktop
- //Divide a path into an array
- Nsarray *array1 = [path pathcomponents];
- NSLog (@"%@", array1);
- //Determine if it is an absolute path (based on: Start with '/')
- Path = @"/users/centralperk/desktop";
- NSLog (@"%i", [path Isabsolutepath]);
- //Get the last directory
- NSLog (@"%@", [path lastpathcomponent]);
- //Delete last directory
- NSLog (@"%@", [path stringbydeletinglastpathcomponent]);
- //Splicing a directory
- NSLog (@"%@", [path stringbyappendingpathcomponent:@"AAA"]); ///users/centralperk/desktop/aaa
- NSLog (@"%@", [path stringbyappendingstring:@"AAA"]); ///users/centralperk/desktopaaa
- NSLog (@"%@", [path stringbyappendingformat:@"%@%@", @"B",@ "C" ]); ///USERS/CENTRALPERK/DESKTOPBC
- //Expand name out
- //Get extension name, without.
- NSString *str2 = @"users/centralperk/desktop/test.txt";
- NSLog (@"%@", [str2 pathextension]);
- //Add extension name, no need to bring.
- NSLog (@"%@", [str2 stringbyappendingpathextension:@"MP3"]);
- //delete extension name, with. Piece Delete
- NSLog (@"%@", [str2 stringbydeletingpathextension]);
- //String converted to int double float
- NSString *STR3 = @"123";
- NSLog (@"%i", [Str3 intvalue]);
- NSLog (@"%zi", [str3 length]);
- //Remove characters from the specified position
- Unichar C = [Str3 characteratindex:2];
- NSLog (@"%c", C);
- //to a C-language string
- Const char *s = [Str3 utf8string];
- NSLog (@"%s", s);
The intercept of the string nsstring *str = @ "123456789"; NSLog (@ "%@", [str substringfromindex:3]); NSLog (@ "%@", [str substringtoindex:6]); NSLog (@ "%@", [Str substringwithrange:nsmakerange (3, 3)]); Splits a string with the specified string, returning an array nsarray *array = [@ "1,2,3,4,5,6" componentsseparatedbystring:@ ","]; NSLog (@ "%@", array); Combines the strings in an array into a file path nsmutablearray *components = [Nsmutablearray array]; [Components addobject:@ "Users"]; [Components addobject:@ "Centralperk"]; [Components addobject:@ "Desktop"]; NSString *path = [NSString pathwithcomponents:components]; NSLog (@ "%@", Path); Users/centralperk/desktop//divides a path into an array nsarray *array1 = [path pathcomponents]; NSLog (@ "%@", array1); Determines whether the absolute path (depending on whether it starts with '/') path = @ "/users/centralperk/desktop"; NSLog (@ "%i", [path Isabsolutepath]); Gets the last directory NSLog (@ "%@", [path lastpathcomponent]); Delete the last directory NSLog (@ "%@", [path stringbydeletinglastpathcomponent]); Splicing a directory NSLog (@ "%@", [Path stringbyappendingpathcomponent:@ "AAA"]); USERS/CENTRALPERK/DESKTOP/AAA NSLog (@ "%@", [Path stringbyappendingstring:@ "AAA"]); USERS/CENTRALPERK/DESKTOPAAA NSLog (@ "%@", [Path stringbyappendingformat:@ "%@%@", @ "B", @ "C"]); USERS/CENTRALPERK/DESKTOPBC//Expand name out//Get extension name, without. NSString *str2 = @ "Users/centralperk/desktop/test.txt"; NSLog (@ "%@", [str2 pathextension]); Add extension name, no need to bring. NSLog (@ "%@", [str2 stringbyappendingpathextension:@ "MP3"]); Delete extension name, with. Piece delete NSLog (@ "%@", [str2 stringbydeletingpathextension]); The string is converted to int double float NSString *STR3 = @ "123"; NSLog (@ "%i", [Str3 intvalue]); NSLog (@ "%zi", [STR3 length]); Remove the character at the specified position unichar c = [Str3 characteratindex:2]; NSLog (@ "%c", c); C-language-converted string Const CHAr *s = [Str3 utf8string]; NSLog (@ "%s", s);
Methods of NSString in OC