Improvements to the swift string truncation method

Source: Internet
Author: User

Tag: swift string capture IOS substringwithrange string

String truncation is a common basic method for string processing. Anyone familiar with IOS knows that there are substringtoindex:, substringfromindex:, and substringwithrange in the nsstring of the basic class.


Problem description:

So does the string class in SWIFT have the same interception function?

Check the header file of the string class repeatedly and do not find the same or similar function interfaces.

The preceding method cannot be called for a string type variable directly in the swift file.

However, by introducing the basic framework, that is

import Foundation

You can use the first two truncation methods:

/* String truncation */var STR: String = "Hello, world! "Println (Str. substringtoindex (3) println (Str. substringfromindex (3 ))

However, it is strange that the third method cannot be used. Whether it is like this, create a range:

var r:NSRange = NSMakeRange(1,2)


The creation scope is as follows:

var r = NSRange(location: 1,length: 2)


The syntax for calling the substringwithrange () function is always incorrect:

VaR str1: String = Str. substringwithrange (r) // syntax error!

Error message: cannot convert the expression's type 'string' to type' range <string. index>'


It can be seen that the parameter type does not match. It is not familiar with how the range <string. index> type can be implemented, but the following two solutions are provided.


Solution:


Solution (1): Convert the variables of the string class to the nsstring type.

var nsString: NSString = strprintln(nsString.substringWithRange(r))


Of course, this solution is a little bit redundant. In fact, it is not a string class but an nsstring class. In addition, each string class must be converted to one stop. If there are more than one string, it will be a little troublesome.

Is there any other way to solve this problem? The second solution is provided below.


Solution (2): extend the string class and overload substringwithrange ()

extension String {    func substringWithRange(range: NSRange) -> String! {        var str1 = self.substringFromIndex(range.location)        var str2 = str1.substringToIndex(range.length)        return str2    }}

With this extension, you can use the nsstring method to intercept strings!


Complete DEMO code:

//// Main. swift // Method for intercepting strings in swift /// created by Du Zi Jun on 14-7-4. // copyright (c) 2014 lanou3g.com lanou All Rights Reserved. // var STR: String = "Hello, world! "/* Before import Foundation, the substring truncation method cannot use * // println (Str. substringtoindex (3) // error // println (Str. substringfromindex (3) // After errorimport Foundation/* import Foundation, the following two functions can be directly used */println (Str. substringtoindex (3) println (Str. substringfromindex (3) var R: nsange = nsmakerange (3, 5)/* the variable of the string class cannot directly take the middle substring * // println (Str. substringwithrange (R) // error // solution (1): Convert to VaR nsstring of nsstring class: nsstring = s Trprintln (nsstring. substringwithrange (R) // solution (II): extended string class extension string {func substringwithrange (range: nsange)-> string! {Var str1 = self. substringfromindex (range. Location) var str2 = str1.substringtoindex (range. Length) return str2 }} println (Str. substringwithrange (r ))








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.