Objective-C uses nsstring to operate strings. nsstring is called a non-modifiable string because the content and length of a string cannot be modified when you create a string using nsstring.
1. Create a string
Nsstring * STR = @ "I Am a string ";
2. format the string
Nsstring * name = @ "Zhang"; nsstring * log = [nsstring stringwithformat: @ "I am '% @'", name]; nslog (@ "str: % @", log );
Format the output symbol:
% @ Object % d, % I integer % u unsigned integer % F floating point/double character % x, % x binary integer % O octal integer % zu size_t % P pointer % E floating point/double character (Scientific Computing) % G floating point/double character % s c string %. * s Pascal string % C character % C unichar % LLD 64-bit long integer (long) % LlU no character 64-bit long integer % lf 64-bit double character % E is a real number, calculated by scientific notation
3. Use one character string to initialize another character string:
Nsstring * STR = @ "I Am a string"; nsstring * str1 = [nsstring stringwithstring: Str];
4. String comparison and determination
-(Bool) isequaltostring :( nsstring *) string;-(bool) hasprefix: (nsstring *) string;-(bool) hassuffix: (nsstring *) string;
5. Numerical Conversion
-(INT) intvalue;-(double) doublevalue;-(nsinteger) integervalue;-(float) floatvalue;
6. case-sensitive Conversion
-(Nsstring *) lowercasestring; // convert it to an unwritten string-(nsstring *) uppercasestring; // convert it to an uppercase string
7. String Truncation
-(Nsstring *) substringfromindex: I; // returns the substring (nsstring *) substringtoindex: I from the beginning to the end; // returns the string starting from string to I-(nsstring *) substringwidthrange: range; // returns the string in the returned range.
8. Obtain the string length.
-(Unsigned INT) length;
9. convert a char * string to an nsstring
Char * string = "I Am a string"; nsstring * nstring = [[nsstring alloc] initwithuf8string: String];
10. Obtain the nsstring as a char * string.
Nsstring * STR = @ "I Am a string"; char * CSTR = [STR utf8string];