7

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?

7 Answers 7

7

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))
Sign up to request clarification or add additional context in comments.

1 Comment

yes, casting is not necessary, because import Foundation is required, so free bridging does the job
4

Try this,

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

Comments

4

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.

1 Comment

substringWithRange requires import Foundation. You don't need to cast because free bridging does the job
2

Try this

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

Comments

2

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)]

1 Comment

@mservidio alternatively use let chars = str.characters.map{$0}; let c = chars[2];
0
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

Comments

-1

Swift 2.2 and Swift 3.0

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

1 Comment

actually for Swift 3 it is: String.substring(with: Range<Index>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.