簡體   English   中英

如何在java中檢查字符串是否以子字符串開頭?

[英]How to check a string starts with a substring or not in 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://"); 

滿足條件返回真,否則返回假。

暫無
暫無

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

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