Restudying, often new//1, creating constant strings.
NSString *astring = @ "This is a string!";
2, create an empty string, give the assignment.
NSString *astring = [[NSString alloc] init];
Astring = @ "This is a string!";
[Astring release];
NSLog (@ "astring:%@", astring);
//
NSString *astring = [[NSString alloc] init];
NSLog (@ "0x%.8x", astring);
[Email protected] "This is a string!";
NSLog (@ "0x%.8x", astring);
[Astring release];
NSLog (@ "astring:%@", astring);
3, in the above method, lifting speed: Initwithstring method
NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];
NSLog (@ "astring:%@", astring);
[Astring release];
4. Create a string with standard C: Initwithcstring method
Char *cstring = "This is a string!";
NSString *astring = [[NSString alloc] initwithcstring:cstring];
NSLog (@ "astring:%@", astring);
[Astring release];
5. Create a formatted string: placeholder (composed of one% plus one character)
int i = 1;
int j = 2;
NSString *astring = [[NSString alloc] initwithstring:[nsstring stringwithformat:@ "%d.this is%i string!", I,j]];
NSLog (@ "astring:%@", astring);
[Astring release];
6. Create a temporary string
NSString *astring;
astring = [NSString stringwithcstring: "This is a temporary string"];
NSLog (@ "astring:%@", astring);
7. Create a string from a file
NSString *path = [[Nsbundlemainbundle] pathforresource:@ "Astring.text" oftype:nil];
NSString *astring = [[NSString alloc] initwithcontentsoffile:path];
NSLog (@ "astring:%@", astring);
[Astring release];
8. Create a string with a string and write to the file
NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];
NSLog (@ "astring:%@", astring);
NSString *path = @ "Astring.text";
[astring Writetofile:path Atomically:yes];
[Astring release];
Note: This path is only indicative, the true path is not so//9, c comparison: strcmp function
Char string1[] = "string!";
Char string2[] = "string!";
if (strcmp (string1, string2) = = 0)
{
NSLog (@ "1");
}
10. Isequaltostring method
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 isequaltostring:astring02];
NSLog (@ "result:%d", result);
11. Compare method (three values returned by comparer)
//
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = Nsorderedsame; Nsorderedsame judge whether the content is the same
NSLog (@ "result:%d", result);
//
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = nsorderedascending; Nsorderedascending determines the size of two object values (in alphabetical order, astring02 greater than ASTRING01 is true)
NSLog (@ "result:%d", result);
//
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = nsordereddescending; Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)
NSLog (@ "result:%d", result);
12, do not consider case comparison string
1.
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 caseinsensitivecompare:astring02] = = Nsorderedsame; Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)
2.
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02
Options:nscaseinsensitivesearch | Nsnumericsearch] = = Nsorderedsame; Nscaseinsensitivesearch: Case-insensitive comparison nsliteralsearch: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value.
13. Output uppercase or lowercase strings
NSLog (@ "string1:%@", [string1 uppercasestring]);//Uppercase
NSLog (@ "string2:%@", [string2 lowercasestring]);//lowercase
NSLog (@ "string2:%@", [string2 capitalizedstring]);//initial Letter size
14,-rangeofstring://Find if string somewhere contains other strings
NSString *string1 = @ "This is a string";
NSString *string2 = @ "string";
Nsrange range = [string1 rangeofstring:string2];
int location = Range.location;
int leight = Range.length;
NSString *astring = [[NSString alloc] initwithstring:[nsstring stringwithformat:@ "location:%i,leight:%i", location, Leight]];
NSLog (@ "astring:%@", astring);
[Astring release];
15.-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);
16.-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);
17,-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);
18,-stringwithcapacity://Generate empty string according to fixed length
Nsmutablestring *string;
String = [nsmutablestring stringwithcapacity:40];
19,-appendstring:and-appendformat://Put one string at the end of another string
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 appendstring:@], I'll be adding some character "];
[String1 appendformat:[nsstring stringwithformat:@ ", I'll be adding some character"]];
NSLog (@ "string1:%@", String1);
20,-insertstring:atindex://Insert string at specified position
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 insertstring:@] hi! [atindex:0];
NSLog (@ "string1:%@", String1);
21,-setstring:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 setstring:@ "Hello word!"];
NSLog (@ "string1:%@", String1);
22,-replacecharactersinrange:withstring://Replace a string in a specified position and length with a specified string
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 replacecharactersinrange:nsmakerange (0, 4) withstring:@ "that"];
NSLog (@ "string1:%@", String1);
23,-hasprefix://Check whether the string starts with another string
@ "NSStringInformation.txt";
[String1 Hasprefix:@ "nsstring1? NSLog (@ "YES"): NSLog (@ "NO");
[String1 Hassuffix:@ ". txt 1? NSLog (@ "YES"): NSLog (@ "NO");
24. Extension path
NSString *path = @ "~/nsdata.txt";
NSString *absolutepath = [Path Stringbyexpandingtildeinpath];
NSLog (@ "absolutepath:%@", Absolutepath);
NSLog (@ "path:%@", [Absolutepath Stringbyabbreviatingwithtildeinpath]);
25. File name extension
NSString *path = @ "~/nsdata.txt";
NSLog (@ "extension:%@", [Path pathextension]);
Common uses of IOS NSString