读取子字符串时遇到Java StringIndexOutOfBoundsException
所以我有一个java类,它读取一个有多行的文件,每当它到达这个部分读取子字符串时遇到Java StringIndexOutOfBoundsException,java,stringindexoutofbounds,Java,Stringindexoutofbounds,所以我有一个java类,它读取一个有多行的文件,每当它到达这个部分 F C Y 它应该读取它的值(返回F,C,Y) 我尝试过ClassString=temp.substring(0,20.trim() 但它总是返回StringIndexOutOfBoundsException(eString索引超出范围:20) 不知道为什么,我从一开始就数了那一行,Y在索引20中。我会这样做,当然可能需要一些错误检查 temp = temp.trim(); String[] resul
F C Y
它应该读取它的值(返回F,C,Y)
我尝试过ClassString=temp.substring(0,20.trim()
但它总是返回StringIndexOutOfBoundsException(eString索引超出范围:20)
不知道为什么,我从一开始就数了那一行,Y在索引20中。我会这样做,当然可能需要一些错误检查
temp = temp.trim();
String[] result = new String[] {String.valueOf(temp.charAt(0)),
temp.substring(1, temp.length()-2).trim(),
String.valueOf(temp.charAt(temp.length()-1))};
或者作为字符数组
temp = temp.trim();
char[] result2 = new char[] {temp.charAt(0),
temp.substring(1, temp.length()-2).trim().charAt(0),
temp.charAt(temp.length()-1)};
先生,总长度或温度为17
temp.子字符串(0,17)。trim()
适用于您的情况。