During the development process, we will encounter a lot of string operations. It is a common data type, and the usage of it is summarized below:
// 1. Create a constant string.
Nsstring * astring = @ "this is a string! ";
// 2. Create an empty string and assign a value.
Nsstring * astring = [[nsstring alloc] init];
Astring = @ "this is a string! ";
Nslog (@ "astring: % @", astring );
[Astring release];
// 3. In the preceding method, the speed is improved by using the initwithstring method.
Nsstring * astring = [[nsstring alloc] initwithstring: @ "this is a string! "];
Nslog (@ "astring: % @", astring );
[Astring release];
// 4. Use Standard C to create a string: initwithcstring Method
Char * cstring = "this is a string! ";
Nsstring * astring = [[nsstring alloc] initwithcstring: cstring];
Nslog (@ "astring: % @", astring );
[Astring release];
// 5. Create a formatted string: A placeholder (consisting of one % and 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 );
Nsstring * Path = @ "astring. Text ";
Nsstring * astring = [[nsstring alloc] initwithcontentsoffile: path];
Nslog (@ "astring: % @", astring );
[Astring release];
Nsstring * astring = [[nsstring alloc] initwithstring: @ "this is a string! "];
Nslog (@ "astring: % @", astring );
Nsstring * Path = @ "astring. Text ";
[Astring writetofile: path atomically: Yes];
[Astring release];
// Compare the strcmp function with C
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 isw.tostring: 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 );
// Determine whether the content of nsorderedsame 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 the two objects (which is compared alphabetically. astring02 is true if it is greater than astring01)
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 the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01)
// Do not consider comparing strings with uppercase and lowercase letters 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 the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01)
// How to judge whether the string is null
Nsstring * urlstring = [urlinput stringvalue];
If (! Urlstring ){
Nslog (@ "no input .");
} Else {
If ([urlstring length] = 0 ){
Nslog (@ "no input .");
} Else {
}
}
}