Improvements to the "IOS" Swift string interception method

Source: Internet
Author: User

The method of string interception is the basic method used frequently in string processing. Friends who are familiar with iOS know that there are substringtoindex:,substringfromindex in the nsstring of the base class: and Substringwithrange: These three main methods of interception.


Description of the problem:

So, does the string class in the Swift language have the same interception capability?

Double-check the header file of the string class. The same or similar function interfaces were not found.

It is also not possible to invoke the above method on a variable of a string class directly in the swift file.

However, by introducing the underlying framework,

Import Foundation

You will be able to use the first two interception methods:

/* String intercept */var str:string = "Hello, world!" println (Str.substringtoindex (3)) println (Str.substringfromindex (3))

However, it is strange that the third method cannot be used.

Either way, create the scope:

var = nsmakerange (R:nsrange)


Or create a scope like this:

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


Calling the Substringwithrange () function is always syntactically 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 type of participation does not meet, do not understand how range<string.index> this type, but give the following two ways to solve.


How to resolve:


Workaround (i): Dump the variables of the string class to the NSString type

var nsstring:nsstring = strprintln (Nsstring.substringwithrange (R))


Of course, this method is somewhat dependent, in essence, it is not the interception of the string class, but the NSString class. And every string class is going to go all of a sudden. Assuming more strings, it's a little cumbersome.

Is there any other way to solve the problem? A second solution is given below.


Workaround (ii): Extend the String class, 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 intercept strings in a nsstring way!


Full Demo Code:

////Main.swift How to intercept strings in Swift////Created by Chieh-on 14-7-4.//Copyright (c) 2014 lanou3g.com Lan All rights Reserved.//var Str:stri ng = "Hello, world!"  /* Before Import Foundation, the substring interception method cannot use *///println (Str.substringtoindex (3))//error//println (Str.substringfromindex (3)) After Errorimport foundation/* import Foundation, the following two functions can be used directly with */println (Str.substringtoindex (3)) println ( Str.substringfromindex (3)) var R:nsrange = Nsmakerange (3,5)/* Variables of the String class cannot be taken directly to the middle substring *///println ( Str.substringwithrange (R))//error//solution (a): var nsstring:nsstring = Strprintln converted to NSString class ( Nsstring.substringwithrange (R))//Solution (II): Extended String Class extension string {func substringwithrange (range:nsrange), St ring! {var str1 = Self.substringfromindex (range.location) var str2 = Str1.substringtoindex (range.length) r Eturn str2}}println (Str.substringwithrange (R)) 








Improvements to the "IOS" Swift string interception method

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.