簡體   English   中英

字符串以DRIVELETTER開頭:\\

[英]String startsWith DRIVELETTER:\

Java 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]+:[\\/]+.*")) {

您將需要一個正則表達式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]:\\\\\\\\")

您應該為此使用正則表達式。

Pattern p = Pattern.compile("^(http|[a-z]):");
Matcher m = p.matcher(str);
if(m.find()) {
   // do your stuff
}
String toCheck = ... // your String
if (toCheck.startsWith("http://")) {
   // starts with http://
} else if (toCheck.matches("^[a-zA-Z]:\\\\.*$")) {
    // is a drive letter
} else {
    // neither http:// nor drive letter
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM