gpt4 book ai didi

java - 当我尝试打印此内容时,为什么新行会附加到我的数组元素中?

转载 作者:行者123 更新时间:2023-12-02 00:20:44 25 4
gpt4 key购买 nike

private int Index(String[] match,String keyword){
int m=0;
keyword=keyword+"1";
match[m]=match[m]+"1";
System.out.println("match:"+match[m]);
System.out.println("keyword:"+keyword);
System.out.println(match[m].equals(keyword));
while(!(match[m].equals("")) && !(match[m].equals(null))){
System.out.println("yes");
if(match[m].equals(keyword)){
break;
}
else
m++;
}
return m;
}

我得到以下输出(关键字的值为sparktg):

match:sparktg
1
keyword:sparktg1
false

为什么在 match[m] 的情况下,“sparktg”和“1”之间有一个新行?

最佳答案

如果您无法控制输入,则可以在使用输入之前执行trim()。这消除了任何 \n 和空格。

if(match[m] != null) {
System.out.println("match:"+match[m].trim());
}
if(keyword != null) {
System.out.println("keyword:"+keyword.trim());
}

您可以通过编写一个实用方法来执行此操作,从而使其更简洁。

public String sanitize(String input) {
return input != null ? input.trim() : null;
}

并按如下方式使用:

match[m] = sanitize(match[m]);
keyword = sanitize(keyword);

关于java - 当我尝试打印此内容时,为什么新行会附加到我的数组元素中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11049771/

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