【问题标题】:How to check whether String contains "?" mark如何检查字符串是否包含“?”标记
【发布时间】:2014-04-30 06:03:21
【问题描述】:

我想检查字符串是否包含“?”标记与否。 我的字符串喜欢 "https://suort.mmo.com/jira/browse/AAA-157?fousedCommentId=120209&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-120209"

我想检查这个 url 字符串是否包含“?”标记或不使用 JavaScript。

【问题讨论】:

    标签: javascript html string substring


    【解决方案1】:

    更新:如果您使用 ES6,请使用 String.prototype.includes

    const containsQuestionMark = myStr.includes('?');

    否则,如果你卡在 ES5 上,use indexOf

    var containsQuestionMark = myStr.indexOf('?') > -1;
    

    【讨论】:

      【解决方案2】:

      检查这个网站:

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

      方法 indexOf 如果没有找到搜索词则返回 -1,所以做这个简单的 if 语句

      var s = 'string with a ? in it';
      
      if(s.indexOf('?') != -1) { /* code if string has a ? */ }else{ /*code if string does not have a ? */ }
      

      【讨论】:

      • 谢谢大家 我还找到了解决这个问题的另一种方法。var s = 'string with a ?在里面'; s.(/\?/);
      猜你喜欢
      • 2015-09-29
      • 2017-09-23
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 2020-12-12
      • 2017-02-27
      • 1970-01-01
      • 2011-03-31
      相关资源
      最近更新 更多