Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

safe string slice

Tags:

groovy

I have a String that I want 250 chars or less. I was doing it the java way, but is there a groovy shortcut for this:

def longString = "This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string.This is my really long string."
def shortString = "This is my really short string."

#ideal would be something like:
return longString[0..250]
#versus how i currently have it
#how can i simplify this one...
return shortString.size() < 250? shortString: shortString.substring(0,250) 
like image 260
Nix Avatar asked Mar 21 '13 14:03

Nix


1 Answers

You can use take:

String shortString = longString.take( 250 )
like image 89
tim_yates Avatar answered Oct 15 '22 20:10

tim_yates