Swift # string

Source: Internet
Author: User

//: Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."println ("---- character");/*characters: Swift and OC characters are not the same, Swift is in double quotation marks. The character type in Swift is not the same as in OC, and the character in OC occupies one byte because it contains only the characters in the ASCII table. While the characters in Swift can store Unicode characters in addition to the characters in the ASCII table, for example, the characters of the Chinese OC are in accordance with the ASCII standard, swift characters are in accordance with the Unicode standard, so they can hold the characters of all national languages in time (most) OC : Oc:char charvalue = ' a '; char charvalue = ' Li '; Error Swift:*/var charvalue:character="a"var charvalue1:character="Li" //correct//Note: Only one character can be placed in double quotes, as in the error notation//Swift:var charvalue:character = "AB"println ("---- string");/*string: A character is a collection of single characters, a string is a collection of multiple characters, and a string is required to hold more than one character. C:char *stringvalue = "AB"; char Stringarr = "AB"; oc:nsstring *stringvalue = "AB"; Swift:var stringvalue = "AB" >c the string in the language is terminated with a, for example: char *stringvalue = "ABC\0BCD";p rintf ("%s", stringvalue); print result is ABC The string in the >oc language is also terminated with a, for example: nsstring *stringvalue = @ "ABC\0BCD"; NSLog (@ "%@", stringvalue); the string in Abc>swift is different from the strings in the C language/oc language*/var stringvalue="AB"var stringValue1="ABC\0BCD"println (stringvalue) println ("-Calculate string, Length-");/*The usual method of string:> computes the length of the string-----c:char *stringvalue = "abc Lee";p rintf ("%tu", strlen (StringValue)); Print result 6, bytes oc:nsstring * stringvalue = @ "abc Lee"; NSLog (@ "%tu", stringvalue.length); print result 4 to UTF16 the string in Swift:swift is fully compatible with the Unicode standard*/var stringValue2="ABC Lee"println (Count (stringValue2))//Printing Results 4, regardless of the encoding, is the number of calculated elementsprintln ("-- stitching, string- -");/*> string concatenation-----c:char str1[] = "abc"; char *str2 = "BCD"; char *str = strcat (str1, str2); oc:nsmutablestring *str1 = [NSMU Tablestring stringwithstring:@ "abc"]; NSString *str2 = @ "BCD"; [Str1 APPENDSTRING:STR2]; NSLog (@ "%@", str1); Swift:*/var str1="ABC"; var str2="BCD"; var str12= str1 +Str2;println ("-- formatting, string- -");/*> String Formatting-----oc:nsinteger index = 1; NSString *str1 = [nsmutablestring stringwithformat:@]Http://ios.itcast.cn/pic/%tu.png", index]; NSLog (@ "%@", str1); Swift:*/var index=1var str3="http://ios.itcast.cn/pic/\ (index). PNG"println (STR3) println ("-- comparison, string--");/*> string comparison-----oc:nsstring *str1 = @ "abc"; NSString *STR2 = @ "abc", if ([str1 isequaltostring:str2]) {NSLog (@ "equal");} Else{nslog (@ "Not Equal");} if ([str1 compare:str2] = = nsorderedsame) {NSLog (@ "equal");} Else{nslog (@ "Not Equal");} Swift: (= = =/! =/>=/<=), as in C language, is compared by strcmp*/var str10="ABC"; var str20="ABC";ifStr10 = =str20{println ("Equal");}Else{println ("Not Equal");} var str11="Abd"; var str21="ABC";ifStr11 >=str21{println ("greater than or equal");}Else{println ("not greater than or equal to");}/*> Determine if null oc:nsstring *STR1 = @ "abc"; if (str1.length = = 0) {NSLog (@ "no value");} Else{nslog (@ "has value");} Swift:*/var str0="ABC"ifstr0.isempty{println ("No value")}Else{println ("has a value")}/*> Judging prefix-----oc:nsstring *str = @ "http://ios.520it.com"; if ([Str hasprefix:@" http "]) {NSLog (@" is url ");} if ([Str hassuffix:@ ". CN"]) {NSLog (@ "is the celestial top-level domain name");} Swift:*/var str00="http://ios.520it.com"ifStr00.hasprefix ("http") {println ("is a URL");}ifStr00.hassuffix (". CN") {println ("is the celestial top-level domain");}/*> Get extension-----oc:nsstring *str = @ "Abc.txt"; NSLog (@ "%@", [str pathextension]); Swift:*/var str02="Abc.txt";p rintln (str02.pathextension)/*> Casing Conversion-----oc:nsstring *str = @ "Abc.txt"; NSLog (@ "%@", [str uppercasestring]); NSLog (@ "%@", [str lowercasestring]); Swift:*/var Str01="Abc.txt";p rintln (str01.uppercasestring) println (str01.lowercasestring)/*> Data type conversion oc:nsstring *STR = @ "250"; Nsinteger number = [str integervalue]; NSLog (@ "%tu", number); Swift:*/var str000=" -"//if Str cannot be converted to an integer, then the optional type returns nil//str = "250SB"//take care not at the time of conversion (. ToInt ()!) Forced unpacking, which triggers a run-time error if the package cannot be converted and forced to unpackvar number:int? =Str000.toint ()ifNumber! =nil{//Previous versions of println will be automatically disassembled, and now will notprintln (number!)}//If you want to convert a string to another type, you need to use the method in OCstr000 ="10.1"var d= (str000 asNSString). Doublevalue

|--> Copyright (c) Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

Swift # 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.