First, NSString
/*----------------method to create a string----------------*/
1. Create a constant string.
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);
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);
/*----------------read string from File: Initwithcontentsoffile method----------------*/
NSString *path = @ "Astring.text";
NSString *astring = [[NSString alloc] initwithcontentsoffile:path];
NSLog (@ "astring:%@", astring);
[Astring release];
/*----------------Write String to file: WriteToFile method----------------*/
NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];
NSLog (@ "astring:%@", astring);
NSString *path = @ "Astring.text";
[astring Writetofile:path Atomically:yes];
[Astring release];
/*----------------Compare two strings----------------*/
Compare by C: strcmp function
Char string1[] = "string!";
Char string2[] = "string!";
if (strcmp (string1, string2) = = 0)
{
NSLog (@ "1");
}
Isequaltostring method
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 isequaltostring:astring02];
NSLog (@ "result:%d", result);
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;
NSLog (@ "result:%d", result);
Nsorderedsame judge whether the content is the same
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = nsorderedascending;
NSLog (@ "result:%d", result);
Nsorderedascending determines the size of two object values (in alphabetical order, astring02 greater than ASTRING01 is true)
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = nsordereddescending;
NSLog (@ "result:%d", result);
Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)
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;
NSLog (@ "result:%d", result);
Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)
Do not consider case comparison string 2
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02
Options:nscaseinsensitivesearch | Nsnumericsearch] = = Nsorderedsame;
NSLog (@ "result:%d", result);
Nscaseinsensitivesearch: Case-insensitive comparison nsliteralsearch: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value.
/*----------------Change the case of the string----------------*/
NSString *string1 = @ "A String";
NSString *string2 = @ "String";
NSLog (@ "string1:%@", [string1 uppercasestring]);//Uppercase
NSLog (@ "string2:%@", [string2 lowercasestring]);//lowercase
NSLog (@ "string2:%@", [string2 capitalizedstring]);//initial Letter size
/*---------------search for substrings in the string----------------*/
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];
/*----------------Extract the substring----------------*/
-substringtoindex: Intercepts from the beginning of a string to the specified position, but not to the character at that position
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringtoindex:3];
NSLog (@ "string2:%@", string2);
-substringfromindex: Starts at the specified position (including the character at the specified position) and includes all subsequent characters
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringfromindex:3];
NSLog (@ "string2:%@", string2);
-substringwithrange://intercept substrings arbitrarily from a string according to the given position, length,
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringwithrange:nsmakerange (0, 4)];
NSLog (@ "string2:%@", string2);
Extension path
NSString *path = @ "~/nsdata.txt";
NSString *absolutepath = [Path Stringbyexpandingtildeinpath];
NSLog (@ "absolutepath:%@", Absolutepath);
NSLog (@ "path:%@", [Absolutepath Stringbyabbreviatingwithtildeinpath]);
File name extension
NSString *path = @ "~/nsdata.txt";
NSLog (@ "extension:%@", [Path pathextension]);
/*******************************************************************************************
Nsmutablestring
*******************************************************************************************/
/*---------------assign capacity to string----------------*/
Stringwithcapacity:
Nsmutablestring *string;
String = [nsmutablestring stringwithcapacity:40];
/*---------------Add a character after an existing string----------------*/
Appendstring:and AppendFormat:
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);
*/
/*--------Delete the character in the existing string according to the given range and length------*/
/*
Deletecharactersinrange:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 deletecharactersinrange:nsmakerange (0, 5)];
NSLog (@ "string1:%@", String1);
/*--------Insert the given string in the specified position after the existing string------*/
-insertstring:atindex:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 insertstring:@] hi! [atindex:0];
NSLog (@ "string1:%@", String1);
/*--------Swop an existing empty character into another string------*/
-setstring:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 setstring:@ "Hello word!"];
NSLog (@ "string1:%@", String1);
/*--------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);
/*-------------Determine if the string also contains other strings (prefixes, suffixes)-------------*/
01: Check if the string starts with another string-(BOOL) Hasprefix: (NSString *) astring;
NSString *string1 = @ "NSStringInformation.txt";
[String1 hasprefix:@ "nsstring"] = = 1? NSLog (@ "YES"): NSLog (@ "NO");
[String1 hassuffix:@ ". txt"] = = 1? NSLog (@ "YES"): NSLog (@ "NO");
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Objectivec Development Tutorial--the basic operation of string processing method