我试图使用string.index和string.length拆分字符串,但我得到一个错误,该字符串超出范围。我该如何解决这个问题?如何从第一个空间发生的字符串拆分Java
while (in.hasNextLine()) {
String temp = in.nextLine().replaceAll("[<>]", "");
temp.trim();
String nickname = temp.substring(temp.indexOf(' '));
String content = temp.substring(' ' + temp.length()-1);
System.out.println(content);
试想,如果没有'”“'在'temp',然后处理这种情况会发生什么。 – Zircon
''''具有32位的ASCII值,所以'''+ temp.length() - 1'将大于32,并且我怀疑'temp.length()'大于32.你需要使用'temp.indexOf('')'而不是''''并且不要添加'temp.length() - 1'。 –