【发布时间】:2020-01-24 12:23:23
【问题描述】:
我有以下字符串“让我成为三明治” 有人决定拖钓我并用随机数量的 LOL 替换空格。 所以现在字符串是“LOLMakeLOLLOLLOLMELOLALOLSandWich”
我的目标是恢复此更改。
我尝试使用 split 方法创建一个字符串数组,但这会导致数组内部的“空”元素具有一个值,但是当我尝试记录它时,它没有显示任何内容。也不等于""
Public class MyClass{
public static void main(String[] args) {
String trollText = "MakeLOLLOLLOLMELOLALOLSandWich";
String[] array = trollText.split("LOL");
if (array[1]=="")System.out.print("it's an empty string");
if (array[1]==" ")System.out.print("it's a space sign");
if (array[1]==null)System.out.print("it's equal to nothing");
if (array[1]==' '+"")System.out.print("I don't know what's that");
else System.out.print(array[1]+"<-- This is an element and it has a value");
}
}
如果有人告诉我 array[1] 等于什么,我认为问题解决了。 在将元素复制到新数组时,知道值会给我一些比较的东西。
【问题讨论】:
-
如果原始字符串包含 LOL 怎么办?
-
问题是:为什么?为什么不简单地使用
replace()方法? -
@user1474111 我们可以假设它不包含 LOL。
-
@Amongalen 这会给我留下重复的空格,这仍然不是我要寻找的答案。