String NSString in iOS

Source: Internet
Author: User

String NSString in iOS
Create a String object:

NSstring * str1 = @ "hello world ";
NSString * str = [[NSString alloc] initWithString: @ "hello world! "];
NSString * str_1 = [[NSString alloc] initwithuf8string: "hello world"]; // converts a C string to an OC string.
Int a = 123;

NSString * str_3 = [[NSString alloc] initWithFormat: @ "a = % d % s % @", a, "abcd", @ "efg"]; // initialize with a formatted string

 

// Concatenates strings and converts strings of C and OC.
NSString * str1 = [NSString stringWithString: @ "hello world"];
NSString * str2 = [NSString stringwithuf8string: "hello world"];
NSString * str3 = [NSString stringWithFormat: @ "% s", "hello world"];
NSstring * str1 = @ "hello ";
NSString * str = [[NSString alloc] initWithString: @ "hello world! "];

NSString * str_1 = [[NSString alloc] initwithuf8string: "hello world"]; // converts a C string to an OC string.

 

Int a = 123;

NSString * str_3 = [[NSString alloc] initWithFormat: @ "a = % d % s % @", a, "abcd", @ "efg"]; // initialize with a formatted string

 

String Conversion:

NSString * str = @ "hello ";
Const char * p = [str UTF8String]; converts an OC string to a C string.

Str = @ "123 ";
Int a = [str intValue]; // converts a numeric string to an integer.
[Str floatValue]; // converts a numeric string to the float type.
[Str doubleValue]; // converts a numeric string to the double type.
NSString * str = @ "hello ";
Const char * p = [str UTF8String]; converts an OC string to a C string.

Str = @ "123 ";
Int a = [str intValue]; // converts a numeric string to an integer.
[Str floatValue]; // converts a numeric string to the float type.

[Str doubleValue]; // converts a numeric string to the double type.

 

Common NSString:

NSLog (@ "% ld", [str1 length]); // evaluate the string length

NSLog (@ "% c", [str1 characterAtIndex: 1]); // obtain the characters in the string

BOOL ret = [str1 isEqualTo: str2]; // compares two strings for equality. If the two strings are equal, YES (1) is returned. If the two strings are not equal, NO (0) (bool yes (1) is returned) NO (0 ))

Long ret1 = [str1 compare: str2]; // compare the size of two strings. If str1 is greater than 1, return 1 equal. Return 0 Less than return-1

Long ret2 = [str1 caseInsensitiveCompare: str2]; // compare the string size case-insensitive

NSString * ptr = [str2 uppercaseString]; // converts all lowercase characters in the string to uppercase without changing the original string.

Ptr = [str1 lowercaseString]; // converts all uppercase characters in the string to lowercase without changing the original string

Ptr = [str3 capitalizedString]; // converts the first letter in the string to uppercase and lowercase letters.

NSString * str4 = @ "hello world ";

Nsange range = [str4 rangeOfString: @ "wr"]; // returns NSNotFound if no substring is found. Returns location and length.

If (range. location! = NSNotFound ){

NSLog (@ "% ld", range. location, range. length );

}

NSString * str5 = @ "helloworld ";

NSString * ptr1 = [str5 substringToIndex: 4]; // string extraction starts from subscript 0 to 4 excluding 4

NSString * ptr2 = [str5 substringFromIndex: 5]; // extracts data from subscript 5 to end of the string, including 5

Nsange range1 = {4, 3}; // structure Initialization

NSString * ptr3 = [str5 substringWithRange: range1]; // extract within the range specified

NSString * ptr4 = [str5 substringWithRange: NSMakeRange (4, 3)]; // NSMakeRange can generate a struct



NSString * str = @ "www.baidu.com ";

BOOL ret = [str hasPrefix: @ "www"]; // determines whether the string starts with a specified string.



NSString * str1 = @ "1.txt ";

BOOL ret1 = [str hasSuffix: @ ". txt"]; // determines whether the string ends with the specified string



NSMutableString variable string (dynamically increasing or decreasing) inherits all NSString methods that can be used

NSMutableString * str = [[NSMutableString alloc] initWithString: @ "hello"]; // converts an unchangeable string to a variable string

[Str insertString: @ "123" atIndex: 1]; // insert an NSString type string at the specified subscript (do not cross-border)

[Str appendString: @ "123"]; // append a string to the end of the string

[Str deleteCharactersInRange: NSMakeRange (0, 2)]; // Delete length characters from the specified subscript

[Str setString: @ "baidu"]; // you can modify a variable string to assign a value to it.

[Str replaceCharactersInRange: NSMakeRange (3, 1) withString: @ "ios"]; // Replace the length of the specified subscript with the specified string

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.