O-c study July record nsstring usage

Source: Internet
Author: User

1 Creating strings

NSString *str1 = @ "Hello";

NSString *str2 = [NSString string];

NSString *STR3 = [NSString stringwithformat:@ "Hello,%@", @ "Lin Lin"];

NSString *STR4 = [NSString stringwithcstring: "hello,mingming" encoding:nsutf8stringencoding];

NSString *STR5 = [NSString stringwithutf8string: "Hello,mingming"];

NSString *STR6 = [NSString stringwithcontentsoffile:@ "/USERS/QINGYUN/DESKTOP/TEST.M" encoding:NSUTF8StringEncoding Error:nil];//stringwithconentsoffile:encoding:error:

NSLog (@ "str1 >>>%@", str1);

NSLog (@ "str2 >>>%@", str2);

NSLog (@ "Str3 >>>%@", STR3);

NSLog (@ "STR4 >>>%@", STR4);

NSLog (@ "STR5 >>>%@", STR5);

NSLog (@ "STR6 >>>%@", STR6);

2 length

NSLog (@ "str6 ' length >>>>%ld", [STR6 length]);

3 String Comparisons

NSString *STR7 = @ "hello000";

NSString *str8 = [NSString STRINGWITHSTRING:STR7];

NSString *STR9 = [NSString stringwithformat:@ "he%@", @ "llo00000"];

NSLog (@ "STR7 >>>>>%p", STR7);

NSLog (@ "Str8 >>>>>%p", str8);

NSLog (@ "STR9 >>>>>%p", STR9);

STR7 and STR9 are two pointers, so use = = to compare values directly

if (STR7 = = STR9) {

NSLog (@ "equal");

} else {

NSLog (@ "unequal");

}

Compare String Contents

if ([Str7 ISEQUALTOSTRING:STR9]) {

NSLog (@ "equal");

} else {

NSLog (@ "unequal");

}

NSString *str10 = @ "100000";

NSString *str11 = @ "000100000";

Nscomparisonresult result = [Str10 compare:str11 options:nsnumericsearch];

NSLog (@ "result >>>%ld", result);

NSString *str12 = @ "410324198510282811";

NSString *str13 = @ "19851028";

Nscomparisonresult RESULT1 = [Str12 compare:str13 options:nscaseinsensitivesearch Range:nsmakerange (6,8)]; Options Range Category

NSLog (@ "Result1 >>>%ld", RESULT1);

4. Connection character Chuan

NSString *str14 = @ "Hello";

NSString *str15 = [Str14 stringbyappendingstring:@ "Weiliang"];//append Append, add

NSString *str16 = [@ "/users/qingyun" stringbyappendingpathcomponent:@ "Desktop"];//component components, components, elements

NSLog (@ "Str15 >>>>>%@", str15);

NSLog (@ "str16 >>>>>%@", str16);

5. String Lookup

NSString *str17 = @ "Today we learn GCD";

if ([Str17 containsstring:@ "GCD"]) {

NSLog (@ "Teenager, study hard, don't care about things you shouldn't care about.");

}//contains contains, accommodates

NSString *str18 = @ "410000xxxxxxxxxx";

if ([Str18 hasprefix:@ "41"]) {

NSLog (@ "Henan people");

}//Hasprefix: The function of a method is to determine whether the created string content starts with a prefix

NSString *str19 = @ "Icon.exe";

if ([Str19 hassuffix:@ "png"] | | [Str19 hassuffix:@ "jpg"]) {

NSLog (@ "legal Avatar");

}//hassuffix: The function of the method is to determine whether the created string content starts with a suffix

NSString *str20 = @ "~/desktop/test.h";

NSLog (@ "pathextension >>>%@", [Str20 pathextension]);//File extension Extension expand, expand

NSLog (@ "absolute path >>>%@", [Str20 Stringbyexpandingtildeinpath]); Expand Expand Tilde Wave number

NSLog (@ "absolute path >>>%@", [[Str20 Stringbyexpandingtildeinpath] stringbyabbreviatingwithtildeinpath]); Close-Up Wave number abbreviate abbreviated shortening

6 Extracting substrings

NSString *str21 = @ "Hello, Yangxu";

NSLog (@ "subString >>>%@", [Str21 substringfromindex:6]);//sub root is ' son ' intercept from sixth character

NSLog (@ "subString >>>%@", [Str21 substringtoindex:6]); Intercept Six characters

