如何在java中检查字符串是否以子字符串开头?
我想检查一个以http://
开头的字符串。 如果没有循环我怎么能这样做? 提前致谢。
在String API中使用public boolean startsWith(String prefix)
例如: boolean isStartsWith = YOUR_STRING.startsWith("http://");
String tst = "http://web.com"; System.out.print(tst.startsWith("http://")); //prints true
您基本上可以使用最简单的方法…即String API提供的方法。
public boolean startsWith(String prefix, int toffset) or public boolean startsWith(String prefix)
例如 :
String str = new String("http://www.google.com"); str.startsWith("http://");
如果条件满足则返回true,否则返回false。