2013-03-21 118 views
4

我有一个字符串,我想要250个字符或更少。我是这样做Java的方式,但是是有这样的常规快捷键:安全字符串切片

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) 

回答