Get the last character of a string without using array?

SwiftString

Swift Problem Overview


I have a string

let stringPlusString = TextBoxCal.text

I want to get the last character of stringPlusString. I do not want to use array.

Java has charAt but I could not find something similar in Swift

Swift Solutions


Solution 1 - Swift

Using last to get the last Character

For Swift 4:

let str = "hello world๐Ÿ˜"
let lastChar = str.last!   // lastChar is "๐Ÿ˜"

For Swift 2 and Swift 3:

let str = "hello world๐Ÿ˜"
let lastChar = str.characters.last!   // lastChar is "๐Ÿ˜"

For Swift 1.2:

let str = "hello world๐Ÿ˜"
let lastChar = last(str)!   // lastChar is "๐Ÿ˜"

Subscripting the String to get the last Character

This works for Swift 3 and Swift 4:

let str = "hello world๐Ÿ˜"
let lastChar = str[str.index(before: str.endIndex)]   // lastChar is "๐Ÿ˜"

And for Swift 1.2 and Swift 2:

let str = "hello world๐Ÿ˜"
let lastChar = str[str.endIndex.predecessor()]   // lastChar is "๐Ÿ˜"

Solution 2 - Swift

Swift 4:

First some char Last some char

let strOSName = "mac operating system"
print(strOSName.prefix(3))       //first char. o/p:  MAC
print(strOSName.suffix(6))       //last char.  o/p:  system

Solution 3 - Swift

"Without using Array on swift"

Here you go:

var text = "Lorem ipsum"
var lastChar = text.substringFromIndex(text.endIndex.predecessor())

Solution 4 - Swift

    let text = "Muhammad Raza"
    println("\(text[advance(text.endIndex, -1)])")

in Short !

Solution 5 - Swift

Details

  • Swift 5.1, Xcode 11.2.1

Solution

extension String {
    func onlyLastCharacters(_ count: Int) -> String { return String(suffix(count)) }
    func onlyLastCharacters(_ count: Int, checkLength: Bool) -> String? {
        if checkLength {
            if self.count >= count { return onlyLastCharacters(count) }
            return nil
        }
        return String(suffix(count))
    }
}

Usage

str.onlyLastCharacters(6)
str.onlyLastCharacters(13, checkLength: true)

Full sample

> Do not forget to paste here the solution code

var testNumber = 0
func show(original: String, modified: String?) {
    testNumber += 1
    if let string = modified {
        print("\(testNumber). Original: \"\(original)\", modified: \"\(string)\"")
    } else {
        print("\(testNumber). nil | count: nil")
    }
}

var str = "Hello world!"
show(original: str, modified: str.onlyLastCharacters(6))
show(original: str, modified: str.onlyLastCharacters(12))
show(original: str, modified: str.onlyLastCharacters(13, checkLength: false))
show(original: str, modified: str.onlyLastCharacters(13, checkLength: true))

str = ""
show(original: str, modified: str.onlyLastCharacters(10))
show(original: str, modified: str.onlyLastCharacters(10, checkLength: true))
show(original: str, modified: str.onlyLastCharacters(10, checkLength: false))

Log

1. Original: "Hello world!", modified: "world!"
2. Original: "Hello world!", modified: "Hello world!"
3. Original: "Hello world!", modified: "Hello world!"
4. nil | count: nil
5. Original: "", modified: ""
6. nil | count: nil
7. Original: "", modified: ""

Solution 6 - Swift

Here is another shorter way to get the last letter from string:

let text = "some string"
last(text) // returns {Some "a"}

The method last() returns an optional Character.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGökhan ÇokkeçeciView Question on Stackoverflow
Solution 1 - SwiftvacawamaView Answer on Stackoverflow
Solution 2 - SwiftDeepak TagadiyaView Answer on Stackoverflow
Solution 3 - SwiftOscar SwanrosView Answer on Stackoverflow
Solution 4 - SwiftMuhammad RazaView Answer on Stackoverflow
Solution 5 - SwiftVasily BodnarchukView Answer on Stackoverflow
Solution 6 - SwiftAnvar AzizovView Answer on Stackoverflow