简体   繁体   中英

Swift 2.0 String with substringWithRange

I am trying to get first char from String. It should be easy but I can't do in Swift 2.0 (with Xcode beta 6).

Get nth character of a string in Swift programming language

I have tried that method also. It use extension but I can't retrieve using that method. May I know how to do?

Two solutions without casting to NSString

let string = "Hello"
let firstChar1 = string.substringToIndex(string.startIndex.successor())

let firstChar2 = string.characters.first

Update for Swift 2:

Since Swift 2 returns Character rather than String a new String must be created.

let firstChar2 = String(string.characters.first!)

Update for Swift 3:

successor() has been replaced with index(after:..)

let firstChar1 = string.substring(to:string.index(after: string.startIndex))

Try this,

let str = "hogehoge"
let text = (str as NSString).substringFromIndex(1) // "ogehoge"

For what it's worth (and for people searching for and finding this topic), without casting the String to NSString, you need to do the following with Swift 2.1:

let myString = "Example String"
let mySubString = myString.substringWithRange(Range<String.Index>(start: myString.startIndex.advanceBy(0), end: myString.startIndex.advanceBy(4)))

print(mySubString) //'Exam'

This would printout "Exam". Must say that it's much more verbose than in Obj-C. And that's saying something... ;-) But it gets the job done and without casting to NSString.

Try this

let myString = "My String" as NSString
myString.substringWithRange(NSRange(location: 0, length: 3))

In Swift 2 a String is not a collection of anything. According to the documentation:

/// `String` is not itself a collection of anything.  Instead, it has
/// properties that present the string's contents as meaningful
/// collections:
///
///   - `characters`: a collection of `Character` ([extended grapheme
///     cluster](http://www.unicode.org/glossary/#extended_grapheme_cluster))
///     elements, a unit of text that is meaningful to most humans.
///
///   - `unicodeScalars`: a collection of `UnicodeScalar` ([Unicode
///     scalar
///     values](http://www.unicode.org/glossary/#unicode_scalar_value))
///     the 21-bit codes that are the basic unit of Unicode.  These
///     values are equivalent to UTF-32 code units.
///
///   - `utf16`: a collection of `UTF16.CodeUnit`, the 16-bit
///     elements of the string's UTF-16 encoding.
///
///   - `utf8`: a collection of `UTF8.CodeUnit`, the 8-bit
///     elements of the string's UTF-8 encoding.

Assuming you want to find the second character,

var str = "Hello, playground"
let chars = str.characters
let n = 2
let c = str.characters[str.characters.startIndex.advancedBy(n)]
var mySuperCoolString = "Hello, World!!!!!!!!1!!";    
println(mySuperCoolString.substringWithRange(Range<String.Index>(start: advance(mySuperCoolString.startIndex, 0), end: advance(mySuperCoolString.startIndex, 1))));

This should print out H

Swift 2.2 and Swift 3.0

let string = "12134"
string.substringWithRange(string.startIndex..<string.startIndex.advancedBy(2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM