Swift Novice Tutorial 3-strings String

Source: Internet
Author: User
Tags scalar

Original blog, reproduced please indicate the source

String
In Swfit, string is Unicode-compatible. The usage is similar to the C language.
Attention

In cocoa and cocoa touch, Swift's string, and NSString in the foundation are compatible, and all NSString-amount APIs can invoke the string type


String constants
1, with escape characters: such as \n,\t, etc.
2, single-byte Unicode scalar, \XMM
3, double-byte Unicode scalar, \ummmm
4, four-byte Unicode scalar, \ummmmmmmm
The m here is the hexadecimal number

Let myname = "HWC"//hwclet myname = "\" hwc\ ""//"HWC"

Initialize

var emptystring = ""//empty string var sameemptystring = string ()//empty string var notemptystring = "First"

Value Passing
In the Swfit. A value is passed when a string is assigned to a function or is passed as a parameter. This means that a copy is passed, not a reference to itself.

This is different from Cocoa's nsstring.


Strings often use operations
1 IsEmpty Properties
Infer whether it is empty

Let str = "" If Str.isempty{println ("This Is empty")}


2 StartIndex EndIndex countelements
Returns the Index of the last character, returns the value type String.index
Let str = "Hello World" println (Str.endindex)//11

println (countelements (str))


3 ToInt ()
return int? That is, if the conversion succeeds, the return value is int, otherwise nil
var str = "123" var result = Str.toint () if result! = Nil{println ("Success")}

4 Sub-string

  Substringfromindex (Index:String.index)  substringtoindex (index:String.index)  Substringwithrange (arange: Range ())
Here to use a advance function: Advance (start:t,n:distance)
is the offset distance distance relative to T. Due to temporary not finding an int to string.index the appropriate API
StackOverflow found some information, it seems that Swift's API has a lot of non-intact places
In the end. I'll talk about how to extend a class, and then give a sample of the substring

var str = "Hello World" str.substringfromindex (Advance (str.startindex,6))//hellostr.substringtoindex (Advance ( str.startindex,5))//worldstr.substringwithrange (Range (Start:advance (str.startindex,2), End:advance ( str.startindex,8))//llo wo

5 string concatenation

Very simple answer, with Plus + or string interpolation

var str1 = "Hello" var str2 = "World" var str = str1 + str2
var str2 = "\ (str1) HWC and the \ (str2)"//hello HWC and the world

6 uppercase and lowercase conversions
Str.uppercasestring//hello World str.lowercasestring//hello World


7 prefix suffix equals
Bool Hasprefix (prefix:string)
Bool Hassuffix (suffix:string)

var str = "Hello HWC" str.hasprefix ("Hello")//truestr.hassuffix ("123")//false
var str1 = "Jack"
if  str = = str1{println ("Equal")}



8 processing substrings
void Insert (NEWELEMEMT:CHARACTER,ATINDEX:STRING.INDEX)//insert character void Removeatindex (I:string.index)//delete character void RemoveRange (subrange:range<string.index>)//delete an interval void Replacerange (SUBRANGE:RANGE<STRING.INDEX> WITH:C)


9 formatting merged strings
String Stringbyappendingformat (Format:string,arguments:cvarargtype ...)
var str = "Hello World" Str.stringbyappendingformat ("%d", 4)//hello World4

Ten UTF8 UTF16 properties

Str.utf8//Returns a collection of UTF8 representations of STR

STR.UTF16//Returns a collection of utf17 representations of STR

Extended string

By extending the Swifr class, you can provide new methods without changing the original class. Here are a few extension functions that expand the

Get substrings by subscript

and three overloads of substring

Extension string {    subscript (r:range<int>), String {         get {            Let Substart = Advance ( Self.startindex, R.startindex, Self.endindex)             Let SubEnd = Advance (Substart, R.endindex-r.startindex, Self.endindex)              return Self.substringwithrange (Range (Start:substart, end:subend))         }   }    func substring (from:int), String {  & nbsp;     Let end = Countelements (self)         return self[ From.. <end]   }    func substring (from:int, length:int), String {   & nbsp;    let end = from + LENGTH&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp Return Self[from. <end]   }    func substring (from:int, to:int)->string    {         return Self[from. <to]   }}var str = "Hello world" var str1 = str.substring (6) var str2 = str.substring (0,to:5) var str3 = s Tr.substring (0,length:5)
On the right side of the playground, you can see

Output

Hello

Hello

World




Swift Novice Tutorial 3-strings 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.