Methods of NSString in OC

Source: Internet
Author: User

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?
  1. //intercept of strings   
  2. NSString *str = @"123456789";
  3. NSLog (@"%@", [str substringfromindex:3]);
  4. NSLog (@"%@", [str substringtoindex:6]);
  5. NSLog (@"%@", [str substringwithrange:nsmakerange (3, 3)]);
  6. //Split the string with the specified string, returning an array   
  7. Nsarray *array = [@"1,2,3,4,5,6" componentsseparatedbystring:@","];
  8. NSLog (@"%@", array);
  9. //Combine strings in an array into one file path   
  10. Nsmutablearray *components = [Nsmutablearray array];
  11. [Components addobject:@"Users"];
  12. [Components addobject:@"Centralperk"];
  13. [Components addobject:@"Desktop"];
  14. NSString *path = [NSString pathwithcomponents:components];
  15. NSLog (@"%@", Path); //users/centralperk/desktop
  16. //Divide a path into an array   
  17. Nsarray *array1 = [path pathcomponents];
  18. NSLog (@"%@", array1);
  19. //Determine if it is an absolute path (based on: Start with '/')   
  20. Path = @"/users/centralperk/desktop";
  21. NSLog (@"%i", [path Isabsolutepath]);
  22. //Get the last directory   
  23. NSLog (@"%@", [path lastpathcomponent]);
  24. //Delete last directory   
  25. NSLog (@"%@", [path stringbydeletinglastpathcomponent]);
  26. //Splicing a directory   
  27. NSLog (@"%@", [path stringbyappendingpathcomponent:@"AAA"]); ///users/centralperk/desktop/aaa
  28. NSLog (@"%@", [path stringbyappendingstring:@"AAA"]); ///users/centralperk/desktopaaa
  29. NSLog (@"%@", [path stringbyappendingformat:@"%@%@", @"B",@ "C"  ]); ///USERS/CENTRALPERK/DESKTOPBC
  30. //Expand name out   
  31. //Get extension name, without.   
  32. NSString *str2 = @"users/centralperk/desktop/test.txt";
  33. NSLog (@"%@", [str2 pathextension]);
  34. //Add extension name, no need to bring.   
  35. NSLog (@"%@", [str2 stringbyappendingpathextension:@"MP3"]);
  36. //delete extension name, with. Piece Delete   
  37. NSLog (@"%@", [str2 stringbydeletingpathextension]);
  38. //String converted to int double float   
  39. NSString *STR3 = @"123";
  40. NSLog (@"%i", [Str3 intvalue]);
  41. NSLog (@"%zi", [str3 length]);
  42. //Remove characters from the specified position   
  43. Unichar C = [Str3 characteratindex:2];
  44. NSLog (@"%c", C);
  45. //to a C-language string   
  46. Const   char *s = [Str3 utf8string];
  47. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.