public static void main(String[] args) {
String st, ss = "什么是数据库";
st = ss.substring(2, 2);
System.out.print(st);
}
可是输出结果什么都没有,也没有报错!晕!
String st, ss = "什么是数据库";
st = ss.substring(2, 2);
System.out.print(st);
}
可是输出结果什么都没有,也没有报错!晕!
String st,ss = "什么是数据库";
st = ss.substring(2,3);
System.out.print(st);
substring(截取开始位置,截取结束位置);
从2开始,在2结束,当然什么的没截取到.
API的疑问,lz要多多查阅文档.
int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples: "hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
st = ss.substring(2, 2); //是index2开始index2!
改为
st = ss.substring(2, 4);//这就是你想要的结果!
你是不是理解错误了substring()方法阿?
第一个参数是从哪里起 第二个是结束 你这里从2起截到2 肯定没东西阿!
你是被JAVASCRIPT里面的substr()搞混淆了吧!
substring——截取开始位置......截取结束位置(不包括);
例如:
public static void main(String[] args) {
String st, ss = "什么是数据库";
st = ss.substring(1, 7); }
要把“什么是数据库”,的全部提取出来,该怎么写!
起始位置2,结束位置2,2-2=0,能打出东西就怪了。
这个要注意和sql中的区分,我估计你是记成sql中的了。
public String substring(int beginIndex,
int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
示例: "hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"