gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:53:15 26 4
gpt4 key购买 nike

我正在尝试拆分字符串并保留有 n 分隔符的 n + 1 项。 SO 上有很多解决方案建议使用 .split(regex, -1) 来检索所有 token 。然而,在 Groovy 中尝试时,这不起作用。

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

打印 0

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

编辑:我的脚本中还有 String.mixin StringUtils 。由于 StringUtils 没有定义 .split(regex, int) 方法,因此不存在冲突的方法签名。我是否错误地使用了mixin?有什么办法可以让这个游戏很好地结合在一起吗?

编辑2: enter image description here

最佳答案

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

此外,您可以看到在应用 mixin 之前,String 类有 43 个方法,然后显示 String 有 149 个方法,其中附加方法与这些方法相匹配在StringUtils

中找到

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

在进行 mixin 时,它与柯里化(Currying)类似,原始字符串“str”作为第一个参数传递给 StringUtils.split() 方法。因此,使用 mixin 时分别有 2 个参数和 1 个参数的 2 个语句相当于使用 StringUtils 而不使用 mixin 时的 2 个语句,分别有 3 个参数和 2 个参数。

更具体地说:

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

应用 mixin 后

关于string - 在 Groovy 中,String.split(regex, int) 不会产生预期的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36107597/

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