How to check a string starts with a substring or not in java?

后端 未结 2 1685
陌清茗
陌清茗 2020-12-22 13:54

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

相关标签:
2条回答
  • 2020-12-22 14:16

    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
    
    0 讨论(0)
  • 2020-12-22 14:24

    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.

    0 讨论(0)
提交回复
热议问题