我正在尝试使用空格分隔我的字符串并提取字符串值的第二个文本。
这是我的代码 -
String title = 'testing abcdefgh.abc.cde.fgh test testing issue'
String[] test = title.split(" ");
String[] newTest = test[1];
println newTest
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出 - [a, b, c, d, ., a, b, c, ., c, d, e, ., f, g, h]
现在我正在寻找的输出是 abcd.abc.cde.fgh,但我得到 [a, b, c, d, ., a, b, c, ., c, d, e, ., f, g , H]
我使用过 ("\s+"), ("\s"),只是括号内的空格,单引号和双引号括起来的空格,没有任何效果。
是因为这里
String[] newTest = test[1]
Run Code Online (Sandbox Code Playgroud)
您告诉 groovy 将您想要的字符串粘贴到字符串数组中
所以 groovy 尝试按照你说的做,并将字符拆分为单独的字符串
你想要的只是一个字符串,而不是一个字符串数组。尝试
String newTest = test[1]
Run Code Online (Sandbox Code Playgroud)
反而