Search code examples
groovy

How to get last 2 digit from a String


I have a problem to get last two digit from a string.

example :

String texter = "5793231309"

how to get '09' ? so when Iprintln "texter : "+texter. It will be Groovy<<09

I try split but it not successful ?


Solution

  • Use this to split your first String:

    static main(args) {
    
        String texter = "5793231309"
        String texter2 = texter[-2..-1]
    
        println(texter2)
    }