Question

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 ?

Was it helpful?

Solution

Use this to split your first String:

static main(args) {

    String texter = "5793231309"
    String texter2 = texter[-2..-1]

    println(texter2)
}

OTHER TIPS

Here's a one liner that's also a safe alternative:

assert "5793231309".reverse().take(2).reverse() == "09"

In groovy you can substring via negative indices.

String last2 = texter[-2..-1] // Last 2 symbols

Its an analogue of substring, and it uses Ranges.

http://groovy.codehaus.org/Collections see 'Slicing with the subscript operator'

Inspired by tim_yates:

It may be safer to use some function, to extract last n characters, as tim suggested. But I think his solution, with regexp is a big overhead, and may be hard to understand by novices.

There is an easier and faster way to do this, using size() check, and then range substring:

def lastN(String input, int n){
  return n > input?.size() ? null : n ? input[-n..-1] : ''
}
assert lastN("Hello", 2) == 'lo'
assert lastN("Hello", 3) == 'llo'
assert lastN("Hello", 0) == ''
assert lastN("Hello", 13) == null
assert lastN(null, 3) == null

Be careful though, if your unit is less than 2 characters long, s[ -2..-1 ] will fail.

Might be better to do:

String lastN( String input, int n ) {
    input == null ?
        null :
        ( input =~ /^.+(\S{$n})$/ ).with { m -> m.matches() ?
                                                    m[ 0 ][ 1 ] :
                                                    null }
}

assert lastN( "5793231309", 2 ) == '09'
assert lastN( "5793231309", 3 ) == '309'
assert lastN( "5793231309", 0 ) == ''
assert lastN( '', 2 ) == null
assert lastN( null, 2 ) == null

Or:

String lastN( String input, int n ) {
    if( input == null || input.length() < n ) null
    else if( n == 0 ) ''
    else input[ -n..-1 ]
}

assert lastN( "5793231309", 2 ) == '09'
assert lastN( "5793231309", 3 ) == '309'
assert lastN( "5793231309", 0 ) == ''
assert lastN( '', 2 ) == null
assert lastN( null, 2 ) == null

The most readable solution is probably just to drop() all but the last two characters:

def texter = "5793231309"
println texter.drop(texter.size() - 2)  //result: "09"

Or as a reusable closure:

def lastTwo = { it.drop(it.size() - 2) }

println lastTwo("ABC")  //result: "BC"
println lastTwo("AB")   //result: "AB"
println lastTwo("A")    //result: "A" (no exception thrown)
println lastTwo("")     //result: ""  (no exception thrown)

Fiddle with the code:
https://groovyconsole.appspot.com/script/5768158526832640

More examples of Groovy goodness:
http://mrhaki.blogspot.com/2011/09/groovy-goodness-take-and-drop-items.html

Another safe alternative using size() and substring() methods:

s?.size() < 2 ? s : s.substring(s.size() - 2)

Note the above takes care of nulls and strings that are less than two characters long.

In Java use

String texter = "5793231309";
String val=texter.substring(texter.length()-2,texter.length());
System.out.println("val-->"+val);

In Groovy you don’t need the above stuff just,

String value= texter[-2..-1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top