【问题标题】:Extract sub string from last in a string JS [duplicate]从字符串JS中的最后一个提取子字符串[重复]
【发布时间】:2013-07-28 23:15:02
【问题描述】:

我需要编写JS函数,如果字符串最后包含- depreciated,则返回true,否则返回false。

例如:

var somestring = "string value - depreciated";

函数应该在上面的例子中返回 true。

function isDepreciated(var S)
{
    //Need to check for substring in last
    //return true or false
}

一种可能的解决方案是使用search 函数,但这意味着如果- depreciated 出现在字符串中,那么它也会返回true。我真的需要找到天气子字符串是否在最后。

请帮忙。

【问题讨论】:

  • 家庭作业?你试过什么?
  • 了解 matchRegExp 并尝试一下。
  • 您也可以通过使用substring 来隔离输入的最后 X 个字符并查看它们是否与您要查找的内容匹配。

标签: javascript


【解决方案1】:

这里已经有很多答案了(首选带有 $ 的那个),尽管我也必须写一个,所以它也可以完成你的工作,

var somestring = "string value - depreciated";
var pattern="- depreciated";

function isDepreciated(var s)
{
    b=s.substring(s.length-pattern.length,s.length)==pattern;
}

【讨论】:

    【解决方案2】:

    你可以使用柯里化:http://ejohn.org/blog/partial-functions-in-javascript/

    Function.prototype.curry = function() {
        var fn = this, args = Array.prototype.slice.call(arguments);
        return function() {
          return fn.apply(this, args.concat(
            Array.prototype.slice.call(arguments)));
        };
      };
    

    使用辅助 curry 函数,您可以创建 isDepriated 检查:

    String.prototype.isDepricated = String.prototype.match.curry(/- depreciated$/);
    
    "string value - depreciated".isDepricated();
    

    或者使用.bind():

    var isDepricated = RegExp.prototype.test.bind(/- depreciated$/);
    
    isDepricated("string value - depreciated");
    

    【讨论】:

    • 要解决的问题更具体,所以我不会为此修改字符串原型。
    • 你可能是对的,所以我添加了一个bind 示例;)
    • 我知道你是 JavaScript 活力的狂热粉丝 ;-) +1 创意方面呵呵
    【解决方案3】:

    好的,我还没有在浏览器上运行此代码,但这应该可以让您大致了解该做什么。如果需要,您可能需要调整一些条件。

    var search = "- depricated";
    var pos = str.indexOf(search);
    
    if(pos > 0 && pos + search.length == str.length){
        return true;
    }
    else{
       return false;
    }
    

    编辑:indexOf() 返回字符串的起始索引。

    【讨论】:

    • pos > 0,这样就应该找到字符串,下一个条件看它是否位于末尾。一种更简洁的方法是使用@Avneesh Raghav 发布的正则表达式。
    • contains 返回布尔值,并非所有浏览器都支持;(
    • 已编辑为 indexOf(),感谢您发现这一点。
    • 为什么if (foo) return true; else return false; 为什么不只返回条件的评估(return foo;)?挑剔,但似乎相当多余。
    • 由于OP好像是学生,我觉得会比较容易理解。
    【解决方案4】:
        function isDeprecated(str) {
              return ((str.indexOf("- depreciated") == str.length - "- depreciated".length) ? true : false);
        }
    
        isDeprecated("this")
        false
    
        isDeprecated("this - depreciated")
        true
    
        isDeprecated("this - depreciated abc")
        false
    

    【讨论】:

      【解决方案5】:

      用正则表达式怎么样

        var myRe=/depreciated$/;
        var myval = "string value - depreciated";
        if (myRe.exec(myval)) {
          alert ('found');
        }
        else{
          alert('not found');
        }
      

      【讨论】:

        【解决方案6】:
        function isDepreciated(S){
            return (new RegExp(" - depriciated$").test(S));
        }
        

        【讨论】:

          【解决方案7】:
          function isDepreciated(S) {
              var suffix = "- depreciated";
              return S.indexOf(suffix, S.length - suffix.length) !== -1;
          }
          

          【讨论】:

            【解决方案8】:

            您需要结合使用 Javascript 字符串方法 .substr().length 属性。

            function isDepreciated(var id)
            {
                var id = "string value - depreciated";
                var lastdepreciated = id.substr(id.length - 13); // => "- depreciated"
                //return true or false check for true or flase
            }
            

            这将获取从 id.length - 13 开始的字符,并且由于 .substr() 的第二个参数被省略,因此继续到字符串的末尾。

            【讨论】:

            【解决方案9】:

            在你的 JS 中添加以下代码

            function isDepreciated(string){
               return  /(-depreciated)$/.test(string);
            }
            

            【讨论】:

            • 不需要正则表达式。
            • 另外,可能应该避开-,尽管它不是必需的。
            • 是的!可能不是,但我发现这是解决此问题的最短且健壮的代码行。
            • @Jack 为什么没有必要?
            • @meze 我已将此问题标记为更通用问题的重复;您也可以在那里找到解决方案,或查看 Marginean Vlad 的答案。
            猜你喜欢
            • 2021-02-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-30
            • 1970-01-01
            • 1970-01-01
            • 2020-11-26
            • 2011-08-01
            相关资源
            最近更新 更多