在Groovy中,String.split(regex,int)不会产生预期的结果

时间:2016-03-19 21:47:59

标签: string groovy split

我尝试拆分字符串并保留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吗?有没有办法让这个游戏很好地融合在一起?

EDIT2: enter image description here

1 个答案:

答案 0 :(得分:2)

你肯定受到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。

此外,您可以看到String类在应用mixin之前有43种方法,然后显示String有149种方法,其中附加方法与{{1}中找到的方法相匹配}

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

当做混合时,它类似于currying,因为原来的String' str'作为第一个参数传递给StringUtils方法。因此,当使用具有2个参数和1的mixin时,2个语句等同于使用StringUtils.split()而没有mixin的2个语句,具有3个和2个参数。

更具体地说:

StringUtils

一旦你申请了mixin