Java String split返回长度为0

时间:2023-01-29 21:41:51
public int lengthOfLastWord(String s) {
        s.replaceAll("\\s", "");
        String[] splittedS = s.split("\\s+");
        if(splittedS.length == 1 && splittedS[0].equals("")) return 0;
        return splittedS[splittedS.length - 1].length();
    }

I tested it out with the string " ", and it returns that the length of splittedS is 0.

我用字符串“”测试了它,然后返回splittedS的长度为0。

When I trimmed the String did I get " " -> "", so when I split this, I should have an array of length with with the first element being ""?

当我修剪字符串时,我得到“” - >“”,所以当我拆分它时,我应该有一个长度数组,第一个元素是“”?

3 个解决方案

#1


2  

Java Strings are immutable so you have to store the reference to the returned String after replacement because a new String has been returned. You have written,

Java字符串是不可变的,因此您必须在替换后存储对返回的String的引用,因为已返回新的String。你写,

s.replaceAll("\\s", "");

But write, s = s.replaceAll("\\s", ""); instead of above.

但是写,s = s.replaceAll(“\\ s”,“”);而不是上面。

Wherever you perform operations on String, keep the new reference moving further.

无论您在String上执行操作,都要保持新引用的进一步移动。

#2


0  

The call to replaceAll has no effect, but since you split on \\s+, split method works exactly the same: you end up with an empty array.

对replaceAll的调用没有任何效果,但是由于你在\\ s +上拆分,split方法的工作原理完全相同:你最终得到一个空数组。

Recall that one-argument split is the same as two-argument split with zero passed for the second parameter:

回想一个参数拆分与两个参数拆分相同,第二个参数传递零:

String[] splittedS = s.split("\\s+", 0);
//                                  ^^^

This means that regex pattern is applied until there's no more changes, and then trailing empty strings are removed from the array.

这意味着应用正则表达式模式,直到不再有更改,然后从数组中删除尾随空字符串。

This last point is what makes your array empty: the application of \\s+ pattern produces an array [ "" ], with a single empty string. This string is considered trailing by split, so it is removed from the result.

最后一点是使你的数组​​为空的原因:\\ s + pattern的应用程序产生一个数组[“”],只有一个空字符串。此字符串被视为通过拆分尾随,因此将从结果中删除它。

This result is not going to change even if you fix the call to replaceAll the way that other answers suggest.

即使您按照其他答案建议的方式修复了对replaceAll的调用,此结果也不会改变。

#3


0  

You need to re assign the variable

您需要重新分配变量

s=s.replaceAll(...)

#1


2  

Java Strings are immutable so you have to store the reference to the returned String after replacement because a new String has been returned. You have written,

Java字符串是不可变的,因此您必须在替换后存储对返回的String的引用,因为已返回新的String。你写,

s.replaceAll("\\s", "");

But write, s = s.replaceAll("\\s", ""); instead of above.

但是写,s = s.replaceAll(“\\ s”,“”);而不是上面。

Wherever you perform operations on String, keep the new reference moving further.

无论您在String上执行操作,都要保持新引用的进一步移动。

#2


0  

The call to replaceAll has no effect, but since you split on \\s+, split method works exactly the same: you end up with an empty array.

对replaceAll的调用没有任何效果,但是由于你在\\ s +上拆分,split方法的工作原理完全相同:你最终得到一个空数组。

Recall that one-argument split is the same as two-argument split with zero passed for the second parameter:

回想一个参数拆分与两个参数拆分相同,第二个参数传递零:

String[] splittedS = s.split("\\s+", 0);
//                                  ^^^

This means that regex pattern is applied until there's no more changes, and then trailing empty strings are removed from the array.

这意味着应用正则表达式模式,直到不再有更改,然后从数组中删除尾随空字符串。

This last point is what makes your array empty: the application of \\s+ pattern produces an array [ "" ], with a single empty string. This string is considered trailing by split, so it is removed from the result.

最后一点是使你的数组​​为空的原因:\\ s + pattern的应用程序产生一个数组[“”],只有一个空字符串。此字符串被视为通过拆分尾随,因此将从结果中删除它。

This result is not going to change even if you fix the call to replaceAll the way that other answers suggest.

即使您按照其他答案建议的方式修复了对replaceAll的调用,此结果也不会改变。

#3


0  

You need to re assign the variable

您需要重新分配变量

s=s.replaceAll(...)