safe string slice

groovy

Solution

You can use `take`:

String shortString = longString.take( 250 )

Problem

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) ```

Original source