1??. NSString
In C, strings are made up of char (ASCII) characters, each of which takes one byte
In OC, the string is composed of Unichar (Unicode) characters, each of which occupies two bytes
NSString: Immutable string, that is: after creation, content and length cannot be changed
Nsmutablestring: variable string, that is: After creation, the content can also be modified
1. > Creation of strings
> Open Space, Initialize
NSString *string = [[NSString alloc] initwithformat:@ "I love IOS"];
> Convenience Builder
NSString *string = [NSString stringwithformat:@ "I love IOS"];
> Literal--laughter grammar, grammar sugar
NSString *string = @ "I love IOS";
Get string Length:
NSLog (@ "%d", string.length);
Get string in string
-(Unichar) Characteratindex: (Nsuinteger) Indes;
Determine if two strings are equal
[String isequaltostring:string1];(return value is type bool)
Comparison of strings
-(Nscomparisonresult) Compare: (NSString *) string;
Get substring
-(NSString *) Substringfromindex: (Nsuinteger) from;
-(NSString *) Substringtoindex: (Nsuinteger) to;
Stitching strings
-(NSString *) Stringbyappendingformat: (NSString *) format, ...;
Replace string
-(NSString *) Stringbyreplacingcharactersinrange: (nsrange) Range withstring: (NSString *) Replacement;
string conversion to int type
@property (readonly) int intvalue;
String all uppercase
@property (readonly, copy) NSString *uppercasestring;
String All lowercase
@property (readonly, copy) NSString *lowercasestring;
Capitalize the first letter of each string separated by a space number
@property (readonly, copy) nsstring*capitalizedstring;
Whether to prefix the specified string
-(BOOL) Hasprefix: (NSString *) str;
Like what:
NSString *string = [NSString stringwithformat:@ "Hello.png"];
BOOL isture = [string hasprefix:@ "Hello"];
if (1 = = isture) {
NSLog (@ "string is preceded by Hello");
} else {
NSLog (@ "string not preceded by Hello");
}
Whether to suffix with the specified string
-(BOOL) Hassuffix: (NSString *) str;
2??. > Variable String nsmutablestring
The string created by nsmutablestring is a dynamic variable string that can be added, deleted, changed, and manipulated for the original string object.
Immutable string--cannot be modified by itself
The modification of an immutable string is done by a copy of the original string, and a new string is obtained.
Variable string--can be modified by itself
mutable strings Modify the original string, so the action method of a mutable string does not return a value!!
Initialize method
-(Nsmutablestring *) initwithcapacity: (Nsuinteger) capacity
Convenience Builder
+ (nsmutablestring *) stringwithcapacity: (Nsuinteger) capacity;
The capacity--parameter value is the estimated space size, but dynamically adjusts the actual space size according to the actual storage situation
Mutable strings also have their own concatenation of string methods
-(void) AppendFormat: (NSString *) format, ...;
Note: nsmutablestring can complete the deletion of the string itself, but if you call the parent class NSString specific methods, there may be some errors such as
nsmutablestring *mstr = [nsmutablestring stringwithformat:@ "Hello World"];
[Mstr stringbyappendingformat:@ "Hello"];
NSLog (@ "%@", MSTR);
This output is still hello world.
Insert String
-(void) insertstring: (NSString *) astring Atindex: (nsuinteger) Loc;
Delete a string
-(void) Deletecharactersinrange: (nsrange) range;
Replace string
-(void) Replacecharactersinrange: (nsrange) Range withstring: (NSString *) astring;
Reset String
-(void) setString: (NSString *) astring;
2?? 1.> an integer 123, stored as an object of type NSNumber.
int b = 123;
NSNumber *number1 = [NSNumber numberwithint:b];
NSLog (@ "number1 =%@", number1);
int B1 = [Number1 intvalue];
NSLog (@ "result =%d", B1);
2.> 3.14159, which is stored as an object of type NSNumber.
float C = 3.14159;
NSNumber *number2 = [NSNumber numberwithfloat:c];
NSLog (@ "number2 =%@", number2);
float C1 = [number2 floatvalue];
NSLog (@ "result =%f", C1);
3.> ' C ' is stored as an object of type NSNumber.
Char d = ' C ';
NSNumber *number3 = [NSNumber numberwithchar:d];
NSLog (@ "Number3 =%@", Number3);
char D1 = [Number3 charvalue];
NSLog (@ "result =%c", D1);
4.> will save Yes as an object of type NSNumber.
BOOL e = YES;
NSNumber *number4 = [NSNumber numberwithbool:e];
NSLog (@ "number4 =%@", number4);
BOOL e1 = [Number4 boolvalue];
NSLog (@ "result =%d", E1);
3?? 1.> defines the student structure, including name, gender, age, score, and number; Defines the variable for the struct and stores it as an object of type Nsvalue.
typedef struct student{
Char name[20];//name
Char gender;//Sex
int age;//Age
int score;//fraction
int number;//No.
}student;
Student stu = {"Xiaoming", ' m ', 18, 88, 6};
Nsvalue *stu = [Nsvalue valuewithbytes:&stu objctype: @encode (Student)];
NSLog (@ "Stu =%@", Stu);
OC in the MRC environment can be such a structure
typedef struct STUDENT {
NSString *name;//Name
NSString *gender;//Sex
Nsinteger age;//Age
Nsinteger score;//Score
Nsinteger number;//School Number
}student;
Student student1 = {@ "xiaoming", @ "male", 18, 88, 6};
Class method
Nsvalue *stu =[nsvalue value:&student1 withobjctype: @encode (Student)];
//
Object methods
Nsvalue *STU1 = [[Nsvalue alloc] initwithbytes:&student1 objctype: @encode (Student)];
NSLog (@ "Stu =%@", Stu);
NSLog (@ "STU1 =%@", STU1);
2.> defines a variable of type nsrange, stored as an object of type Nsvalue.
Nsrange range = Nsmakerange (2, 5);
Nsvalue *rangev = [Nsvalue valuewithrange:range];
NSLog (@ "rangevalue =%@", Rangev);
3.> the object of the above Nsvalue type back to the corresponding data type.
Nsrange Newrange = [Rangev rangevalue];
NSLog (@ "Newrange =%@", Nsstringfromrange (Newrange));
OC Strings and values