All actions for iOS immutable strings

Source: Internet
Author: User

The code can be copied directly to run a look at the results, easy to understand

NSString

OC string cannot be output with printf

But unfortunately OC does not have a way to read data from the terminal, it needs to use scanf to read the C string and then convert it to OC string

#pragma the Mark character class

Create an immutable string str1 value is a constant string

Print string to print with%@

%@ can also print objects

NSString * str1 = @ "constant string";

NSLog (@ "%@", str1);

Create a string with a string

NSString * str2 = [[NSString ALLOC]INITWITHSTRING:STR1];

NSLog (@ "%@", str2);

//Use string formatting to initialize to create a string

Api

NSString * STR3 = [[NSString alloc]initwithformat:@ "%@%d%f", @ "abc", 123,12.3];

NSLog (@ "%@", STR3);

Create a formatted string with the string's class method

NSString * STR4 = [NSString stringwithformat:@ "%@%d", @ "Def", 456];

NSLog (@ "%@", STR4);

#pragma mark converts C-language strings to OC strings

The problem with string encoding is nsutf8stringencoding in OC as long as it is encountered

Universal Code Unicode

NSString * STR5 = [[NSString alloc]initwithcstring: "ABC" encoding:nsutf8stringencoding];

NSLog (@ "%@", STR5);

char * p = "ABC";

NSString * Str5_1 = [[NSString alloc]initwithutf8string:p];

NSLog (@ "%@", Str5_1);

class method to convert C string

NSString * Str5_2 = [NSString stringwithcstring:p encoding:nsutf8stringencoding];

NSString * Str5_3 = [NSString stringwithutf8string:p];

NSLog (@ "%@%@", Str5_2, Str5_3);

#pragma the mark string comparison

Judging a string cannot be judged by = =

In the design of NSString, the internal application of an optimized idea, the idea of sharing meta-design

In the design of the string, there will be a buffer pool, as long as the buffer pool has the current string, then it will return the address of the current string, not to repeat the creation, because a lot of the creation of the string will consume memory, so mentioned this way, When we use stringWithFormat to recreate a new string, we cannot use the = = number to judge

In the judgment string = = Determines whether the pointer address of the string is equal, there is a certain risk of error.

In the actual development can not be used = = instead of using a special method to judge the string isequaltostring

NSString * STR6 = @ "AB";

NSString * Str6_1 = @ "Hello";

Receive return value

BOOL IsOK = [STR6 isequaltostring:str6_1];

if ([STR6 Isequaltostring:str6_1]) {

NSLog (@ "%@ = =%@", str6,str6_1);

}else{

NSLog (@ "%@! =%@", str6,str6_1);

}

Gets the length of the string

The length of any letter, number, or Chinese character in NSString is 1

NSLog (@ "%@ is%ld", STR6,[STR6 length]);

//@ "ABCDE";

The position of the string is counted starting from 0.

A kanji account of 2 bytes

NSString * STR7 = @ "Xcode";

//Get a single character

Unichar is a two-byte char

The position of the string is counted starting from 0.

Unichar Newchar = [Str7 characteratindex:4];

NSLog (@ "Newchar =%c", Newchar);

Chinese Characters "%c" in//nsstring

Unichar Hanzi = [Str6_1 characteratindex:0];

NSLog (@ "Hanzi =%c", Hanzi);

//Compare the size of a two string

NSString * str8 = @ "Yang Chang";

NSString * str8_1 = @ "B Duan";

Nscomparisonresult result =[str8 Compare:str8_1];

if (result = = Nsorderedsame) {

NSLog (@ "%@ =%@", str8, Str8_1);

//Ascending the former small latter large

}else if (result = = nsorderedascending) {

NSLog (@ "%@ <%@", str8,str8_1);

//Descending the former large latter small

}else if (result = = nsordereddescending) {

NSLog (@ "%@ >%@", str8,str8_1);

}

Stitching #pragma the Mark string

NSString * STR9 = @ "Beijing Beijing";

NSString * Str9_1 = @ "I am the most handsome";

//str9_1 Stitching to the back of STR9

NSString * str9_2 = [STR9 stringbyappendingstring:str9_1];

NSLog (@ "%@", str9_2);

//Format stitching at the same time as the initialization of the string

Str9_2 = [Str9 stringbyappendingformat:@ "%@%d%f", str9_1,520,520.0];

