The string is a very important and commonly used part of OC, the string in OC is different from the string I learned in C,c++,java, which is very similar to the concept of container in C + +, but the usage is still very different from it. Maybe it's because OC's syntax is different from the other programming languages we use. Here's a summary of the string nsstring and nsmutablestring.
I. NSString
NSString represents an immutable string of character sequences, NSString is very powerful, and OC's string processing is much simpler and easier to use than the C language's hungry strings.
Here we have a concrete example to analyze.
Create a two string object:
NSString *STR1 = @ "This is string A"; NSString *STR2 = @ "This is string B";
To count the number of characters in a string:
NSLog (@ "Length of str1:%lu", [str1 length]);
Use stringwithstring to copy a string to another string:
res = [NSString stringwithstring:str1];
res = [str1 copy];
NSLog (@ "copy:%@", res);
stringbyappendingstring, copy a string to the end of another string:
STR2 = [str1 stringbyappendingstring:str2];
isequaltonumber : Method compares the values of two NSNumber objects. The program returns a bool value to see if the two values are equal.
Isequaltostring, determine whether two strings are equal:
if ([str1 isequaltostring:res] = = YES) NSLog (@ "STR1 = = res"); else NSLog (@ "STR1! = res");
Compare : Method Tests whether a value is less than, equal to, or greater than another value on a number.
such as: [Intnumber Compare:mynumber]
If intnumber is less than MyNumber, return to nsorderedascending;
Equal, returning nsorderdsame;
Greater than, return nsorderddescending
Verifies whether a string is less than, equal to, or greater than another string compareresult = [str1 compare:str2]; if (compareresult = = nsorderedascending) NSLog (@ "str1 < str2"); else if (compareresult = = nsorderedsame) NSLog (@ "str1 = = str2"); else NSLog (@ "str1 > str2");
uppercasestring, converts the string to uppercase.
lowercasestring, converts the string to lowercase.
Converts a string to uppercase res = [str1 uppercasestring]; Converts a string to lowercase res = [str1 lowercasestring];
stringbyappendingstring, add a fixed string after the string:
str = [str stringbyappendingstring:@ ", ios!"];
Substringtoindex, gets the string consisting of the first 10 characters of STR:
Substringtoindex: Method creates a substring, including the number of indexes specified by the first character, but does not include this character . Because the number of indexes starts at 0, the parameter 3 represents extracting 0, 1, 2 from the string and returning the result string object. For all string methods that take the number of indexes as arguments, if the supplied number of indexes is not valid for the string, a range or index out of bounds error message is obtained.
Gets the first 10 characters of STR, consisting of a string nsstring *s1 = [str substringtoindex:10]; NSLog (@ "%@", S1);
Substringfromindex, gets the string that str starts with the 5th character and consists of the following characters:
Gets the string nsstring *s2 = [Str substringfromindex:5] Starting with the 5th character of STR, and the following characters; NSLog (@ "%@", S2);
Gets the string that str starts with the 5th character and consists of the 15th character:
Gets STR starting from the 5th character, to the 15th character of the string nsstring *s3 = [str substringwithrange:nsmakerange (5)]; NSLog (@ "%@", S3);
rangeofstring , get the location where iOS appears in Str:
Gets the location where iOS appears in str nsrange pos = [str rangeofstring:@ "IOS"]; NSLog (@ "ios start in str:%ld, Length:%ld", Pos.location, Pos.length);
Two. nsmutablestring
The Nsmutablestring object represents a string with a variable sequence of characters, and Nsmutablestring is a subclass of NSString, so the methods included in the NSString described earlier, nsmutablestring can be used directly, Nsmutablestring objects can also be used directly as NSString objects.
stringwithstring, creates a mutable string with an immutable string:
Creating mutable strings from immutable strings MSTR = [nsmutablestring stringwithstring:str1]; NSLog (@ "%@", MSTR);
insertstring, inserting characters:
Insert character [mstr insertstring:@ "mutable" atindex:7]; NSLog (@ "%@", MSTR);
insertstring:atindex: , insert the end of the effective stitching:
Insert the end of the effective stitching [Mstr insertstring:@ "and string B" atindex:[mstr length]]; NSLog (@ "%@", MSTR);
Deletecharactersinrange:nsmakerange () , to delete substrings by range:
Delete substrings by range [MSTR deletecharactersinrange:nsmakerange (+)]; NSLog (@ "%@", MSTR);
Find and then delete directly :
Find and then delete substr = [Mstr rangeofstring:@ "string B and"]; if (substr.location! = nsnotfound) { [mstr deletecharactersinrange:substr]; NSLog (@ "%@", MSTR); }
The sample code is as follows:
1 #import <Foundation/Foundation.h> 2 3 int main (int argc, const char * argv[]) 4 {5 6 @autoreleasepool {7 8//Insert code here ... 9//nslog (@ "Hello, world!"); NSString *STR1 = @ "This is string A"; NSString *search, *replace;13 Nsmutablestri ng *mstr;14 Nsrange substr;15 16//Immutable string creation variable String * MSTR = [nsmutablestring Stringwithstri Ng:str1];18 NSLog (@ "%@", MSTR); 19 20//Insert character [mstr insertstring:@ "mutable" atindex:7];22 NSLog (@ "%@", MSTR); 23 24//effective stitching at the end of insert [Mstr insertstring:@ "and string B" Atindex:[mst R length]];26 NSLog (@ "%@", MSTR); 27 28 29//Direct use of appendString30 [MSTR appendstring:@ "and String C "];31 NSLog (@"%@ ", MSTR); 32 33//delete substring based on range [MSTR Deletecharactersinrange:nsma Kerange (+)];35 NSLog (@ "%@", MSTR); 36 37 Find and then delete it substr = [Mstr rangeofstring:@ "string B and"];39 if (substr.location! = Nsnotfou nd) {MSTR deletecharactersinrange:substr];42 NSLog (@ "%@", MSTR); 43}44 45 46//Directly set to variable string MSTR [setstring:@ "This is String A"];48 NSLog (@ "%@", MSTR); 49 50 Replace some characters [MSTR Replacecharactersinrange:nsmakerange (8, 6) withstring:@ "a mutable string"];52 NSL OG (@ "%@", MSTR); 53 54//Find and Replace with search = @ "This is"; "a example of"; 57 substr = [Mstr rangeofstring:search];59 if (substr.location! = Nsnotfound) {61 [Mstr replacecharactersinrange:substr withstring:replace];62 NSLog (@ "%@", MSTR); 63}64 65 Find and replace all occurrences of the search = @ "a"; substr replace = @ "X"; MSTR rangeofstring:s EARCH];70 71 while (substr.location! = nsnotfound) {MSTR Replacecharactersinrange:substr withstring:replace];73 }75 NSLog (@ "%@", MSTR);}79 return 0;80}
Objective-c: String nsstring and Nsmutablestring