Small white Learning Development (IOS) OC_ string comparison judgment (2015-08-12)

Source: Internet
Author: User

//

Main.m

// string comparison judgment

//

Created by admin on 15/8/13.

Copyright (c) years admin. All rights reserved.

//


#import <Foundation/Foundation.h>


int Main (int argc,const Char * argv[]) {

@autoreleasepool {

//1. Determines whether a string is empty

nsstring *str = @ "";

if (str = = Nil | | str.length = =0) {

NSLog (@ " string is empty ");

}

//2. Determines whether a string starts with the specified content

nsstring *str1 = @ "www.baidu.com";

BOOL isperfix = [str1 hasprefix:@ "RWW"]; //Perfix prefix

if (Isperfix) {

NSLog (@ " starts with ww ");

}Else

{

NSLog (@ " start error ");

}

NSLog (@ "Hasperfix%@", Isperfix? @ "YES": @ "NO"); optimization of the above judgment

//3. Determines whether a string ends with the specified content

In the development of commonly used in judging file formats such as:. txt. AVI,.RMVB mkv Doc mp3,mp4 PDF

BOOL issuffix = [str1 hassuffix:@ "com"]; //suffix suffix

NSLog(@ "Hassuffix%@", Issuffix? @ "YES" : @ "NO");

/ * 4. Determine if two strings are equal

Note:

> uses = = To determine two strings, in fact, to determine whether the address of two strings is equal

> If the addresses are equal, then the strings are equal

> However, in the actual development, remember not to use = = To determine the two strings of equal

> to determine if two strings are equal , you must use the method that NSString provides to us

Isequaltostring

*/

nsstring *str2 = @ "ABCDEFGH";

nsstring *STR3 = @ "abcdeeee";

BOOL isequal = [str2 isequaltostring: STR3];

NSLog (@ "IsEqual%@", isequal?) @ "YES" : @ "NO");

/*

5. Isequaltostring 's enhanced version compare

> Compare compares ASCII values

> The return value is an enumeration

*/

nscomparisonresult result = [str2 compare: STR3];

Switch (Result) {

casensorderedascending:

NSLog(@ "nsorderedascending");

break;

Case nsorderedsame:

NSLog (@ "Nsorderedsame");

break;

casensordereddescending:

NSLog(@ "nsordereddescending");

break;

default:

break;

}

}

return 0;

}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Small white Learning Development (IOS) OC_ string comparison judgment (2015-08-12)

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.