gpt4 book ai didi

groovy - 安全字符串切片

转载 作者:行者123 更新时间:2023-12-03 21:23:26 25 4
gpt4 key购买 nike

我有一个字符串,我想要 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)

最佳答案

You can use take :

String shortString = longString.take( 250 )

关于groovy - 安全字符串切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com