gpt4 book ai didi

java - String.split 导致最后一个索引换行

转载 作者:行者123 更新时间:2023-11-29 05:02:46 24 4
gpt4 key购买 nike

当我拆分我的字符串时,我在最后一个索引后得到一个换行符,有人知道是什么原因造成的吗?

代码:

String string = "1234324:Kalle"

public void splitString(String string){
if (string.contains(":")) {
String[] parts = string.split(":");
returnedId = parts[0];
returnedUserName = parts[1];
System.out.println(returnedUserName + " this gets printed on a new line");
} else {
throw new IllegalArgumentException("String " + string + " does not contain :");
}
}

这会导致我的 UI 出现一些不需要的问题,我是否可以解决这个问题?

最佳答案

在拆分之前添加:

string = string.replaceAll("(\\r|\\n|\\t)", "");

这将删除任何 CRLF 或 TAB ASCII 字符。

关于java - String.split 导致最后一个索引换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31495535/

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