我试图在java中提取部分字符串,但是我收到错误:
java.lang.StringIndexOutOfBoundsException: String index out of range: -23
代码:
String searchLine = "feng shui";
String str = "Feng shui (i pinyin: fēng shuǐ) is a Chinese philosophical system of harmonizing everyone with the surrounding environment. The term feng shui literally ...";
String pstr= str.substring(str.indexOf(searchLine)+1, str.indexOf("."));
我希望字符串从它找到的位置直到“。”
有谁知道如何解决这个问题?
答案 0 :(得分:1)
根据http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring%28int,%20int%29,如果endIndex
位于startIndex
之前,您将获得例外。
在您的情况下,字符串.
中的第一个str
字符位于第一个feng shui
之前(资本化和确切的字符匹配很重要),这会导致异常。
"Feng shui (i pinyin: fēng shuǐ) is a Chinese philosophical system of
harmonizing everyone with the surrounding environment. The term feng shui literally ...";
^ ^