How to check a string starts with a substring or n

2019-01-29 14:31发布

问题:

I want to check a string which starts with http:// or not. How can I do that without loop? Thanks in advance.

回答1:

use public boolean startsWith(String prefix) in String API

eg : boolean isStartsWith = YOUR_STRING.startsWith("http://");

String tst = "http://web.com";
System.out.print(tst.startsWith("http://")); //prints true


回答2:

You can basically use the simplest method... i.e methods provided with the String API..

public boolean startsWith(String prefix, int toffset)
                 or
public boolean startsWith(String prefix)

For example :

String str = new String("http://www.google.com");
str.startsWith("http://"); 

It returns true if the condition is met else will return false.