1

I have a string:

myString = "mystring"

I would simply like to get the first 5 characters

which is the easiest way to do that in Swift?

Daniele B
  • 19,801
  • 29
  • 115
  • 173
  • 2
    possible duplicate http://stackoverflow.com/questions/24044851/how-do-you-use-string-substringwithrange-or-how-do-ranges-work-in-swift – Victor Sigler Aug 24 '15 at 21:38

3 Answers3

2
let substring: String = (myString as NSString).substringToIndex(5)
Daniele B
  • 19,801
  • 29
  • 115
  • 173
Choppin Broccoli
  • 3,048
  • 2
  • 21
  • 28
2

Correct answer from Choppin, but if you want to do it in the pure swift way (without casting to NS String :

 myString = myString.substringToIndex(advance(myString.startIndex, 5))
Glenn
  • 2,808
  • 2
  • 24
  • 30
  • it's crazy how very little intuitive this is in Swift – Daniele B Aug 24 '15 at 21:37
  • 1
    yep, I have to google it again and again in each project because I keep forgetting it – Glenn Aug 24 '15 at 21:56
  • It may seem odd, but there are good reasons for it. For example http://stackoverflow.com/questions/24880604/type-string-index-does-not-conform-protocol-integerliteralconvertible – Abizern Sep 22 '15 at 15:50
  • great Addition Abizern! – Glenn Sep 22 '15 at 16:10
  • Not working anymore in Swift 3, but there is a working extension here: http://stackoverflow.com/questions/24044851/how-do-you-use-string-substringwithrange-or-how-do-ranges-work-in-swift/39742687#39742687 – winterized Sep 28 '16 at 09:13
0

Use these extensions:

Swift 2.3

extension String
{
    func substringFromIndex(index: Int) -> String
    {
        if (index < 0 || index > self.characters.count)
        {
            print("index \(index) out of bounds")
            return ""
        }
        return self.substringFromIndex(self.startIndex.advancedBy(index))
    }

    func substringToIndex(index: Int) -> String
    {
        if (index < 0 || index > self.characters.count)
        {
            print("index \(index) out of bounds")
            return ""
        }
        return self.substringToIndex(self.startIndex.advancedBy(index))
    }

    func substringWithRange(start: Int, end: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if end < 0 || end > self.characters.count
        {
            print("end index \(end) out of bounds")
            return ""
        }
        let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(end))
        return self.substringWithRange(range)
    }

    func substringWithRange(start: Int, location: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if location < 0 || start + location > self.characters.count
        {
            print("end index \(start + location) out of bounds")
            return ""
        }
        let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(start + location))
        return self.substringWithRange(range)
    }
}

Swift 3

extension String
{   
    func substring(from index: Int) -> String
    {
        if (index < 0 || index > self.characters.count)
        {
            print("index \(index) out of bounds")
            return ""
        }
        return self.substring(from: self.characters.index(self.startIndex, offsetBy: index))
    }

    func substring(to index: Int) -> String
    {
        if (index < 0 || index > self.characters.count)
        {
            print("index \(index) out of bounds")
            return ""
        }
        return self.substring(to: self.characters.index(self.startIndex, offsetBy: index))
    }

    func substring(start: Int, end: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if end < 0 || end > self.characters.count
        {
            print("end index \(end) out of bounds")
            return ""
        }
        let startIndex = self.characters.index(self.startIndex, offsetBy: start)
        let endIndex = self.characters.index(self.startIndex, offsetBy: end)
        let range = startIndex..<endIndex

        return self.substring(with: range)
    }

    func substring(start: Int, location: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if location < 0 || start + location > self.characters.count
        {
            print("end index \(start + location) out of bounds")
            return ""
        }
        let startIndex = self.characters.index(self.startIndex, offsetBy: start)
        let endIndex = self.characters.index(self.startIndex, offsetBy: start + location)
        let range = startIndex..<endIndex

        return self.substring(with: range)
    }
}

Usage:

let myString = "mystring"
let substring = myString.substringToIndex(5)
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86