iOS Learning NSString

Source: Internet
Author: User

NSString:
Range Nsrangesomething that is relevant to a thing
typeof struct_nsrange {
unsignedLocaltion;
unsignedlength;
} Nsrange;

Localtion represents the range of characters within a character or the extent of an array element, and length indicates the number of elements that the string contains. For example: Objective-c is a cool language. Cool can be expressed in the range of Location17,length4, location can also use Nsnotfound to indicate that the value does not represent a range. There are three ways to create NsrangeLaw One: direct assignment to a stringNsrangerange;
range.location= -;
Range.length=4
method Two: The mechanism of aggregation structure assignment using C languageNsragerange = { -,4};
Law III: Use a shortcut function provided by Cocoa Nsmakerange ()Nsrangerange =Nsmakerange( -,4);
The advantage of Nsmakerange () is that it can be used in any function, for example, as a parameter:[AnObject Flarbulatewithrange:nsmakerange ( -, 4)];
nsstring syntax is a double quote preceded by the @ sign, for example@ "hi~"
Create string: stringWithFormat
+ (ID) stringWithFormat: (NSString *) format,...;
NSString*STR;
str = [NSString stringWithFormat:@ "I am%d years old and my wight are%d Kg.", at, $];
The result is:I am years old and my weight are 65kg.
stringWithFormat is a class method.
string Lengths: length
-(nsuinteger) Length
(the difference between Nsuinteger and Nsinteger is that it is an unsigned integer, and the latter is a signed integral type.) )
you can use that .Nsuintegerlen = [str length]
you can do that .if([str length] > *) {
NSLog (@ "Wow,this string is really tall!")
}

string comparison: isequaltostring
isequaltostring is used to compare the receiver (Recever, the object that accepts the message) and the string passed as a parameter. Isequaltostring returns a bool value (yes or no) to indicate whether the two values are the same.
-(BOOL) isequaltostring: (NSString *) astring
NSString*thing1 =@ "Hello world!";
NSString*thing2 = [NSString stringWithFormat:@ "Hello world!"];
if([thing1 isequaltostring:thing2]) {
NSLog (@ "They is the same!");
}
Note: Compare two strings to use isequaltostring instead of simply using = = to determine
If you compare two strings, you can use the Compare method
-(Nscomparisonresult) Compare: (NSString *) astring;
compare to compare two strings and return a Nscomparisonresult object, Nscomparisonresult described below
enum {
nsorderedascending =-1,
Nsorderedsame,
Nsordereddescending
};
typedef NsintegerNscomparisonresult;

For example: [@ "Tron" compare:@ "Tom"] will return Nsorderedascending (the former length is greater than the latter) [@ "genious" Compare @ "Brillient"] will return Nsordereddescend ING (the former length is less than the latter) [@ "AAA" Compare @ "AAA"] will return Nsorderedsame (both lengths equal)
Case-insensitive comparison method:-(Nscomparisonresult) Compare: (NSString *) astring option: (nsstringcompareoptions) mask;
option is a mask code that can be used to add options tags, which are commonly used as follows Nscaseinsensitivesearch: case-insensitive Nsliteralsearch: Full comparison, case-sensitive nsnumericsearch: Compares the number of strings rather than the value of a string. Without this option, 100 would be in front of 99.
So the case-insensitive comparison is:
if([thing1 compare:thing2 Option:nscaseinsensitivesearch | Nsnumericsearch] = = Nsorderedsame) {
NSLog (@ "They match");
}

The string contains:To see if a string contains another string, or to see if some file names are draft files that begin with draft, NSString provides two methods, one starting with a string and the other ending with a string.
-(BOOL) Hasprefix: (NSString *) astring;-(BOOL) Hassuffix: (NSString *) astring;
NSString*filename =@ "Draft-chapter.pages";
if([fileName hasprefix:@ "Draft"]) {
NSLog (@ "This is a draft");
} Else if([fileName hassuffix:@ ". Pages"]) {
NSLog (@ "This is a pages");
}
to determine if the character characters has any other characters you can use rangeofstring-(Nsrange) rangeofstring: (NSString *) astring;

Variability
NSString are immutable, they are not allowed to be manipulated, strings can be compared, found, created, etc., but they cannot be added or subtracted from characters. However, Cocoa provides a subclass, nsmutablestring, but is used to change the string. + (ID) stringwithcapacity: (Nsuinteger) capacity;
This only gives Nsmutablestring a suggested capacity, and of course it can go beyond recommendations. This recommendation is an optimal value, if you have opened up the space of 40M, then the subsequent operation will be very fast. You can create a mutable string as follows
nsmutablestring*string = [nsmutablestring stringwithcapacity: the];

Once you have a mutable string, you can perform various operations on the resulting string
-(void) appendString: (NSString *) astring;-(void) AppendFormat: (NSString *) format,...;
AppendString: Accepts the astring parameter and then copies it to the end of the accepted object. AppendFormat: Works like stringWithFormat, but does not create a string, but instead adds the formatted string to the end of the string.
nsmutablestring*string = [nsmutablestring stringwithcapacity: -];
[String appendString:@ "Hello world!"];
[String AppendFormat:@ "I am%d years old", at]

The result string is assigned a value of
Hello World! I am years old
You can use the Deletecharactersinrange method to delete characters in a string.
-(void) Deletecharacterinrange: (Nsrange *) Arange;
at this point, for example, we want to remove a friend from the Address Book Jack, then you can do so. Start by creating a friend list.
nsmutablestring*friend = [nsmutablestring stringwithcapacity: -];
[Friend AppendString:@ "Tom Mary James Evan Jack Jim"];

the next step is to find the scope of Jack's name.
Nsrangejackrange = [Friend rangeofstring:@ "Jack"];
jackrange.length++

and then we can kick Jack out of the Address Book .
[friend Deletecharactersinrange:jackrange];

as a result, Jack removed it from the Address book.
Tom Mary James Evan Jim
at this point we can replace the nsmutablestring at any time when we can use NSString. Of course stringWithFormat can also be used for nsmutablestring objects.
nsmutablestring*string = [nsmutablestring stringWithFormat:@ "joy-%d",2];

so the initial value of the string is Joy-2

iOS Learning NSString

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.