Java 字符串以DRIVELETTER开头:\
JavaJava 字符串以DRIVELETTER开头:\,java,string,startswith,Java,String,Startswith,JavaString.startsWith()中的快速问题,它需要某种通配符 我需要检查链接是否以http://或本地驱动器(c:\,d:\等)开头,但我不知道驱动器号 所以我想我需要类似于myString.startsWith(“?:\\”)的东西 有什么想法吗 干杯 为此干杯,但我想我需要在这基础上再接再厉 我现在需要照顾你 1.http:// 2.ftp:// 3.file:/// 4.c:\ 5.\\ 这太过分了,但我们要确保我们抓住了他们 我有 if(!link.toLowerCas
String.startsWith()
中的快速问题,它需要某种通配符
我需要检查链接是否以http://
或本地驱动器(c:\
,d:\
等)开头,但我不知道驱动器号
所以我想我需要类似于myString.startsWith(“?:\\”)的东西
有什么想法吗
干杯
为此干杯,但我想我需要在这基础上再接再厉
我现在需要照顾你
1.http://
2.ftp://
3.file:///
4.c:\
5.\\
这太过分了,但我们要确保我们抓住了他们
我有
if(!link.toLowerCase().matches("^[a-z]+:[\\/]+.*")) {
它适用于任何一个或多个后跟a:(例如http:,ftp:,C:)的字符,包括1-4个字符,但我不能满足\\
我能得到的最接近的是这个(它可以工作,但最好在正则表达式中得到它)
您应该为此使用正则表达式
Pattern p = Pattern.compile("^(http|[a-z]):");
Matcher m = p.matcher(str);
if(m.find()) {
// do your stuff
}
您将需要一个不受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]:\\\\\”
使用正则表达式,startsWith
在这里帮不了你。伙计们,干杯,有这么多快速的响应,但我要选择的是@Jack-^[a-zA-Z]:\\\\\\.*谢谢你,这是我一直使用的示例,它看起来效果不错。只是好奇,为什么需要\\\\n在正则表达式中表示\呢?我可以理解\\(转义),但为什么要四个反斜杠?因为\即使在正则表达式中也是转义字符,所以正则表达式应该是“\\”以表示一个反斜杠。但要将\\放在一个字符串中,必须将它们都转义为“\\\\”
Pattern p = Pattern.compile("^(http|[a-z]):");
Matcher m = p.matcher(str);
if(m.find()) {
// do your stuff
}
^[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