gpt4 book ai didi

Java string.split ("\\n")

转载 作者:行者123 更新时间:2023-12-02 05:16:22 25 4
gpt4 key购买 nike

String[] lines = ascii.getText().split("\\n");
for(int i = 0; i < lines.length; i++){
System.out.println(lines[i]);
if(lines[i] == "")
System.out.println("abc");
}

ascii是一个 JTextArea。这是一个示例代码,我只是在将其放入程序之前对其进行测试。现在,它将来自 ascii 的行分割成单独的字符串,然后显示它们。

它正确地执行了此操作,并包含我想要的空行。现在我想检查行是否为空(它只能包含空格)。我尝试过组合

if(lines[i] == "", " ", "\n"...)

但这些都不起作用。谁能解释一下怎么做吗?

最佳答案

but what is actually in the empty string? I've tried if(lines[i] == "", " ", "\n"... and none of those worked. Can anyone help explain or lead me to what thats called?

如果空行仅包含空白字符,您可以使用trim()删除它们。并调用isEmpty()

if (lines[i].trim().isEmpty()) {
continue;
}

关于Java string.split ("\\n"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26922402/

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