Objective-C [NSString-string Comparison & amp; prefix and suffix check and search], objectivecnsstring

Source: Internet
Author: User

Objective-C [NSString-string Comparison & Pre-suffix check and search], objectivecnsstring

-------------------------------------------
NSString comparison

# Import <Foundation/Foundation. h>

Void test1 ()
{
// Compare the string size
// First, we define two strings.
NSString * str1 = @ "abc ";
NSString * str2 = @ "Acd ";

// We need to use the compare method to compare strings (this is an object method called by a string instance object, which is precise to each character when comparing strings. Note: Compare ASCII values)
NSComparisonResult result1 = [str1 compare: str2];
// We are not very familiar with this method at present. We do not know the type of the return value of this method. If we call this method, we cannot determine who is the same as the two strings. So Let's click to see what's at the bottom.

//-(NSComparisonResult) compare :( NSString *) string; we can see this at the bottom layer. Obviously, the returned value is of the NSComparisonResult type.

// Then click this type, and we find that:
// Typedef NS_ENUM (NSInteger, NSComparisonResult) {NSOrderedAscending =-1L, NSOrderedSame, NSOrderedDescending };

// In fact, The NSComparisonResult type is an enumeration type, and there are three return values: NSOrderedAscending (ascending), NSOrderedSame (equal), NSOrderedDescending (descending)
// Let me explain the three return types:
// ① NSOrderedAscending: ascending, that is, the first small, followed by a large
// ② NSOrderedSame: equal, that is, two strings are equal
// ③ NSOrderedDescending: descending order, that is, the front is large and the back is small

// After understanding the compare method, we can continue to judge:
Switch (result1 ){
Case NSOrderedAscending:
NSLog (@ "str1 <str2 ");
Break;
Case NSOrderedDescending:
NSLog (@ "str1> str2 ");
Break;
Case NSOrderedSame:
NSLog (@ "str1 = str2 ");
Break;

Default:
Break;
}

}

Void test2 ()
{
// When comparing the string size, add conditions (for example, Case Insensitive)
NSString * str3 = @ "abc ";
NSString * str4 = @ "ABC ";

NSComparisonResult result2 = [str3 compare: str4 options: NSCaseInsensitiveSearch];

Switch (result2 ){
Case NSOrderedAscending:
NSLog (@ "str1 <str2 ");
Break;
Case NSOrderedDescending:
NSLog (@ "str1> str2 ");
Break;
Case NSOrderedSame:
NSLog (@ "str1 = str2 ");
Break;

Default:
Break;
}

// NSCaseInsensitiveSearch case-insensitive comparison
// NSLiteralSearch for full comparison (default attribute)
// You can add many additional attributes for comparison.
}

Int main (int argc, const char * argv []) {
@ Autoreleasepool {

// Compare whether two strings are equal
// We cannot use if (str1 = str2) when comparing two strings for equality. This is completely incorrect. We need to use another method isEqualToString.
NSString * str5 = @ "abc ";
NSString * str6 = @ "abc ";

// The Return Value of isEqualToString is of the BOOL type. If 1 is returned, the return value is equal. If 0 is returned, the return value is not equal.
If ([str5 isEqualToString: str6]) {
NSLog (@ "equal ");
}
Else
{
NSLog (@ "not equal ");
}
}
Return 0;
}


-------------------------------------------
NSString pre-suffix check and search

# Import <Foundation/Foundation. h>

Void test1 ()
{
// Check whether the prefix and suffix of the string are correct

// Check whether the prefix meets the condition hasPrefix
NSString * str1 = @ "https: // saaddaddaddfgga ";

If ([str1 hasPrefix: @ "http: //"] | [str1 hasPrefix: @ "https: //"]) {
NSLog (@ "prefix ");
}
Else
{
NSLog (@ "not compliant ");
}


// Check whether the suffix meets the condition hasSuffix
NSString * str2 = @ "asfa. jp1g ";

// We can determine multiple suffixes to see if the given string matches (of course, the prefix can also determine multiple suffixes at the same time)
If ([str2 hasSuffix: @ ". jpg"] | [str2 hasSuffix: @ ". avi"] | [str2 hasSuffix: @ ". gif"]) {
NSLog (@ "compliant with the suffix ");
}
Else
{
NSLog (@ "not compliant ");
}
}

Void test2 ()
{
// Forward query
// Search for the position of a short string in str2 (and output the position and length of the substring in str2)
// Note: Here we are looking for a substring of a string.

NSString * str2 = @ "La 4234324 la 1231122342 Wang zhongyao la ";

Nsange qqqq = [str2 rangeOfString: @ "Wang zhongyao"]; // you do not need to add * to declare an Instance Object of the enumeration type. If * is added, it becomes the address of this object, obviously not.
//-(Nsange) rangeOfString :( NSString *) aString;
// In the annotation, we can know that the return value of the rangeOfString method is nsange.

// What is nsange?

// Typedef struct _ nsange {
// NSUInteger location;
// NSUInteger length;
//} Nsange;

// Obviously, this is an enumeration type. The returned value is the position and length of the substring in str2.

// RangeOfString is used to find the position where the substring itcast first appeared in str.
// If you can find a string, return the position information of the substring.
// If no value is found
// The location is a very large number
// Length 0
NSLog (@ "location: % ld, length: % ld", qqqq. location, qqqq. length );
}

Int main (int argc, const char * argv []) {
@ Autoreleasepool {

// When searching, we often use NSNotFound to determine whether this string is found.
// NSNotFound is a # define NSIntegerMax LONG_MAX (that is, an unsigned long integer)
// This is equivalent to a bottleneck defined by the system. If the limit is exceeded, an error is reported.
NSString * str = @ "fafaffaitcawwwstsdfasffa ";

Nsange qq = [str rangeOfString: @ "itcast"];

If (qq. location! = NSNotFound) {// In fact, if the position of the substring in this string is not infinitely large, it must be the same. If it is infinitely large, it will definitely go outside, I cannot find it ~
NSLog (@ "search successful ");
}
Else
{
NSLog (@ "wrong! ");
}
}
Return 0;

}


-------------------------------------------

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.