indexof它所查出来的是你给的母字符中某一个字符的下标位置,
以下引用自java1.6api文档:
Returns the index within this string of the first occurrence of the specified
character. If a character with value ch
occurs in the character
sequence represented by this String
object, then the index (in
Unicode code units) of the first such occurrence is returned.
还是举个例子吧
这里用这个方法定义一个验证输入字符为数字的方法:
public void KeyTyped(KeyEvent e){
String key="-0123456789.*";
if(key.indexOf(e.getKeyChar())<0)//如果getKeyChar返回的字符是是数字,那么indexOf返回的必然是这个数字在字符串"-0123456789.*"的下标
//如果返回值小于0 ,则输入字符不是数字,然后调用consume()销毁该输入即可
e.consume();//销毁输入的非法字符
}