NSString and iosnsstring for IOS Learning

Source: Internet
Author: User

NSString and iosnsstring for IOS Learning
The range of nsange is used to indicate transaction-related matters.
Typeof struct _ nsange {
Unsigned localtion;
Unsigned length;
} Nsange;

Localtion indicates the character range in the character or the actual range of array elements, and length indicates the number of elements contained in the string. For example, cool in Objective-c is a cool language. can be expressed by the range of location17 and length4, And the location can also be expressed by NSNotFound to indicate that the value does not represent the range. There are three methods to create the nsange: one is to directly assign a value to the string "nsange range;
Range. location = 17;
Range. length = 4
Method 2: Use the aggregation structure Assignment Mechanism of C language: nsage range = {17,4 };
Method 3: Use a shortcut function NSMakeRange () nsange range = NSMakeRange (17,4) provided by Cocoa );
The advantage of NSMakeRange () is that it can be used in any function, for example, passed as a parameter: [anObject flarbulateWithrange: NSMakeRange (17, 4)];
NSString syntax is to add the @ symbol before double quotation marks, for example, @ "Hi ~"
Create a string: stringWithFormat
+ (Id) stringWithFormat :( NSString *) format ,...;
NSString * str;
Str = [NSString stringWithFormat: @ "I am % d years old and my wight is % d Kg.", 23,65];
The result is: I am 23 years old and my weight is 65 kg.
StringWithFormat is a class method.
String length: length
-(NSUInteger) length
(The difference between NSUInteger and NSInteger is that holding is an unsigned integer, and the latter is a signed integer .)
You can use NSUInteger len = [str length] in this way.
Alternatively, if ([str length]> 35 ){
NSLog (@ "Wow, this string is really tall! ")
}

String comparison: isw.tostring
IsEqualToString is used to compare the receiver (recever, the object that receives the message) and the string passed as the 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 isw.tostring: thing2]) {
NSLog (@ "They are the same! ");
}
Note: To compare two strings, use isEqualToString instead of ==.
You can use the compare method to compare two strings.
-(NSComparisonResult) compare :( NSString *) aString;
Compare to compare two strings and return an NSComparisonResult object. The NSComparisonResult is described as follows:
Enum {
NSOrderedAscending =-1,
NSOrderedSame,
NSOrderedDescending
};
Typedef NSInteger NSComparisonResult;

For example, [@ "Tron" compare: @ "Tom"] will return NSOrderedAscending (the former is longer than the latter) [@ "genious" compare @ "brillient"] NSOrderedDescending (the former length is smaller than the latter) [@ "aaa" compare @ "aaa"] NSOrderedSame (the two are of the same length) will be returned)
Case-insensitive comparison:-(NSComparisonResult) compare :( NSString *) aString option :( NSStringCompareOptions) mask;
Option is a hidden code. You can use | to add option tags. Common options are as follows: NSCaseInsensitiveSearch: case-insensitive NSLiteralSearch: full comparison, case-sensitive NSNumericSearch: compares the number of strings instead of the string value. If this option is not available, 100 will be placed before 99.
So case-insensitive comparison:
If ([thing1 compare: thing2 option: NSCaseInsensitiveSearch | NSNumericSearch] = NSOrderedSame ){
NSLog (@ "They match ");
}

String inclusion: Check whether the string contains another string or whether some file names start with draft. NSString provides two methods, the other end 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 whether there are other characters in the character, you can use rangeOfString-(nsange) rangeOfString :( NSString *) aString;

Variability
NSString is immutable. It is not impossible to operate on them. It can be compared, searched, created, and so on, but cannot be increased or decreased. Cocoa provides a subclass NSMutableString, which is used to change the string. + (Id) stringWithCapacity :( NSUInteger) capacity;
This only provides a recommended capacity for NSMutableString. Of course, the recommended capacity can be exceeded. This recommendation is an optimal value. If 40 m space has been opened up, the subsequent operations will be fast. You can create a variable string as follows:
NSMutableString * string = [NSMutableString stringWithCapacity: 42];

Once a variable string is available, various operations can be performed on the generated string.
-(Void) appendString :( NSString *) aString;-(void) appendFormat :( NSString *) format ,...;
AppendString: accept the aString parameter and copy it to the end of the receiving object. AppendFormat: works in the same way as stringWithFormat, but does not create a string. Instead, it adds the formatted string to the end of the string.
NSMutableString * string = [NSMutableString stringWithCapacity: 50];
[String appendString: @ "Hello World! "];
[String appendFormat: @ "I am % d years old", 23]

Result string is assigned
Hello World! I am 23 years old
You can use the deleteCharactersInrange method to delete characters in a string.
-(Void) deletecharacterInRange :( nsange *) aRange;
For example, if we want to delete a friend Jack from the address book, we can do this. First, create a friend list.
NSMutableString * friend = [NSMutableString stringWithCapacity: 50];
[Friend appendString: @ "Tom Mary James Evan Jack Jim"];

Next, find the range of Jack's name.
Nsange jackRange = [friend rangeOfString: @ "Jack"];
JackRange. length ++

Then we can kill Jack from the address book.
[Friend deleteCharactersInRange: jackRange];

In the result, Jack is deleted from the address book.
Tom Mary James Evan Jim
In this case, we can replace NSString with NSMutableString at any time. Of course, stringWithFormat can also be used with the NSMutableString object.
NSMutableString * string = [NSMutableString stringWithFormat: @ "Joy-% d", 2];

In this case, the initial value of string is Joy-2.
In iOS, how does one compare the Chinese characters in NSString?

NSString * chnString = @ "Chinese, Chinese ";
If ([chnString isEqualToString: @ "Chinese, Chinese"])
{NSLog (@ "Equal ");}
Else NSLog (@ "NOT Equal ");
If ([chnString isw.tostring: @ "Chinese, Chinese"]) NSLog (@ "Equal ");
Else NSLog (@ "NOT Equal ");

In iOS, how does one compare the Chinese characters in NSString?

Compare:
 

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.