NSLog (@ "%@", str9_2);

//How to traverse every character in a string

NSString * Str10 = @ "ABCDEFGH";

for (int i = 0; I<[str10 length];i++) {

char c = [Str10 characteratindex:i];

NSLog (@ "%d =%c", i,c);

}

Find #pragma the Mark string

NSString * Str11 = @ "Hello Beijing 123";

find the 123 string in the current string str11, returning his position and length

Nsrange range = [Str11 rangeofstring:@ "123"];

NSLog (@ "loc =%lu, Len =%lu", range.location,range.length);

/**

typedef struct _NSRANGE {

Nsuinteger location;//This is the position is not the length!!!!!

Nsuinteger length;//This is the length is not the position!!!

} Nsrange;

*/

Determine if the current string exists?

if (range.location! = nsnotfound) {

NSLog (@ "123 in%@ position is%lu", str11,range.location);

}else{

NSLog (@ "123 no longer string");

}

if (range.length! = 0) {

NSLog (@ "123 in%@ length is%lu", str11,range.length);

}else{

NSLog (@ "not");

}

Interception of #pragma mark string

NSString * Str12 = @ "Welcome to Beijing, iOS getting started or easier ios";

is to intercept a substring from the string str12 (from the No. 0 character of the original string to the position of the passed [value-1])

NSString * Str12_1 = [Str12 substringtoindex:5];

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

//String str12 the end of the string at the specified position 6

NSString * str12_2 = [Str12 substringfromindex:6];

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

//Find a substring in the original string with a range of known ranges

The first one is the location

The second one is the length

Nsrange Range2 = {2,4};

NSLog (@ "%@", [Str12 substringwithrange:range2]);

//Str12 The characters in the specified range with the @ "study hard" replace the original string

NSString * Str12_3 = [Str12 stringbyreplacingcharactersinrange:nsmakerange (0, 9) withstring:@ "study Hard"];

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

#pragma mark to judge prefixes and suffixes

NSString * Str13 = @ "http://www.baidu.com";

//Check whether a string starts with the specified string (prefix)

if ([Str13 hasprefix:@ "http"]) {

NSLog (@ "yes");

}else{

NSLog (@ "not");

}

//Check suffix

if ([Str13 hassuffix:@ "com"]) {

NSLog (@ "yes");

}else{

NSLog (@ "not");

}

#pragma the conversion of the Mark string

NSString * str14 = @ "abc";

//Convert Str14 to uppercase

Str14 = [Str14 uppercasestring];

NSLog (@ "%@", str14);

//Str14 converted to lowercase

Str14 = [Str14 lowercasestring];

NSLog (@ "%@", str14);

//Convert a basic type string

stringWithFormat

Initwithformat

Convert a string to a base data type

NSString * Str14_1 = @ "1";

int a = [str14_1 intvalue];

Floatvalue

Doublevalue

NSString * Str14_2 = @ "YES";

BOOL IsOK = [Str14_2 boolvalue];

//boolvalue

NSLog (@ "%d", a);

//@ "1"

#pragma the split of the mark string

NSString * Str15 = @ "A,b,c,d";

//Use the specified delimiter to divide the string into arrays

Is+-n*/io=pp

Nsarray * array = [Str15 componentsseparatedbystring:@ ","];

NSLog (@ "%@", array);

for (int i= 0; I<[array count];i++) {

NSLog (@ "%@", [array objectatindex:i]);

//    }

#pragma mark reads a string from the specified file

Read the contents of a file into a string

The first parameter is the path to a file

Second character encoding

A third error message

NSString * str16 = [NSString stringwithcontentsoffile:@ "/users/hhh/desktop/github.txt" encoding:nsutf8stringencoding Error:nil];

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

//Read the contents of the file from the Internet into a string

First URL

Second character encoding

A third error message

NSString * Str17 = [NSString stringwithcontentsofurl:[nsurl urlwithstring:@ "Http://10.0.178.5/resource/github.txt"] Encoding:nsutf8stringencoding Error:nil];

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

//write file

The first one is the destination file path

Whether the second write cache (atomic)

A third encoding

Fourth error message

If there is no file, then a new file will be created, if the original file already exists, then the original file will be overwritten

[Str12 writetofile:@ "/users/hhh/desktop/aa.txt" Atomically:yes encoding:nsutf8stringencoding Error:nil];

All actions for iOS immutable strings

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.