views:

311

answers:

7

Say, I have a string

"hello is it me you're looking for"

I want to cut part of this string out and return the new string, something like

s = string.cut(0,3);

s would now be equal to:

"lo is it me you're looking for"

EDIT: It may not be from 0 to 3. It could be from 5 to 7.

s = string.cut(5,7);

would return

"hellos it me you're looking for"
+1  A: 

string.substring() is what you want.

Cheeso
+1  A: 

Use

substring

function

Returns a subset of a string between one index and another, or through the end of the string.

substring(indexA, [indexB]);

indexA

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

indexB (optional) An integer between 0 and the length of the string.

substring extracts characters from indexA up to but not including indexB. In particular:

* 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.

If indexA is larger than indexB, then the effect of substring is as if the two arguments were swapped; for example, str.substring(1, 0) == str.substring(0, 1).

rahul
+2  A: 

You're almost there. What you want is:

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

So, in your example:

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

As only providing a start (the first arg) takes from that index to the end of the string.

Update, how about something like:

function cut(str, cutStart, cutEnd){
  return str.substr(0,cutStart) + str.substr(cutEnd+1);
}
Jake
check the edit.
dotty
and the second argument would set how many characters you want to include, so `s = string.substr(3, 5)` would result in "lo is" (five characters)
peirix
However i don't it to return the that. I want it to cut out the section it found and return the rest of the string.
dotty
Oops, see update.
Jake
A: 

You need to do something like the following:

var s = "I am a string";

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

You have two options about how to go about it:

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

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

Kevin
A: 

Try the following:

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

You can check this link

Himadri
+2  A: 
s = string.cut(5,7);

I'd prefer to do it as a separate function, but if you really want to be able to call it directly on a String from the prototype:

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

Just as a reference for anyone looking for similar function, I have a String.prototype.bisect implementation that splits a string 3-ways using a regex/string delimiter and returns the before,delimiter-match and after parts of the string....

/*
      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"]

As stated here: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/split

If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.

Quickredfox