String.startsWith()中的快速问题,它需要某种通配符。
我需要检查链接是否以http://或本地驱动器(c:\、d:\等)开头,但我不知道驱动器号。
所以我想我需要像myString.startsWith("?:\\")这样的东西
有什么想法吗?
干杯
为此干杯,但我认为我需要在此基础上多加努力。
我现在需要迎合
1.http://
2.ftp://
3.file:///
4.c:\
5.\\这是过度杀戮,但我们想确定我们已经抓住了他们的全部。
我有过
if(!link.toLowerCase().matches("^[a-z]+:[\\/]+.*")) {它适用于任何一个或多个字符,后面跟着一个:(例如http:,ftp:,C:),覆盖1-4,但我不能满足\
我能得到的最接近的是这个(这很有效,但是最好能在regEx中得到它)。
if(!link.toLowerCase().startsWith("\\") && !link.toLowerCase().matches("^[a-z]+:[\\/]+.*")) {发布于 2013-05-29 15:04:02
您将需要一个正则表达式,startsWith不支持它
^[a-zA-Z]:\\\\.*
^ ^ ^ ^
| | | |
| | | everything is accepted after the drive letter
| | the backslash (must be escaped in regex and in string itself)
| a letter between A-Z (upper and lowercase)
start of the line那么您可以使用yourString.matches("^[a-zA-Z]:\\\\")
发布于 2013-05-29 15:02:47
为此您应该使用正则表达式。
Pattern p = Pattern.compile("^(http|[a-z]):");
Matcher m = p.matcher(str);
if(m.find()) {
// do your stuff
}https://stackoverflow.com/questions/16817354
复制相似问题