Swift-strings (string) usage detailed

Source: Internet
Author: User

The following is a summary of the properties and methods commonly used in string

1, determine whether it is empty: IsEmpty
123 varstr:Stringifstr.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 varstr = "hangge.com"ifstr.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.comvar toStr = (str as NSString).substringToIndex(5)  //Welecvar 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.comvar toStr = str.substringToIndex(index2)  //Welecome to hanggvar rangeStr = str.substringWithRange(range) //ome to hangg

Swift-strings (string) usage detailed

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.