有时候真的学艺不精。。。
故做此篇,以警之。
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String first = "ni'hao'";
String third = null;
// System.out.println("-->" + first.lastIndexOf("'") + "<--"+ first.length());
//好不靠谱
if ( (first.substring(first.lastIndexOf("'") + 1)!="") ) {
third = first.substring(first.lastIndexOf("'") + 1);
System.out.println("-->" + third + "<--");
} else {
System.out.println("err");
}
//可靠
if ( !(first.substring(first.lastIndexOf("'") + 1).equals("")) ) {
third = first.substring(first.lastIndexOf("'") + 1);
System.out.println("-->" + third + "<--");
} else {
System.out.println("err");
}
}
}
输出结果:
--><--
err
结果显而易见,两种比较,完全是不同的。。。
顺便留心一下 substring()方法和给出的例子:
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Examples:
"unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string)
an empty string!
有时候一不留神就跳坑了,警示一下