IOS nssting about finding, intercepting, splitting, replacing, verifying, changing case, encoding and decoding of strings

Source: Internet
Author: User
Tags lowercase

1, find

Nsstring*str = @ "BFJFNNFKANFKNF";

Nsrange range = [str rangeofstring:@ "111"];

if (range.location = = Nsnotfound)

{

NSLog (@ "not found in STR 111");

}else {

NSLog (@ "Search range%@", Nsstringfromrange (range));

}


The following method is used later in the ios8.0 and is also judged to include a string

BOOL result = [@ "123" containsstring:@ "2"]; 2. Interception

NSSTRING*BSTR = [str substringwithrange:nsmakerange (loc,2)];

The LOC represents the beginning of the first few characters, 2 means intercepting two characters;

to intercept n characters starting from the first character: [str substringtoindex:n]; intercept from the first n+1 to the end of the string: [Str substringfromindex:n];
3, Split

Nsstring*string =@ "Facing the Sea/spring";

Nsarray*array = [string componentsseparatedbystring:@ '/'];//an array of 2 elements separated from characters/

NSLog (@ "array1 = =%@", [array firstobject]);

NSLog (@ "array2 = =%@", [array lastobject]); Print Result: Array1 = face toward the sea, Array2 = Spring 4. Replace

Nsstring*str =@ "12345678";

NSSTRING*REPLACEDSTR = [str stringbyreplacingoccurrencesofstring:@ "345" withstring:@ "string to be replaced"];

NSLog (@ "REPLACEDSTR = =%@", replacedstr);

Print results:

Replacedstr = = 12 you want to replace the string 678 5. Verify

NSSTRING*STR1 =@ "http:www.baidu.com";

BOOL a = [str1 hasprefix:@ "http"];//whether to start with HTTP

BOOL B = [str1 hassuffix:@ "com"];//file path ends with COM

NSLog (@ "A =====%d, b = =%d", a,b);

Print results:

A ===== 1, b = = 1 6. Turn into numbers

[Str boolvalue];

[Str IntegerValue]; 7. Change the case of a string

nsstring* str2 =@ "BeiJING";

Nsstring*da = [str2 uppercasestring];//converted to uppercase

Nsstring*xiao = [str2 lowercasestring];//to lowercase

Nsstring*shouda = [str2 capitalizedstring];//first letter uppercase, remaining lowercase

NSLog (@ "\ n Capital ==%@\n lowercase = =%@\n First letter uppercase Other lowercase = =%@", Da,xiao,shouda);

Print results:

Capital ==beijing

lowercase = = Beijing

First letter Uppercase Remaining lowercase = = Beijing 8. Encode, decode

Coding:

Nsstring*hstr =@ "Hello";

nsstring*hstring = [Hstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

NSLog (@ "hstring = =%@", hstring);


Decoding:

NSSTRING*STR3 =@ "\u5982\u4f55\u8054\u7cfb\u5ba2\u670d\u4eba\u5458\uff1f";

NSSTRING*STR5 = [str3stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

NSLog (@ "string = =%@", STR5);

Print results:

hstring = =%e4%bd%a0%e5%a5%bd%e5%95%8a

String = = How to contact customer service personnel.


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.