【问题标题】:How to check a string starts with a substring or not in java?如何在java中检查字符串是否以子字符串开头?
【发布时间】:2014-04-02 05:23:51
【问题描述】:

我想检查是否以http:// 开头的字符串。没有循环我怎么能做到这一点?提前致谢。

【问题讨论】:

  • if(str.indexOf("http://")==0)?
  • 您的第一站应该始终是文档。

标签: java string substring


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您基本上可以使用最简单的方法...即 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://"); 
    

    如果条件满足则返回真,否则返回假。

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 1970-01-01
      • 2023-01-01
      • 2019-08-24
      • 2015-04-15
      • 2012-02-06
      • 2011-02-16
      • 1970-01-01
      相关资源
      最近更新 更多