The string NSString in iOS

Source: Internet
Author: User

Create a String object:

NSString * str1 = @ "Hello world";
NSString * str = [[NSString alloc]initwithstring:@ "Hello world!"];
NSString * str_1 = [[NSString alloc]initwithutf8string: "Hello World"];//convert C string to OC string
int a = 123;

NSString * str_3 = [[NSString alloc]initwithformat:@ "a =%d%s%@", A, "ABCD", @ "EFG"];//is initialized with a formatted string


Can complete the concatenation of the string and the completion of C string and OC string conversion
NSString * str1 = [nsstring stringwithstring:@ "Hello World"];
NSString * str2 = [NSString stringwithutf8string: "Hello World"];
NSString * STR3 = [nsstring stringwithformat:@ "%s", "Hello World"];
NSString * str1 = @ "Hello";
NSString * str = [[NSString alloc]initwithstring:@ "Hello world!"];

NSString * str_1 = [[NSString alloc]initwithutf8string: "Hello World"];//convert C string to OC string


int a = 123;

NSString * str_3 = [[NSString alloc]initwithformat:@ "a =%d%s%@", A, "ABCD", @ "EFG"];//is initialized with a formatted string


String Conversions:

NSString * str = @ "Hello";
const char *p = [str utf8string]; convert OC string to C string

str = @ "123";
int a = [str intvalue];//converts a digital string into integer data
[Str floatvalue];//converts a digital string into a float type
[Str doublevalue];//converts a digital string into a double type
NSString * str = @ "Hello";
const char *p = [str utf8string]; convert OC string to C string

str = @ "123";
int a = [str intvalue];//converts a digital string into integer data
[Str floatvalue];//converts a digital string into a float type

[Str doublevalue];//converts a digital string into a double type


Common nsstring:

NSLog (@ "%ld", [str1 length]); To find the string length

NSLog (@ "%c", [str1 characteratindex:1]);//Get characters in a string

BOOL ret = [str1 isequalto:str2];//compares two strings for equality, Equals returns YES (1), not equal returns No (0) (BOOL YES (1) No (0))

Long Ret1 = [str1 compare:str2];//compares the size of two strings, STR1 is greater than return 1 equals returns 0 less than return-1

Long Ret2 = [str1 caseinsensitivecompare:str2];//case-insensitive comparison string size

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

ptr = [str1 lowercasestring];//converts all uppercase characters in a string to lowercase without changing the original string

ptr = [Str3 capitalizedstring];//converts the first letter appearing in a string to uppercase, the remaining letter lowercase

NSString * STR4 = @ "Hello world";

Nsrange range = [STR4 rangeofstring:@ "WR"];//find sub-string, cannot find return Nsnotfound find return location and length

if (range.location! = nsnotfound) {

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

}

NSString * STR5 = @ "HelloWorld";

NSString * ptr1 = [STR5 substringtoindex:4];//string extraction from subscript 0 to 4 not including 4

NSString * ptr2 = [STR5 substringfromindex:5];//starting from subscript 5 to end of string including 5

Nsrange range1 = {4,3};//struct-body initialization

NSString * PTR3 = [STR5 substringwithrange:range1];//is extracted within range specified range

NSString * PTR4 = [STR5 substringwithrange:nsmakerange (4,3)];//nsmakerange can generate a struct



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

BOOL ret = [str hasprefix:@ "www"];//determines whether a string begins with a specified string



NSString * str1 = @ "1.txt";

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



Nsmutablestring variable strings (dynamically incremented and decreased) inherit from all methods that NSString can use NSString

nsmutablestring * str = [[nsmutablestring alloc]initwithstring:@ "Hello"];//converts immutable strings to mutable strings

[Str insertstring:@ "123" atindex:1];//inserts the NSString type string in the specified subscript (not crossed) position

[Str appendstring:@ "123"];//appends string to the end of the string

[Str deletecharactersinrange:nsmakerange (0, 2)];//remove length characters from the specified subscript

[Str setstring:@ "Baidu"];//modified string also known as the variable string assignment

[Str replacecharactersinrange:nsmakerange (3, 1) withstring:@ "ios"];//replaces the length character of the specified subscript position with the specified string

The string NSString in iOS

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.