我试图拆分一个字符串并保留有n + 1分隔符的n项。 SO上有许多解决方案,建议使用.split(regex, -1)检索所有令牌。但是,当在Groovy中尝试时,这是行不通的。

println ",,,,,,".split(",", -1).length


打印0

知道我可以采取什么措施来获得与Java方法一致的行为吗?对其调用.toString()没什么区别(将GString转换为java.lang.String

编辑:我的脚本中也有String.mixin StringUtils。因为StringUtils没有定义.split(regex, int)方法,所以没有方法签名冲突。我是否使用了不正确的mixin?有什么办法可以让这场比赛很好地在一起吗?

编辑2:
string - 在Groovy中,String.split(regex,int)不会产生预期的结果-LMLPHP

最佳答案

您肯定会受到StringUtils类混合的影响。如果运行此命令:

import org.apache.commons.lang3.StringUtils
String str = ",,,,,,"

println "String..."
println str.split(",",-1).length
println str.split(",").length
def methods = String.metaClass.methods*.name.sort().unique()
println "$methods.size:$methods"

println "\nStringUtils..."
println StringUtils.split(str, ",").length
println StringUtils.split(str,",",-1).length

println "\nStringUtils mixin..."
String.mixin StringUtils
println str.split(",",-1).length
println str.split(",").length
methods = String.metaClass.methods*.name.sort().unique()
println "$methods.size:$methods"


您将获得输出:

String...
7
0
43:[charAt, codePointAt, codePointBefore, codePointCount, compareTo, compareToIgnoreCase, concat, contains, contentEquals, copyValueOf, endsWith, equals, equalsIgnoreCase, format, getBytes, getChars, getClass, hashCode, indexOf, intern, isEmpty, join, lastIndexOf, length, matches, notify, notifyAll, offsetByCodePoints, regionMatches, replace, replaceAll, replaceFirst, split, startsWith, subSequence, substring, toCharArray, toLowerCase, toString, toUpperCase, trim, valueOf, wait]

StringUtils...
0
0

StringUtils mixin...
0
0
149:[abbreviate, abbreviateMiddle, appendIfMissing, appendIfMissingIgnoreCase, capitalize, center, charAt, chomp, chop, codePointAt, codePointBefore, codePointCount, compareTo, compareToIgnoreCase, concat, contains, containsAny, containsIgnoreCase, containsNone, containsOnly, containsWhitespace, contentEquals, copyValueOf, countMatches, defaultIfBlank, defaultIfEmpty, defaultString, deleteWhitespace, difference, endsWith, endsWithAny, endsWithIgnoreCase, equals, equalsIgnoreCase, format, getBytes, getCR, getChars, getClass, getEMPTY, getFuzzyDistance, getINDEX_NOT_FOUND, getJaroWinklerDistance, getLF, getLevenshteinDistance, getPAD_LIMIT, getSPACE, hashCode, indexOf, indexOfAny, indexOfAnyBut, indexOfDifference, indexOfIgnoreCase, intern, isAllLowerCase, isAllUpperCase, isAlpha, isAlphaSpace, isAlphanumeric, isAlphanumericSpace, isAsciiPrintable, isBlank, isEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, lastIndexOf, lastIndexOfAny, lastIndexOfIgnoreCase, lastOrdinalIndexOf, left, leftPad, length, lowerCase, matches, mid, normalizeSpace, notify, notifyAll, offsetByCodePoints, ordinalIndexOf, overlay, prependIfMissing, prependIfMissingIgnoreCase, regionMatches, remove, removeEnd, removeEndIgnoreCase, removePattern, removeStart, removeStartIgnoreCase, repeat, replace, replaceAll, replaceChars, replaceEach, replaceEachRepeatedly, replaceFirst, replaceOnce, replacePattern, reverse, reverseDelimited, right, rightPad, setCR, setEMPTY, setINDEX_NOT_FOUND, setLF, setPAD_LIMIT, setSPACE, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithAny, startsWithIgnoreCase, strip, stripAccents, stripEnd, stripStart, stripToEmpty, stripToNull, subSequence, substring, substringAfter, substringAfterLast, substringBefore, substringBeforeLast, substringBetween, substringsBetween, swapCase, toCharArray, toLowerCase, toString, toUpperCase, trim, trimToEmpty, trimToNull, uncapitalize, upperCase, valueOf, wait, wrap]


表明在没有mixin StringUtils的情况下行为是不同的。
实际上,如果您加上-1和0,则普通Groovy会返回7,这是使用通用Java split()方法的结果,而Java和Groovy都返回相同的结果。

StringUtils在带或不带-1参数的情况下将0调整为0。

此外,您可以看到String类在应用mixin之前有43种方法,然后显示String有149种方法,其中其他方法与StringUtils中的方法匹配

因此,您会注意到println "\StringUtils..."语句后的两行输出与在StringUtils中混合执行时输出的结果相同,两个语句都返回0。

进行混合时,它类似于currying,因为原始的字符串'str'作为第一个参数传递给StringUtils.split()方法。因此,使用mixin时分别具有2个参数和1的2条语句等效于使用StringUtils而不使用mixin且具有3个和2个参数的2条语句。

进一步来说:

str.split(",",-1) == StringUtils.split(str, ",", -1)


一旦应用了mixin

10-05 19:47