【问题标题】:cut out part of a string剪下部分字符串
【发布时间】:2009-11-10 12:15:02
【问题描述】:

说,我有一个字符串

"hello is it me you're looking for"

我想剪掉这个字符串的一部分并返回新的字符串,比如

s = string.cut(0,3);

s 现在等于:

"lo is it me you're looking for"

编辑:它可能不是从 0 到 3。它可能是从 5 到 7。

s = string.cut(5,7);

会回来

"hellos it me you're looking for"

【问题讨论】:

  • 只是好奇... String.substr() 方法似乎适用于我遇到的大多数情况-但是您似乎需要删除“中间”字符串位-您有示例吗经常发生这种情况的“现实世界”案例?
  • 你为什么不剪掉 0-2 和 5-6 呢?

标签: javascript string split


【解决方案1】:

你快到了。你想要的是:

http://www.w3schools.com/jsref/jsref_substr.asp

所以,在你的例子中:

Var string = "hello is it me you're looking for";
s = string.substr(3);

因为只提供一个开始(第一个参数)从那个索引到字符串的结尾。

更新,怎么样:

function cut(str, cutStart, cutEnd){
  return str.substr(0,cutStart) + str.substr(cutEnd+1);
}

【讨论】:

  • 这个函数有换行问题,我建议使用 substring 而不是 substr,如下所示 return this.substring(0, cutStart)+this.substring(cutEnd);
【解决方案2】:

使用

substring

功能

返回一个字符串的子集 一个索引和另一个,或通过 字符串的结尾。

substring(indexA, [indexB]);

索引A

An integer between 0 and one less than the length of the string. 

索引B (可选)介于 0 和字符串长度之间的整数。

substring 从 indexA 中提取字符,直到但不包括 indexB。特别是:

* If indexA equals indexB, substring returns an empty string.
* If indexB is omitted, substring extracts characters to the end 
  of the string.
* If either argument is less than 0 or is NaN, it is treated as if 
  it were 0.
* If either argument is greater than stringName.length, it is treated as 
  if it were stringName.length.

如果 indexA 大于 indexB,那么 substring 的效果就像两个参数交换了一样;例如,str.substring(1, 0) == str.substring(0, 1)。

【讨论】:

    【解决方案3】:
    s = string.cut(5,7);
    

    我更愿意将它作为一个单独的函数来执行,但如果你真的希望能够直接在原型中的字符串上调用它:

    String.prototype.cut= function(i0, i1) {
        return this.substring(0, i0)+this.substring(i1);
    }
    

    【讨论】:

    • 这是 javascript 吗?子串
    【解决方案4】:

    其他一些更现代的替代品是:

    1. 拆分和加入

      function cutFromString(oldStr, fullStr) {
        return fullStr.split(oldStr).join('');
      }
      cutFromString('there ', 'Hello there world!'); // "Hello world!"
      

      改编自MDN example

    2. String.replace(),它使用正则表达式。这意味着它可以更灵活地区分大小写。

      function cutFromString(oldStrRegex, fullStr) {
        return fullStr.replace(oldStrRegex, '');
      }
      cutFromString(/there /i , 'Hello THERE world!'); // "Hello world!"
      

    【讨论】:

      【解决方案5】:

      string.substring() 是你想要的。

      【讨论】:

        【解决方案6】:

        作为任何寻找类似功能的人的参考,我有一个 String.prototype.bisect 实现,它使用正则表达式/字符串分隔符将字符串拆分为 3 路,并返回字符串的前、定界符匹配和后部分....

        /*
              Splits a string 3-ways along delimiter.
              Delimiter can be a regex or a string.
              Returns an array with [before,delimiter,after]
        */
        String.prototype.bisect = function( delimiter){
          var i,m,l=1;
          if(typeof delimiter == 'string') i = this.indexOf(delimiter);
          if(delimiter.exec){
             m = this.match(delimiter);
             i = m.index;
             l = m[0].length
          }
          if(!i) i = this.length/2;
          var res=[],temp;
          if(temp = this.substring(0,i)) res.push(temp);
          if(temp = this.substr(i,l)) res.push(temp);
          if(temp = this.substring(i+l)) res.push(temp);
          if(res.length == 3) return res;
          return null;
        };
        
        /* though one could achieve similar and more optimal results for above with: */
        
        "my string to split and get the before after splitting on and once".split(/and(.+)/,2) 
        
        // outputs => ["my string to split ", " get the before after splitting on and once"]
        

        如此处所述:https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/split

        如果 separator 是一个包含捕获括号的正则表达式,那么每次 separator 匹配时,捕获括号的结果(包括任何未定义的结果)都会拼接到输出数组中。 但是,并非所有浏览器都支持此功能。

        【讨论】:

          【解决方案7】:

          您需要执行以下操作:

          var s = "I am a string";
          
          var sSubstring = s.substring(2); // sSubstring now equals "am a string".
          

          你有两种选择:

          http://www.quirksmode.org/js/strings.html#substring

          http://www.quirksmode.org/js/strings.html#substr

          【讨论】:

            【解决方案8】:

            尝试以下方法:

            var str="hello is it me you're looking for";
            document.write(str.substring(3)+"<br />");
            

            您可以查看this link

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-07
              • 2018-01-13
              • 2015-07-17
              • 1970-01-01
              • 1970-01-01
              • 2019-04-17
              相关资源
              最近更新 更多