The following is a summary of the properties and methods commonly used in string
1, determine whether it is empty: IsEmpty
123 |
var str: String if str.isEmpty{ } |
2, get the number of characters: countelements
12 |
let str = "hangge.com" println( "\(countElements(str))个字符" ) |
3, check if the string has a specific prefix/suffix: hasprefix/hassuffix
123 |
var str = "hangge.com" if str.hasSuffix( ".com" ){ } |
4, you can also use "\ ()" In the string to wrap variables, constants
12 |
let name = "hangge.com" let msg = "欢迎来到 \(name)" |
5, Case conversion:
Use the uppercasestring, LowerCaseString, capitalizedstring properties of a string to access a string of uppercase/lowercase/first letter capitals
12345 |
let str = "Welecome to Hangge.com"          var uppercase = str.uppercasestring //welecome to hangge.com var lowercase = str.lowercasestring //welecome to hangge.com var capitalized = str.capitalizedstring //welecome to hangge.com |
6, String intercept
(1) Convert string to NSString and intercept
12345 |
var str =
"Welecome to hangge.com"
var fromStr = (str
as NSString
).substringFromIndex(5)
//ome to hangge.com
var toStr = (str
as NSString
).substringToIndex(5)
//Welec
var rangeStr = (str
as NSString
).substringWithRange(
NSMakeRange
(4,1))
//c
|
(2) direct call to the corresponding method of string (recommended)
123456789 |
var str =
"Welecome to hangge.com"
let index =
advance
(str.startIndex, 5)
let index2 =
advance
(str.endIndex, -5)
var range =
Range
<
String
.
Index
>(start: index, end: index2)
var fromStr = str.substringFromIndex(index)
//ome to hangge.com
var toStr = str.substringToIndex(index2)
//Welecome to hangg
var rangeStr = str.substringWithRange(range)
//ome to hangg
|
Swift-strings (string) usage detailed