Common types of objective-C Foundation 2 -- nsstring and nsmutablestring

Source: Internet
Author: User

Continue nsstring, because there are many used, so we should focus on understanding

1. Evaluate the string length

Nsstring * myname = @ "aobama"; int length = (INT) [myname length]; // It is found that the return value type is nsuinteger. If it is not strong, a warning is returned, I don't know whether strong conversion is a common method. I hope the user can tell me nslog (@ "the string length is % d", length ); if ([myname length]> 5) {nslog (@ "AAAAA"); // print AAAAA}

2. String Conversion

Change case sensitivity

Nsstring * string = @ "where are you, boy! "; // All capital nslog (@" % @ ", [String uppercasestring]); // print where are you, boy! // All lowercase nslog (@ "% @", [String lowercasestring]); // print where are you, boy! // All words are capitalized, and other letters are lowercase nslog (@ "% @", [String capitalizedstring]); // print where are you, boy!

Converts a string to a basic data type.

Nsstring * aboutfloat = @ "3.14"; nsstring * aboutbool = @ "no"; nslog (@ "% F", [aboutfloat floatvalue]); // print 3.140000 nslog (@ "% d", [aboutbool boolvalue]); // print 0 // convert float type to integer nslog (@ "% d ", [aboutfloat intvalue]); // print 3

Combines two strings into one string.

// Method 1 nsstring * str1 = @ "hello"; nsstring * str2 = @ "world"; nsstring * str3 = [[nsstring alloc] initwithformat: @ "% @", str1, str2]; nslog (@ "the result of merging str1 and str2 into str3 is: % @", str3); // print the result: the result of merging str1 and str2 into str3 is: Hello World // method 2 nsstring * str4 = [str1 stringbyappendingstring: str2]; nslog (@ "the result of merging str1 and str2 into str4 is: % @", str4); // print the result: the result of merging str1 and str2 into str4 is: hello World // connect multiple strings nsstring * str5 = [str1 stringbyappendingformat: @ "% @", str1, str2, str3]; nslog (@ "str5: % @ ", str5); // print the result: str5 is hellohello worldhello world.

3. Whether to start with a string hasprefix: whether to end with a string hassuffix

Nsstring * str6 = @ "who are you"; bool isstart1 = [str6 hasprefix: @ "Wh"]; bool isstart2 = [str6 hasprefix: @ "H"]; bool isend1 = [str6 hassuffix: @ "U"]; bool isend2 = [str6 hassuffix: @ "O"]; nslog (@ "Result: % d ,,, % d, % d, % d ", isstart1, isstart2, isend1, isend2); // result: 1, 0, 1, 0

4. truncate strings into arrays according to rules

Nsstring * str7 = @ "You/have/A/baby"; nsarray * array = [str7 componentsseparatedbystring: @ "/"]; nslog (@ "% @", array ); // The array can be directly printed without traversing/* print the result (you, have, A, baby )*/

5. String Truncation

Nsstring * str8 = [str7 substringfromindex: 2]; // The Block starting from the second position includes 2. Of course, nslog starting with 0 (@ "% @", str8 ); // U/have/A/baby nsstring * str9 = [str7 substringtoindex: 5]; // from the start to the specified position, but does not include this location nslog (@ "% @", str9); // you/h // intercept the nsange range = nsmakerange (2, 3) According to the range ); // define the range first. You can also define the nsange range = {2, 3}; nslog (@ "location .. % LD length... % lD ", range. location, range. length); // location .. 2 length... 3 range. location = 3; // includes 2 range. length = 4; // nslog (@ "location .. % LD length... % lD ", range. location, range. length); // location .. 3 length... 4 nsstring * str10 = [str7 substringwithrange: range]; nslog (@ "% @", str10); // hav

6. String Query

NSRange range2 = [str7 rangeOfString:@"have"];        NSRange range3 = [str7 rangeOfString:@"aaa"];                NSLog(@"location1...%ld",range2.location);        NSLog(@"location2...%ld",range3.location);

Print result:

Location1. .. 4

Location2... 9223372036854775807

The search result can be determined based on the position of the source string to be queried.

9223372036854775807 is the value of nsintegermax

So we can do this in our judgment.

If (range3 = nsintegermax)

{

Nslog (@ "this string contains ");

}

About nsmutablestring (variable string)

Nsmutablestring inherits from nsstring, so all methods in nsstring apply to it.

1. Insert a string

NSMutableString *str = [[NSMutableString alloc]initWithFormat:@"hello"];        [str insertString:@"wwwwwww" atIndex:3];                NSLog(@"%@",str);

Print result:

Helwwwwwlo


2. delete a string

[str deleteCharactersInRange:NSMakeRange(1, 4)];        NSLog(@"%@",str);

Print result:

Hwwwwwlo

3. Replace string

[str replaceCharactersInRange:NSMakeRange(1, 3) withString:@"world"];        NSLog(@"%@",str);

Print result:

Hworldwwlo


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.