Original blog, reproduced please indicate the source
String
In Swfit, string is Unicode-compatible. The use method 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"//hwc
let myname = "\"Hwc\""//"hwc"
Initialize
Var emptyString = "" // empty string
Var sameEmptyString = String()//empty string
Var notEmptyString = "first"
Value Passing
In Swfit, a value is passed when a string is assigned or passed as a parameter to a function, that is, a copy is passed, not a reference to itself. This is different from Cocoa's nsstring.
string Common operations
1 IsEmpty Properties
Determines whether the 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 from the T, as the String.index suitable API is not found for the time being
StackOverflow found some information, it seems that Swift's API has a lot of imperfect places
In the end, I'll talk about how to extend a class and then give examples of substrings
var str = "hello world"
str.substringFromIndex(advance(str.startIndex,6))//hello
str.substringToIndex(advance(str.startIndex,5)) //world
str.substringWithRange(Range(start:advance(str.startIndex,2),end:advance(str.startIndex,8)))//llo wo
5 string concatenation
Very simple answer, with a 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 Case Conversion
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") //true
str.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 characters
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
11 Extension string
By extending the Swifr class, you can provide new methods without altering the original classes. 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 {
let end = countElements(self)
return self[from..<end]
}
func substring(from: Int, length: Int) -> String {
let end = from + length
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 = str.substring(0,length:5)
On the right side of the playground, you can see
Output
Hello
Hello
World
Swift Getting Started Tutorial 3-string strings