当我拆分我的字符串时,我在最后一个索引后得到一个换行符,有人知道是什么原因造成的吗?
代码:
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/