1. String concatenation
nsstring* string; Result string
nsstring* string1 = @ "WOSHIZTQ"; Existing string, string1 and string2 need to be connected together
NSString *string2= @ "13456";
Method 1.
string = [NSString stringwithformat:@ "%@%@", string1, string2];
//Method 2
string = [string1 stringbyappendingstring:string2];
Method 3.
string = [string stringbyappendingformat:@ "%@%@", string1, string2];
/*--------Delete the character in the existing string according to the given range and length------*/
[String1 deletecharactersinrange:nsmakerange (0, 5)];
/*--------Insert the given string in the specified position after the existing string------*/
[String1 insertstring:@] hi! [atindex:0];
2. String interception
NSString *string2 = [string1 substringtoindex:3]; Intercepts from the beginning of a string to the specified position, but not the character at that position (that is, the first n characters are truncated)
NSString *string2 = [string1 substringfromindex:3]; Starts at the specified position (including the character at the specified position) and includes all subsequent characters (intercepts all characters of the first n)
NSString *string2 = [string1 substringwithrange:nsmakerange (0, 4)]; Arbitrarily intercept substrings from a string in the given position, length
3. String comparison and judgment
BOOL result = [Astring01 compare:astring02
Options:nscaseinsensitivesearch | Nsnumericsearch] = = Nsorderedsame; Do not consider case comparison string 2
Newkeys = [string Sortedarrayusingcomparator:result]; String size sorting
Nsarray *array1=[str1 componentsseparatedbystring:@ "."];/ /String Segmentation
/*-------------Determine if the string also contains other strings (prefixes, suffixes)-------------*/
NSString *string1 = @ "NSStringInformation.txt";
[String1 hasprefix:@ "nsstring"] = = 1? NSLog (@ "YES"): NSLog (@ "NO");
[String1 hassuffix:@ ". txt"] = = 1? NSLog (@ "YES"): NSLog (@ "NO");
4. String file operation
Read File contents
NSString *[email protected] "/users/kenshincui/desktop/test.txt"; NSString *str1=[nsstring Stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];
Since we have Chinese in the test.txt, we can get an error using the following code, which shows the procedure
Nserror *error; NSString *str2=[nsstring Stringwithcontentsoffile:path encoding:kcfstringencodinggb_18030_2000 error:&error];/ /Note that the error variable in this sentence is **error, that is, the pointer is the pointer's address, because error is a pointer here is the address of error &error,
if (Error) {NSLog (@ "read error, the error is%@", error);}
else{NSLog (@ "read Success,the file content is%@", str2);}
One way to read the contents of a file is to use the URL, which can read the network file in addition to the local file
Nsurl *url=[nsurl urlwithstring:@ "File:///Users/kenshincui/Desktop/test.txt"];
Nsurl *url=[nsurl urlwithstring:@ "http://www.apple.com"];
NSString *str3=[nsstring stringwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil];
Here is the file write
NSString *[email protected] "/users/kenshincui/desktop/test2.txt";
Nserror *error1; NSString *[email protected] Hello world, Hello! ";
[Str11 writetofile:path1 Atomically:yes encoding:nsutf8stringencoding error:&error1];//automically represents a write-once, If it goes wrong in the middle, it's not written at all.
if (Error1) {NSLog (@ "Write fail,the error is%@", [Error1 localizeddescription]);//Call localizeddescription to print only critical error messages}
else{NSLog (@ "Write success!");}
5. Variable string
/*--------swop existing empty characters into other strings------*/
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[string1 setstring:@ "Hello Word! "];
/*--------The original characters replaced by the given range, and the string------*/
-setstring:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 replacecharactersinrange:nsmakerange (0, 4) withstring:@ "that"];
NSLog (@ "string1:%@", String1);
ios--string Common Properties