英文:
JAVA: Delimiter removed when reversing a string
问题
如何在下面的代码中,在将字符串反转时包含分隔符?
Pattern.compile(Pattern.quote("."))
.splitAsStream("BCTVW.")
.map(s->new StringBuilder(s).reverse())
.collect(Collectors.joining("."))
当前输出:WVTCB.
期望输出:.WVTCB
期望输出中缺少了分隔符。
英文:
How can I include the delimiter when reversing my string via code below?
Pattern.compile(Pattern.quote("."))
.splitAsStream("BCTVW.")
.map(s->new StringBuilder(s).reverse())
.collect(Collectors.joining("."))
Current output: WVTCB.
Desired output: .WVTCB
The delimiter is missing from the desired output.
答案1
得分: 1
你可以使用非捕获组,例如前瞻。
Pattern.compile("(?<=\\.)")
.splitAsStream("BCTVW.")
.map(s->new StringBuilder(s).reverse())
.collect(Collectors.joining("."));
英文:
You can use non-capturing groups, e.g. lookahead.
Pattern.compile("(?<=\\.)")
.splitAsStream("BCTVW.")
.map(s->new StringBuilder(s).reverse())
.collect(Collectors.joining("."));
答案2
得分: 1
Pattern(regex).splitAsStream(data)
具有与data.split(regex)
类似的功能,因此它也会删除作为分割结果而创建的尾随空字符串。
例如,"a,b,".split(",")
不会导致["a","b",""]
,而是["a","b"]
,因为数组末尾的空字符串会被删除。
Pattern#splitAsStream
和String#split
之间最关键的区别是String#split(regex)
方法具有重载版本String#split(regex, limit)
,其中limit
的值会影响其结果。
如果limit
为:
正数
,它会设置结果数组中的最大元素数量,例如"a,b,c,d".split(",", 3)
会得到["a", "b", "c,d"]
。0
,它会导致删除尾随的空字符串(split(regex,0)
与split(regex)
相同)。负数
- 阻止split
删除尾随的空字符串 - 这是您想要的效果。
因此,不要使用Pattern#splitAsStream
,而是使用您在先前问题中尝试过的String#split
方法:
> 引用以防先前的问题被移除
> > String fileContent = "Abc.134dsq"; > String delimiter = "."; > fileContent = fileContent.replace(delimiter, "-"); > String[] splitWords = fileContent.split("-"); > StringBuilder stringBuilder = new StringBuilder(); > for (String word : splitWords) { > StringBuilder output = new StringBuilder(word).reverse(); > stringBuilder.append(output); > } > > System.out.println(stringBuilder.toString()); >
您需要纠正的问题:
-
不要将字符串中的
delimiter
替换为-
以便在-
上分割,因为您的字符串可能包含自己的-
。例如,将.
替换为-
之后,"A.B-C"
将变为A-B-C
,这将导致不需要的B-C
分割。而是使用
Pattern.quote(delimiter)
进行分割 - 就像您在当前问题中提供的示例中所做的那样。 -
使用带有负限制参数的
split
,以防止删除尾随的空字符串(类似于fileContent.split(Pattern(delimiter), -1)
)。 -
您没有将分隔符添加回结果字符串。一个简单的解决方案是使用
StringJoiner joiner = new StringJoiner(delimiter);
joiner.add(reversedElement)
,而不是使用StringBuilder
,因为joiner.add
还会处理我们要连接的元素之间的分隔符添加。
英文:
Pattern(regex).splitAsStream(data)
has similar functionality as data.split(regex)
so it also removes trailing empty strings created as result of splitting.
For instance "a,b,".split(",")
would NOT result in ["a","b",""]
but ["a","b"]
because empty strings at the end of array would be removed.
Most crucial difference between Pattern#splitAsStream
and String#split
is that String#split(regex)
method has overloaded version String#split(regex, limit)
where limit
depending on value affects its result.
If limit is
positive
it sets maximal amount of elements in resulting array like"a,b,c,d".split(",", 3)
would result in["a", "b", "c,d"]
.0
it causes removal of trailing empty strings (split(regex,0)
is same assplit(regex)
negative
- preventssplit
from removing trailing empty strings - this is what you are after.
So don't use Pattern#splitAsStream
but instead String#split
method like you tried in your previous question:
> quotation in case previous question would be removed
> > String fileContent = "Abc.134dsq"; > String delimiter = "."; > fileContent = fileContent.replace(delimiter, "-"); > String[] splitWords = fileContent.split("-"); > StringBuilder stringBuilder = new StringBuilder(); > for (String word : splitWords) { > StringBuilder output = new StringBuilder(word).reverse(); > stringBuilder.append(output); > } > > System.out.println(stringBuilder.toString()); >
Things you need to correct there:
-
DONT replace
delimiter
in your string to-
to split on-
because your string may contain its own-
. For instance"A.B-C"
after replacing.
to-
will becomeA-B-C
which will result in unwanted splittingB-C
.Instead split using
Pattern.quote(delimiter)
- like you did in example provided in current question. -
Use
split
with negative limit parameter to prevent removing trailing empty strings (something likefileContent.split(Pattern(delimiter), -1)
) -
You are not adding delimiters back to resulting string. Easy solution would be using
StringJoiner joiner = new StringJoiner(delimiter);
joiner.add(reversedElement)
instead ofStringBuilder
becausejoiner.add
will also handle adding delimiter between elements we want to join.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论