2013-03-21 216 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) 

回答