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