# Series :iOS development -NSString

What I have said before is more theoretical, and there are few introductions that will be used in the formal development. Today, after work, I took time to start the first explanation of the whole series.

I’m still not going to talk directly about the development process today. As mentioned, today’s talk is about a data type that is commonly used in iOS development ——NSString,

So what is NSString?

Let’s just go into his class definition and see,

We can see that NSString inherits from NSObject, so it’s not the basic data type that we recognize in C,

If we go into NSObject, what we can see is that NSObject only has one member variable that is an ISA variable of type Class. So what are Class types? See, the name should refer to the class that it belongs to.

@interface NSObject  { 
        Class isa; 
} 
Copy the code

Let’s go into Class and see what it really is.

We find that Class is just a pointer to a structure.

Okay, let’s not go down that deep.

We can simply think of NSObject as the object type defined by OC

NSString inherits NSObject, yes, it’s also an object, If we have learned C++, we must all know what is called object-oriented development, all things or things are abstracted into an object, then there will be corresponding properties and methods of the object, for example, a person is abstracted into a person class, then there will be corresponding properties such as age,sex… Corresponding methods like eat,sleep,talk…

So NSString does the same thing, so what properties should it have? We can take a guess, no mistake, the length of the string, and with that in mind let’s look at the definition of NSString @Property (readonly) NSUInteger length; Sure enough, there is an attribute called Length, which represents the length of the string….

So what should he do?

We can imagine…..

What is the character corresponding to the subscript? What is the substring of the corresponding range? Comparison methods with other strings… String cutting, concatenation, reverse order…… You can think of a lot of….

OK,anywhere, with these ideas in mind, we start with the OC string NSString

First, a string is a class. So we want a string of us, what do we do? Yes, create a string object.

Here’s how I organized it. It may not be complete, but it’s pretty close.

NSString *str1 = [[NSString alloc] init]; NSLog(@"str1 = %@",str1); / / 2, initWithBytes: length: encoding: nsstrings * str2 = [[nsstrings alloc] initWithBytes:"str2" length:4 encoding:NSUTF8StringEncoding];
        NSLog(@"str2 = %@",str2); / / 3, initWithCharacters: length: UniChar s [4] = {'s'.'t'.'r'.'3'};
        NSString *str3 = [[NSString alloc] initWithCharacters:s length:4];
        NSLog(@"str3 = %@",str3); / / 4, initWithCString: encoding: nsstrings * str4 = [[nsstrings alloc] initWithCString:"str4" encoding:NSUTF8StringEncoding];
        NSLog(@"str4 = %@",str4); //5, initWithUTF8String: NSString *str5 = [[NSString alloc] initWithUTF8String:"str5"];
        NSLog(@"str5 = %@",str5); //6, initWithFormat: NSString *str6 = [[NSString alloc] initWithFormat:@"str6"];
        NSLog(@"str6 = %@",str6); StringWithFormat: NSString *str7 = [NSString stringWithFormat:@"str7"];
        NSLog(@"str7 = %@",str7); / / 8, stringWithCharacters: length: UniChar ss [4] = {'s'.'t'.'r'.'8'};
        NSString *str8 = [NSString stringWithCharacters:ss length:4];
        NSLog(@"str8 = %@",str8); NSString * str9 = [NSString stringWithString:@"str9"];
        NSString *str9 = @"str9";
        NSLog(@"str9 = %@",str9); / / 10, stringWithCString: encoding: nsstrings * str10 = [nsstrings stringWithCString:"str10" encoding:NSUTF8StringEncoding];
        NSLog(@"str10 = %@",str10); StringWithUTF8String: NSString *str11 = [NSString stringWithUTF8String:"str11"];
        NSLog(@"str11 = %@",str11);
Copy the code

So once you’ve created an object, you’re basically going to have a bunch of manipulation methods and there’s a lot more to strings in OC than that, but you can jump into a class and see what’s an instance method and what’s a class method, just experiment with it, all right

NSLog(@)"%ld", [str11 length]);
        NSLog(@"%ld", str11.length); //characterAtIndex: NSLog(@"%c", [str11 characterAtIndex:1]); GetCharacters :range: NSRange ran = NSMakeRange(1, 2); //getCharacters:range: NSRange ran = NSMakeRange(1, 2); [str11 getCharacters:s range:ran];for (int i = 0; i < ran.length; i++) {
            NSLog(@"s[%d] = %c",i,s[i]); } //substringFromIndex: (string truncated from a subscript) NSString *tmpStr = [str11 substringFromIndex:1]; NSLog(@"% @",tmpStr); //substringToIndex: (intercepts string to an index) tmpStr = [str11 substringToIndex:2]; NSLog(@"% @",tmpStr); //substringWithRange: (intercepts a string in a range) tmpStr = [str11 substringWithRange:ran]; NSLog(@"% @",tmpStr); //compare: (in descending order, equal) NSLog(@"%ld",[str11 compare:str10]); //isEqualToString: (compare: the two are equal) NSLog(@"%hhd",[str11 isEqualToString:str10]); //hasPrefix: (with or without the string header) NSLog(@"%hhd",[str11 hasPrefix:@"st"]); //hasSuffix: (with or without the string end) NSLog(@"%hhd",[str11 hasSuffix:@"11"]); //uppercaseString (string all uppercase) NSLog(@"% @",[str11 uppercaseString]); //lowercaseString (lowercase all strings) NSLog(@"% @",[str11 lowercaseString]); //capitalizedString (uppercase) NSLog(@"% @",[str11 capitalizedString]);
        
        //float//doubleValue //intValue //integerValue
        //longLongValue
        //boolValue
        NSLog(@"%f",[str11 floatValue]); //rangeOfString: (position of substring in string) NSLog(@"% @",NSStringFromRange([str11 rangeOfString:@"11"])); / / rangeOfCharacterFromSet: (a collection of characters in a string of location) NSLog (@"% @",NSStringFromRange([str11 rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"s"]])); / / stringByAppendingString: (1) additional string) NSLog (@"% @",[str11 stringByAppendingString:str10]); / / stringByAppendingFormat: (add a formatted string) NSLog (@"% @",[str11 stringByAppendingFormat:@"%d", 1)); / / stringByReplacingOccurrencesOfString: withString: (with a substring to replace a substring) NSLog (@"% @",[str11 stringByReplacingOccurrencesOfString:@"11" withString:@"2"]); / / cStringUsingEncoding: (to a certain format into a C string) const char * p1 = [str11 cStringUsingEncoding: NSUTF8StringEncoding]; NSLog(@"p1 = %s",p1);