NSLog (@ "subString >>>%@", [Str21 Substringwithrange:nsmakerange (3, 5)]); Word definition intercept Range

/********* variable String *********/

nsmutablestring *mstr = [nsmutablestring stringwithcapacity:50];//capacity capacity

nsmutablestring *mstr = [nsmutablestring stringwithstring:@ "1506"];

nsmutablestring *mstr = [nsmutablestring stringwithformat:@ "1506-%@-", @ "master"];

1 Append

[Mstr appendstring:@ "Zhang"];

NSLog (@ "MSTR >>>%@", MSTR);

[Mstr appendstring:@ "Qinpei"];

NSLog (@ "MSTR >>>%@", MSTR);

2. Delete

[Mstr deletecharactersinrange:nsmakerange (0, 4)];

NSLog (@ "MSTR >>>%@", MSTR);

3. Insert

[Mstr insertstring:@ "1506" atindex:0];

NSLog (@ "MSTR >>>%@", MSTR);

4. Replace

[Mstr Replacecharactersinrange:nsmakerange (5, 4) withstring:@ "Teacher"];//replaces 4 characters with teacher starting with the fifth character

NSLog (@ "MSTR >>>%@", MSTR);

5. Formatting append

[Mstr appendformat:@ "-%@", @ "Stage1"];

NSLog (@ "MSTR >>>%@", MSTR);

2015-07-18 10:14:17.013 testnsstring[3033:385566] str1 >>> Hello

2015-07-18 10:14:17.014 testnsstring[3033:385566] str2 >>>

2015-07-18 10:14:17.014 testnsstring[3033:385566] str3 >>> Hello, Lin Lin

2015-07-18 10:14:17.014 testnsstring[3033:385566] STR4 >>> hello,mingming

2015-07-18 10:14:17.014 testnsstring[3033:385566] STR5 >>> hello,mingming

2015-07-18 10:14:17.014 testnsstring[3033:385566] STR6 >>> (NULL)

2015-07-18 10:14:17.014 testnsstring[3033:385566] str6 ' length >>>> 0

2015-07-18 10:14:17.014 testnsstring[3033:385566] STR7 >>>>> 0x1000021b0

2015-07-18 10:14:17.014 testnsstring[3033:385566] str8 >>>>> 0x1000021b0

2015-07-18 10:14:17.015 testnsstring[3033:385566] STR9 >>>>> 0x100300210

2015-07-18 10:14:17.015 testnsstring[3033:385566] Not Equal

2015-07-18 10:14:17.015 testnsstring[3033:385566] Not Equal

2015-07-18 10:14:17.015 testnsstring[3033:385566] RESULT1 >>> 0

2015-07-18 10:14:17.015 testnsstring[3033:385566] str15 >>>>> Helloweiliang

2015-07-18 10:14:17.015 testnsstring[3033:385566] str16 >>>>>/users/qingyun/desktop

2015-07-18 10:14:17.015 testnsstring[3033:385566] Boy, study hard, don't care about things you shouldn't care about. ..

2015-07-18 10:14:17.015 testnsstring[3033:385566] it's Henan.

2015-07-18 10:14:17.025 testnsstring[3033:385566] pathextension >>> H

2015-07-18 10:14:17.026 testnsstring[3033:385566] absolute path >>>/users/qingyun/desktop/test.h

2015-07-18 10:14:17.026 testnsstring[3033:385566] absolute path >>> ~/desktop/test.h

2015-07-18 10:14:17.026 testnsstring[3033:385566] subString >>> Yangxu

2015-07-18 10:14:17.026 testnsstring[3033:385566] subString >>> Hello,

2015-07-18 10:14:17.026 testnsstring[3033:385566] subString >>> lo, y

2015-07-18 10:14:17.026 testnsstring[3033:385566] MSTR >>> 1506-master-zhang

2015-07-18 10:14:17.026 testnsstring[3033:385566] MSTR >>> 1506-master-zhangqinpei

2015-07-18 10:14:17.026 testnsstring[3033:385566] MSTR >>>-master-zhangqinpei

2015-07-18 10:14:17.027 testnsstring[3033:385566] MSTR >>> 1506-master-zhangqinpei

2015-07-18 10:14:17.027 testnsstring[3033:385566] MSTR >>> 1506-teacherer-zhangqinpei

2015-07-18 10:14:17.027 testnsstring[3033:385566] MSTR >>> 1506-teacherer-zhangqinpei-stage1

O-c study July record nsstring usage

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.