Swift Development String+nsstring Detailed use

Source: Internet
Author: User

//

Viewcontroller.swift

Swift+string

Import UIKit

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {

Super.viewdidload ()

The var variable let constant does not describe too much

Self.view.backgroundColor = Uicolor.yellow;

Let my:nsstring = "My Name"

Let name = "are FAI"

var myName = ""

Let myfirst:nsstring = "My Name"

MyName = (my as String) + name

Print (MyName)

Calculate the length of a string

Print (myfirst.length)//nsstring type string

Print (MyName.characters.count)//string type string

Get string position

var stra = "China is the most powerful country"

Let startindex = stra.startindex//get start position

Print (startindex)

Let EndIndex = stra.endindex//Get End position

Print (EndIndex)

Let char = Stra[stra.index (After:startindex)]//gets the character that corresponds to a subscript after a subscript

Print (char)

Let char2 = Stra[stra.index (before:endindex)]//gets the character that corresponds to the previous subscript of a subscript

Print (CHAR2)

Let subString = Stra[startindex...stra.index (startindex, Offsetby:4)]//get a substring in a string by scope

Print (subString)

Let num = Stra.range (of: "Most powerful")//Gets the range of a string in the parent string

Print (num)

Stra.append ("World of")//Append string operation

Print (Stra)

Stra.insert ("~", At:strA.index (Stra.startindex, offsetby:10))//In the specified position, such as a group of characters

Print (Stra)

Stra.replacesubrange (Stra.startindex...stra.index (Stra.startindex, Offsetby:9), with: "Our big China Stands Forever")//Replace a string in the specified range

Print (Stra)

Stra.remove (At:strA.index (Before:strA.endIndex))//delete a character at the specified location

Print (Stra)

Stra.removesubrange (Stra.startindex...stra.index (Stra.startindex, Offsetby:2))

Print (Stra)

var StrB = "MyName"

StrB = strb.uppercased ()//full capitalization

Print (StrB)

StrB = strb.lowercased ()//convert all to lowercase

Print (StrB)

Connection of strings

String type strings

Let str1:string = "Up"

Let str2:string = "sea"

Connect two strings using the "+" sign

var str3:string = str1 + str2

Print (STR3)

Stitching strings at the end of the string instead of Str3.extend ("123456789")

Str3.write ("123456789")

Print (STR3)

string comparison

Sting type String

Let str4:string = "Shanghai"

Let str5:string = "Shanghai"

Determine if a string is equal

Let Bl1:bool = STR4 = = STR5

Print (BL1)

Determines whether a string is empty

Let Bl2:bool = Str4.isempty

Print (BL2)

Judging string prefixes

Let Bl3:bool = Str4.hasprefix ("SHANG")

Print (BL3)

To determine a string suffix

Let Bl4:bool = Str4.hassuffix ("HAI")

Print (BL4)

Comparison of two strings

Let Result1:comparisonresult = Str4.compare (STR5)

Print (RESULT1)

Ignore case comparison

Let Result2:comparisonresult = Str4.caseinsensitivecompare (STR5)

Print (RESULT2)

Nssting type String

Let str6:nsstring = "Shanghai"

Let str7:nsstring = "Shanghai"

Determine if two strings are equal

Let Bl5:bool = Str6.isequal (STR7)

Print (BL5)

Judging string prefixes

Let Bl6:bool = Str6.hasprefix ("Shang")

Print (BL6)

To determine a string suffix

Let Bl7:bool = Str6.hassuffix ("Hai")

Print (BL7)

Ignore case comparison

Let Result3:comparisonresult = Str6.compare (str7 as String)

Print (RESULT3)

Let Result4:comparisonresult = Str6.caseinsensitivecompare (str7 as String)

Print (RESULT4)

String cutting

Sting type String

Let str11:string = "Dong$fang$ming$zhu"

String is cut by character

Let Arr1:array = str11.components (separatedby: "$")

Print (ARR1)

Automatically according to the "/" split string NSString Unique, string does not

Let str13:nsstring = "Users/jhq0228/desktop"

Let Arr3:array = str13.pathcomponents

Print (ARR3)

The conversion of a string to a number is a little less than a bit of an understanding.

String type strings

Character to number

Let Charr:character = "A";

var charval1:int = 0

String to traverse character conversion to

For Charstr in String (Charr). Unicodescalars {

Converts the removed character to an Int type number

CharVal1 = Int (charstr.value)

}

String to Number

Numeric string-to-Int number, instead of intstr.toint ()

Let intstr:string = "64"; Let Intval1:int? = Int (INTSTR)

Numeric to character

Let IntVal2 = 65; Let Charval2:character = Character (Unicodescalar (INTVAL2)!)

Numeric to String

Int type numeric to string

Let intval3:int = 123; Let intstr1:string = Intval3.description

Int32 type Digital to string

Let Int32val1:int32 = 32; Let int32str1:string = Int32val1.description

Int64 type Digital to string

Let Int64val1:int64 = 64; Let int64str1:string = Int64val1.description

Float type numeric to string

Let floatval1:float = 2.1; Let floatstr1:string = Floatval1.description

Double numeric-to-string

Let doubleval1:double = 162.1; Let doublestr1:string = Doubleval1.description

Bool type to String

Let Boolval1:bool = true; Let boolstr1:string = Boolval1.description

NSString type String

String to Number

String-to-Int number

Let Intval4:int = NSString (string: "6432"). IntegerValue

String to Int32 type number

Let Int32val2:int32 = NSString (string: "+"). Intvalue

String to Int64 type number

Let Int64val2:int64 = NSString (string: "$"). Longlongvalue

String to float type number

Let Floatval2:float = NSString (string: "2.1"). Floatvalue

String to Double type number

Let doubleval2:double = NSString (string: "162.1"). Doublevalue

String to Bool type

Let Boolval2:bool = NSString (string: "false"). Boolvalue

Additional setup after loading the view, typically from a nib.

}

Override Func didreceivememorywarning () {

Super.didreceivememorywarning ()

Dispose of any resources the can be recreated.

}

}

Swift Development String+nsstring Detailed use

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.