Copy the code

Methods must be endless, some OC to help us achieve, some OC did not help us achieve, we need to achieve their own, such as reverse letters, such as reverse words….

So this is an NSString class, which is also called an immutable string, so if you say immutable, you’re going to have a mutable string, right? Yeah, it doesn’t, it also has a subclass called mutable string NSMutableString with the same learning logic, so hop in there

@interface NSMutableString : NSString
Copy the code

No, it’s a subclass of NSString, so what’s the point? Basically, NSString methods, they work, and they have methods that NSString doesn’t

Let’s go straight to the document

If you look at it a little bit, one of the things that’s different is that the NSString method returns an OBJECT of type NSString, but NSMutableString doesn’t. Why is that? The simple thing is mutable and immutable

Variable do operation means of this string, return the new results or string or itself, so don’t need a new return objects, and the meaning of the immutable is to create what is what appearance, will not change, return is a new object, of course you can directly use the original objects to represent For example: str11 = [str11 substringFromIndex:1];

Again, briefly enumerate some creation methods

// create //initWithCapacity: NSMutableString *str12 = [[NSMutableString alloc]initWithCapacity:0]; NSLog(@"str12 = %@",str12);
        
        //stringWithCapacity:
        NSMutableString *str13 = [NSMutableString stringWithCapacity:0];
        NSLog(@"str13 = %@",str13);
        
        //initWithFormat:
        NSMutableString *str14 = [[NSMutableString alloc]initWithFormat:@"str14"];
        NSLog(@"str14 = %@",str14);
        
        //stringWithFormat:
        NSMutableString *str15 = [NSMutableString stringWithFormat:@"str15"];
        NSLog(@"str15 = %@",str15);
        
        //initWithString:
        NSMutableString *str16 = [[NSMutableString alloc]initWithString:@"str16"];
        NSLog(@"str16 = %@",str16);
        
        //stringWithString:
        NSMutableString *str17 = [NSMutableString stringWithString:@"str17"];
        NSLog(@"str17 = %@",str17);
Copy the code

Here are some common methods:

Methods / / / / replaceCharactersInRange: withString: (to replace the scope of a certain class substring) str17 replaceCharactersInRange: NSMakeRange (0, 1) withString: @"1"];
        NSLog(@"% @",str17); //insertString:atIndex: [str17 insertString:@"s" atIndex:1];
        NSLog(@"% @",str17); / / deleteCharactersInRange: (delete a substring) within the scope of [str17 deleteCharactersInRange: NSMakeRange (0, 1)); NSLog(@"% @",str17); //appendString: (appends substrings) [str17 appendString:@"str17"];
        NSLog(@"% @",str17); //appendFormat: (append formatting substring) [str17 appendFormat:@"%d"1]; NSLog(@"% @",str17);
        
        //setString: (Sets the String content) [str17setString:@"str17"];
Copy the code

Of course, there are more methods than that, and again, to master a class, you need to go into that class and start with its creation and start with its properties and start with its methods, and go through it, and if you’re familiar with all of that you can do a lot of things with it, Now it’s just a string, it probably doesn’t feel anything, so what can I do with it?

Think about it, a text box, it must have content, so what content? No, it’s a string, a button with text on it, so what is it? There’s no mistake, it’s a string, but anyway, in development, all the text that you can see is a string what about the text that you can’t see? Similarly, if I have a person’s object, then it will have a property named, what type do I store this property in? Yes, it’s also a string….

So in development we will often use it, intentionally or unintentionally use it, whether it is on the surface of the representation, or some underlying methods, some logic, some storage, will be used

Just a string of words, you might say. What can you do?

Just to give you a quick example from before, this is a string can you convert it to string a is this using the logic you learned like if like for like all of the methods above?

There’s no mistake, that’s a method, you’ll see this in your development, and if there’s a person whose name is xiaoming, age is 12, sex is male, can we use an NSString to represent the name :xiaoming, gender: male, age :12?

These seem to be simple, but in fact are the development will encounter, all encountered in the development of complex methods are known by us simple methods or a number of logical judgments and so on to achieve…

OK, strings for now, there will be strings, I will continue to add…

The Demo address: github.com/spicyShrimp…

Series: iOS development – preface + outline blog.csdn.net/spicyShrimp…