public String[] split(String regex) Splits this string around matches of the given regular expression. This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result : { "boo", "and", "foo" } o { "b", "", ":and:f" }
Chapter 8. Exceptions When used to best advantage, exceptions can improve a program's readability, reliability, and maintainability. When used improperly, they can have the opposite effect. This chapter provides guidelines for using exceptions effectively. -------------------------------------------------------------- From <<Effective Java Programming Language Guide>> --------------------------------------------------------------这两条是对异常的一点说明 • Because exceptions are designed for use under exceptional circumstances, few, if any, JVM implementations attempt to optimize their performance. It is generally expensive to create, throw, and catch an exception. • Placing code inside a try-catch block precludes certain optimizations that modern JVM implementations might otherwise perform.如果你要简单的话,异常可以;但你要保证是有质量的,高效的代码,建议使用正则。Exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow.
System.out.println("01104548".match("\\d+"));
楼上的想说明什么问题,两个测试结果有区别吗??? 附: java.lang.String: public boolean matches(String regex) { return Pattern.matches(regex, this); }java.util.regex.Pattern public static boolean matches(String regex, CharSequence input) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); return m.matches(); }孟子云:尽信“书”不如无“书” 以下是连续5次的测试结果 Use RegEx cost: 47 Use Exception catching cost: 0Use RegEx cost: 32 Use Exception catching cost: 0Use RegEx cost: 32 Use Exception catching cost: 0Use RegEx cost: 46 Use Exception catching cost: 16Use RegEx cost: 47 Use Exception catching cost: 0 实在看不出正则式高效在哪? 原因自己看java.util.regex包源码!
呵呵,实际上一句话也就搞定了阿 public class Test { /** * @param args */ public static void main(String[] args) { String s = "name"; if(!s.matches("^\\d")){ System.out.println("Input error!"); } }}
如果需求只是那么简单的话,象Long/Integer/Float....都有 valueOf(String s)的方法,你可以调用一下,然后catch NumberFormatException.
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
try {
Long.parseLong(s);
System.out.println("成功.");
} catch(Exception e) {
System.out.println("错误!字符串里只能有数字.");
}
if (string.charAt(i) > 32 && cityname.charAt(i) < 42)
{
System.out.println("ok");
}
else
System.out.println("error");
}
ChDw(米)
woolceo(努力升仙)
同意
public static void main (String[] args) {
String tmp = "1a1";
long start = System.currentTimeMillis();
String regEx = "^\\d+$";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(tmp);
System.out.println(matcher.find());
System.out.println("Use RegEx cost: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
Long.valueOf(tmp);
} catch (NumberFormatException nfe) {
System.out.println("Use Exception catching cost: " + (System.currentTimeMillis() - start));
}
}
When used to best advantage, exceptions can improve a program's readability, reliability, and maintainability. When used improperly, they can have the opposite effect. This chapter provides guidelines for using exceptions effectively.
--------------------------------------------------------------
From <<Effective Java Programming Language Guide>>
--------------------------------------------------------------这两条是对异常的一点说明
• Because exceptions are designed for use under exceptional circumstances, few, if any, JVM implementations attempt to optimize their performance. It is generally expensive to create, throw, and catch an exception.
• Placing code inside a try-catch block precludes certain optimizations that modern JVM implementations might otherwise perform.如果你要简单的话,异常可以;但你要保证是有质量的,高效的代码,建议使用正则。Exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow.
附:
java.lang.String:
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}java.util.regex.Pattern
public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}孟子云:尽信“书”不如无“书”
以下是连续5次的测试结果
Use RegEx cost: 47
Use Exception catching cost: 0Use RegEx cost: 32
Use Exception catching cost: 0Use RegEx cost: 32
Use Exception catching cost: 0Use RegEx cost: 46
Use Exception catching cost: 16Use RegEx cost: 47
Use Exception catching cost: 0
实在看不出正则式高效在哪?
原因自己看java.util.regex包源码!
public class Test { /**
* @param args
*/
public static void main(String[] args) {
String s = "name";
if(!s.matches("^\\d")){
System.out.println("Input error!");
}
}}
要效率的,用StringTokenizer把原字符串按","分割成若干段,然后逐段按catch exception判断。
要遵循principle的写出优雅代码的,自己写正则。
if (Pattern.matches("\\d\\d,\\d\\d,\\d\\d,\\d\\d,\\d\\d,\\d\\d#\\d\\d", str)) {
System.out.println("good !");
} else {
System.out.println("bad !");
}
} public static void main(String[] args) {
check("01,02,03,04,05,06#01");
check("X1,02,03,04,05,06#01");
}}
用Long.parseLong(s)方式可以,
不过我个人更推荐用正则表达式,
给你个实例------------------------------------------
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;public class Test extends JFrame implements KeyListener {
JTextField tf = new JTextField(20); int limit = 10; public Test() { JPanel jp = new JPanel();
tf.addKeyListener(this);
jp.add(tf);
getContentPane().add(jp);
setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(new Dimension(400, 300));
setVisible(true);
} public void keyTyped(KeyEvent e) {
if (tf.getText().length() >= limit) {
getToolkit().beep();
e.consume();
System.out.println("Value cannot greater than 10");
JOptionPane.showMessageDialog(null, "Value cannot greater than 10");
}
} public void keyReleased(KeyEvent e) {
} public void keyPressed(KeyEvent e) {
} public static void main(String[] a) {
new Test();
}
}