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
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
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
String! ";
Nsstring * astring02 = @ "This Is
String! ";
Bool result = [astring01
Isequaltostring: astring02];
Nslog (@ "Result: % d", result );
// Compare method (three values returned by comparer)
Nsstring * astring01 = @ "This Is
String! ";
Nsstring * astring02 = @ "This Is
String! ";
Bool result = [astring01 compare: astring02]
= Nsorderedsame;
Nslog (@ "Result: % d", result );
// Determine whether the content of nsorderedsame is the same
Nsstring * astring01 = @ "This Is
String! ";
Nsstring * astring02 = @ "This Is
String! ";
Bool result = [astring01 compare: astring02]
= Nsorderedascending;
Nslog (@ "Result: % d", result );
// Nsorderedascending
Determine the size of the two object values (in alphabetical order, astring02 is true if it is greater than astring01)
Nsstring * astring01 = @ "This Is
String! ";
Nsstring * astring02 = @ "This Is
String! ";
Bool result = [astring01 compare: astring02]
= Nsordereddescending;
Nslog (@ "Result: % d", result );
// Nsordereddescending
Determine the size of the two object values (in alphabetical order, astring02 is true if it is smaller than astring01)
// Do not consider comparing strings with uppercase and lowercase letters 1
Nsstring * astring01 = @ "This Is
String! ";
Nsstring * astring02 = @ "This Is
String! ";
Bool result = [astring01
Caseinsensitivecompare: astring02] =
Nsorderedsame;
Nslog (@ "Result: % d", result );
// Nsordereddescending determines the size of the two object values (in alphabetical order, astring02 is smaller than astring01
True)
// 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
{
}
}
}