objective-c-Use of strings

Source: Internet
Author: User

typedef struct PERSON

{

int age;

Char *name;

}person;

int main (int argc, const char * argv[]) {

@autoreleasepool {

person person;

Person.name = "Little Fellow";

Person.age = 20;

Person Liusanjie;

Liusanjie.name = "Liu Sanjie";

Liusanjie.age = 45;

/*

1, the string is divided into variable strings and immutable strings;

2, the initialization of the string method;

3, the string is not a container class, can not add data;

*/

NSString *string = [[NSString alloc]init];

NSString *string1 = [NSString string];

NSString *string2 = [[NSString alloc]initwithstring:string1];

NSString *string3 = @ "Qwe";

int age;

NSLog (@ "%d", age);

Assigning a value to a string

string = @ "120";

/ * processing of strings;

1, the format of the string processing;

*/

Age = 20;

NSString *name = @ "Xiaozhuang";

NSString *info = [NSString stringwithformat:@ "%@ is%d years old", Name,age];

NSLog (@ "%@", info);

/*

A string that intercepts that position;

where to intercept;

Where to start the interception;

*/

1, from which position to start intercepting the string;

NSString *newstring = [info substringfromindex:4];

NSLog (@ "%@", newstring);

Practice

NSString *string4 = @ "Hoe wo day copse, sweat wo xia Tu, ... ";

NSString *newstring2 = [info substringfromindex:10];

NSLog (@ "%@", [String4 substringfromindex:12]);

2, intercept the string to that position;

NSLog (@ "%@", [String4 substringtoindex:12]);

NSLog (@ "%@", [String4 substringtoindex:6]);

3, from where to start to where the end of the position;

Nsrange range;

Range.location = 5;

On the basis of this position, continue to count several lengths;

Range.length = 7;

The length of the continuation number;

NSLog (@ "%@", [String4 Substringwithrange:range]);

Nsrange Jia;

Jia.length = 5;

Jia.location = 8;

NSLog (@ "%@", [String4 Substringwithrange:jia]);

Divides a string into an array by a specified character;

Nsarray *messagelist = [String4 componentsseparatedbystring:@ ","];

Note: The comma inside "," must be the same as the comma inside the character to be split

NSLog (@ "%@", messagelist[0]);

for (id obj in messagelist) {

NSLog (@ "%@\n", obj);

}

NSString *ni = @ "The moon in front of the bed light, staring at the ground frost, Jutou look at the moon, think Home";

Nsarray *messagelist1 = [ni componentsseparatedbystring:@ ";];

NSLog (@ "%@", a);

For (ID think home in messageList1) {

NSLog (@ "%@\n", think home);

}

Convert all the English letters to uppercase;

NSString *english = @ "I study english!do you know";

NSLog (@ "%@", english.uppercasestring);

Uppercase converted to lowercase;

NSString *english1 = @ "I STUDY english! Do you KNOW ";

NSLog (@ "%@", english1.lowercasestring);

Convert to first letter capitalization;

NSLog (@ "%@", english.capitalizedstring);

The first letter of an English word is capitalized;

NSLog (@ "%@", 中文版);

stitching strings;

NSString *string5 = @ "I";

NSString *string6 = @ "hit";

NSString *string7 = @ "You";

NSString *q = [String5 Stringbyappendingstring:string6];

NSLog (@ "%@\n", Q);

NSString *w = [String5 stringbyappendingformat:@ "want to hit%@", String7];

NSLog (@ "%@", W);

/* Concatenation of immutable strings, is the original string based on the concatenation of a string to generate another new string;

The two methods of the concatenation of immutable string strings will generate a new string;

*/

query string;

Determines whether a string is contained within a string;

NSString *link = @ "Zxcbvnmkajsldhfgquwyeireotpy";

Nsrange range1 = [link rangeofstring:@ "DSA"];

NSLog (@ "%ld", Nsnotfound);

if (Range1.location!=nsnotfound) {

NSLog (@ "%@", link);

}else

NSLog (@ "no query");

/*

(Range.location!=nsnotfound) to determine whether it exists,

*/

The string begins with what;

What is contained in the header of the string;

if ([link hasprefix:@ "Zxc"]!=no) {

NSLog (@ "exists");

}else

NSLog (@ "error");

The string ends with what;

if ([link hassuffix:@ "HJ"]!=no) {

NSLog (@ "yes");

}

Else

NSLog (@ "no");

Variable string;

Replace

nsmutablestring *mustring = [[nsmutablestring alloc]initwithstring:@ "Bruse"];

Nsrange Range3;

Range3.location = 1;

Range3.length = 4;//replaces four lengths from the second position;

Replaces the string in the specified position with another string;

[Mustring replacecharactersinrange:range3 withstring:@ "Lood"];

NSLog (@ "%@", mustring);

#pragma mark-------variable string concatenation--------------

/*1, a string is added on the basis of the original string;

2, the use of variable strings, expanded out of two methods;

3. Synthesize a string (or an object of the original string) with the original string

*/

#pragma mark--------Exercise 2---------------------

nsmutablestring *ver = [[nsmutablestring alloc]initwithstring:@] I am a programmer! "];

Nsrange Range4;

Range4.location = 4;

Range4.length = 3;

[Ver replacecharactersinrange:range4 withstring:@ "Doctor"];

NSLog (@ "%@", Ver);

Deletes the information at the specified location;

Nsrange Delect;

delect.location = 0;

Delect.length = 4;

[Ver Deletecharactersinrange:delect];

NSLog (@ "%@", Ver);

Nsmutabledictionary *mud = [Nsmutabledictionary dictionary];

Nsdictionary *dic = @{@ "Ewq": @ "FRS" @ "Tre": @ "Gfdsh"};

[Mud setobject:@ "WEW" forkey:@ "DFSF"];

[Mud setobject:dic forkey:@ "qwe"];

NSLog (@ "%@", mud);

}

return 0;

}

objective-c-Use of strings

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.