切掉一部分字符串

时间:2009-11-10 12:15:03

标签: javascript string split

说,我有一个字符串

"hello is it me you're looking for"

我想删除部分字符串并返回新字符串,例如

s = string.cut(0,3);

现在等于:

"lo is it me you're looking for"

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

s = string.cut(5,7);

将返回

"hellos it me you're looking for"

8 个答案:

答案 0 :(得分:32)

你快到了。你想要的是:

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

所以,在你的例子中:

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

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

更新,如下:

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

答案 1 :(得分:8)

使用

substring

功能

  

返回之间字符串的子集   一个指数和另一个,或通过   字符串的结尾。

substring(indexA, [indexB]);

indexA

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

<强> indexB     (可选)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)。

答案 2 :(得分:4)

s = string.cut(5,7);

我更喜欢将它作为一个单独的函数来实现,但是如果你真的希望能够直接在原型的String上调用它:

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

答案 3 :(得分:3)

string.substring()就是你想要的。

答案 4 :(得分:3)

其他一些更现代的替代方案是:

  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 :(得分:1)

作为任何寻找类似函数的人的参考,我有一个String.prototype.bisect实现,它使用正则表达式/字符串分隔符分割字符串3-ways并返回字符串的前面,分隔符匹配和后面部分....

/*
      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是包含捕获括号的正则表达式,则每次匹配时,捕获括号的结果(包括任何未定义的结果)都会拼接到输出数组中。 但是,并非所有浏览器都支持此功能。

答案 6 :(得分:0)

您需要执行以下操作:

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

答案 7 :(得分:0)

尝试以下方法:

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

您可以查看this link