The Objective-c class in the core processing string is NSString and nsmutablestring, the biggest difference between the two classes is that the content and length of the string cannot be changed dynamically after nsstring creation of the assignment, unless the string is re-assigned to the value. Nsmutablestring creates an assignment to dynamically change the content and length on that string.
+ (ID) stringwithcontentsoffile:path Encoding:enc error:err
Creates a new string and sets it to the contents of the file specified by path, uses the character encoding enc, and returns err in error if nonzero
+ (ID) stringwithcontentsofurl:url Encoding:enc error:err
Creates a new string and sets it to the contents of the URL, using the character encoding enc, and returns the error in err if nonzero
+ (ID) string
Create a new empty string
+ (ID) stringwithstring:nsstring
Create a new string and set it to NSString
NSString *str = @ "1";
Initwithstring and stringwithtstring is not now possible to add strings directly, but must be the name of the string?
NSString *STR2 = [NSString stringwithstring:str];
-(ID) initwithstring:nsstring
Set the assigned string to NSString
NSString *str = @ "1";
NSString *STR1 = [[NSString alloc] initwithstring:str];
-(ID) initwithcontentsoffile:path Encoding:enc error:err
Set the string to the contents of a file made by path
-(ID) initwithcontentsofurl:url Encoding:enc error:err
Sets the string to the content of the URL (nsurl *) URL, uses the character encoding enc, and returns the error in err if nonzero
-(ID) (UNSIgned int) length
Returns the number of characters in a string
[str length]; string length
-(Unichar) characteratindex:i???????
Returns the Unicode character of index I
-(NSString *) substringfromindex:i
Returns a substring that knows the end from I
-(NSString *) Substringwithrange:range
Returns a substring based on a specified range
-(NSString *) substringtoindex:i
Returns the substring from the beginning of the string to the rope I
-(Nscomparator *) caseinsensitivecompare:nsstring
Compares two strings, ignoring case
-(Nscomparator *) compare:nsstring
Comparison of two strings
-(BOOL) hasprefix:nsstring//prefix for the meaning of the prefix
Test whether the string starts with NSString
-(BOOL) hassuffix:nsstring//suffix for suffix subscript meaning
Test whether the string ends with Nsstrng
-(BOOL) isequaltostring:nsstring??????? What's the difference with isequal?
Tests whether two strings are equal
-(NSString *) capitalizedstring
Returns the first uppercase string of each word (the remaining letters of each word are converted to lowercase)
-(NSString *) lowercasestring
Returns a string converted to lowercase
-(NSString *) uppercasestring
Returns a string converted to uppercase
-(const char*) utf8string
Returns a string converted to a UIF-8 string
-(double) doublevalue
Returns a string converted to double
-(float) floatvalue
Returns a string converted to a floating-point value
-(Nsinteger) IntegerValue
Returns a string converted to an Nsinteger integer
-(int) intvalue
Returns a string converted to an integer
Nsmutablestring method
The Nsmutablestring class can be used to create string pairs that can change characters. Because it is a subclass of NSString, all methods of the NSString class can be used.
+ (ID) stringwithcapacity:size
Creates a string that initially contains the size of the character
-(ID) initwithcapacity:size
Initializes a string with a string with an initial capacity of size
-(void) setstring:nsstring
Set the string to NSString
-(void) appendstring:nsstring
Append NSString to the end of the receiver
-(void) Deletecharactersinrange:range
Delete a character in a specified range
-(void) insertstring:nsstring atindex:i
Insert NSString with index I as the starting position
-(void) Replacecharactersinrange:range withstring:nsstring
Replace the character specified by range with NSString
-(void) replaceoccurrencesofstring:nsstring withstring:nsstring2 options:opts Range:range
OPTs according to the option. Replaces all nsstring with the nsstring2 in the specified range. Options can include Nsbackwardssearch (search from the end of the range) Nsanchoredsearch (NSString must match the start of the range), Nsliteralsearch (performs byte-by-bit comparisons and bitwise OR combination of the Nscaceinsensitivesearch
Instance:
1. nsstring *beijing= @ "Welcome to Beijing"; Declaration of a string
2, NSString *[email protected] "Beijing welcomes you a";//[nsstring stringwithformat:@ "I am '%@ '", Beijing]; String Formatting
3. NSString *zhui = [Beijing stringbyappendingstring:@ "hahaha"]; String append
The result is that Beijing welcomes you hahaha; append meaning attached to hang up;
4, BOOL b=[beijing Isequaltostring:log]; string comparison
5, NSString *hh = @ "http://www.sina.com.cn";
if ([HH hasprefix:@ "http"]) {//Find string starting with HTTP
NSLog (@ "contains http");
}else{
NSLog (@ "no http");
}
6, NSString *ss = @ "123";
int a = [SS intvalue]+13; string to int type
Double DD = [SS doublevalue]+33.3; String to double type
NSLog (@ "%g", DD);
7. String to Array
NSString *zifuchuan [email protected] "one,two,three,four";
NSLog (@ "string:%@", Zifuchuan);
Nsarray *array = [Zifuchuan componentsseparatedbystring:@ ","];
NSLog (@ "array:%@", array); Outputs the results of all elements in the entire array:
2015-08-19 16:30:19.900 demo[93879:4702032] String:one,two,three,four
2015-08-19 16:30:19.901 demo[93879:4702032] Array: (One
,Both
,
Three,
Four
)
NSString *value = [array objectatindex:0]; Take the No. 0 element out of the
NSLog (@ "value:%@", value); Result:
2015-08-19 16:33:03.577 demo[93917:4703555] Value:one
8,//array to string
NSString * zifuchuan2 = [array componentsjoinedbystring:@ ","];
NSLog (@ "zifuchuan2:%@", zifuchuan2);
Results:
Zifuchuan2:one,two,three,four
9.-substringtoindex: Truncate from the beginning of the string to the specified position, but not the character of the position
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringtoindex:3];
NSLog (@ "string2:%@", string2); Results:
String2:thi10.-substringfromindex: Start at the specified position (including the character at the specified position) and include all subsequent characters
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringfromindex:3];
NSLog (@ "string2:%@", string2); Results:
2015-08-19 16:41:51.772 demo[94021:4708352] String2:s is a string11,-substringwithrange://In accordance with the given position, length, arbitrarily intercept the substring from the string
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringwithrange:nsmakerange (0, 4)];
NSLog (@ "string2:%@", string2);
Results:
String2:this
Remove a position in parentheses around a string
-(NSString *) handlestringwithstring: (NSString *) str{
nsmutablestring * Mustr = [nsmutablestring stringwithstring:str];
while (1) {
Nsrange range = [mustr rangeofstring:@ "("];
Nsrange Range1 = [Mustr rangeofstring:@ "]"];
if (range.location! = nsnotfound) {
Nsinteger loc = range.location;
Nsinteger len = range1.location-range.location;
[Mustr deletecharactersinrange:nsmakerange (Loc, Len + 1)];
}else{
Break
}
}
return mustr;
}
--------manipulating dynamic string--nsmutablestring----------------------------------------------------
nsmutablestring *mstr = [[Nsmutablestring alloc] Init]; instance session a mutable string;
NSString *STR1 = @ "This is a example.";
Creating a mutable string
MSTR = [nsmutablestring stringwithstring:str1];
Insert character
[Mstr insertstring:@ "very easy" atindex:10]; Results:
2015-08-19 16:48:13.620 demo[94107:4712065] This was a very easy example.[Mstr Deletecharactersinrange:nsmakerange (10,5)];//Delete some character results:
2015-08-19 16:51:54.396 demo[94184:4714267] This was a easy example.Find and delete
Nsrange substr = [MSTR rangeofstring:@ "Example"]; String lookup, you can determine if there is a string in the
if (substr.location! = nsnotfound) {
[Mstr Deletecharactersinrange:substr];
}
Re-set string
[MSTR setstring:@ "This is a string AAA"];
Replace string
[Mstr Replacecharactersinrange:nsmakerange (2) withstring:@ "BBB"]; Replace the 2 string at the 15th string
Results:
2015-08-19 16:54:14.757 demo[94216:4715472] This is string BbbaFind the first one and replace
NSString *search = @ "This is";
NSString *replace = @ "An example of";
substr = [MSTR rangeofstring:search];//substr type is nsrange;
if (substr.location! = nsnotfound) {
[MSTR Replacecharactersinrange:substr Withstring:replace]; Replace the 1th encountered substr with replace
NSLog (@ "%@", MSTR);
}
Rangeofstring The preceding argument is the string to be searched, followed by the character to be searched
Nsnotfound indicates that a request operation is something or item is not found, or does not exist
Find all matches, and replace
Search = @ "a";
replace = @ "X";
substr = [MSTR Rangeofstring:search];
while (substr.location! = nsnotfound) {
[MSTR Replacecharactersinrange:substr Withstring:replace];
substr = [MSTR Rangeofstring:search];
}
NSLog (@ "%@", MSTR);
Objective-c of NSString and nsmutablestring