23

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"
dlamblin
  • 43,965
  • 20
  • 101
  • 140
dotty
  • 40,405
  • 66
  • 150
  • 195
  • Just curious... the String.substr() method seems to work for most scenarios I encounter - however you seem to have a need to remove a "middle" string bit - do you have a sample "real world" case where this occurs regularly? – scunliffe Nov 10 '09 at 13:50
  • Why wouldn't you cut 0-2 and 5-6 instead? – dlamblin Dec 15 '09 at 23:17

9 Answers9

35

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
  • 1,026
  • 6
  • 10
  • 1
    This function has an issue with line breaks, i suggest using substring and not substr like advised below return this.substring(0, cutStart)+this.substring(cutEnd); – Liad Livnat Oct 10 '15 at 11:13
8

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
  • 184,426
  • 49
  • 232
  • 263
5

Some other more modern alternatives are:

  1. Split and join

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

    Adapted from MDN example

  2. String.replace(), which uses regex. This means it can be more flexible with case sensitivity.

    function cutFromString(oldStrRegex, fullStr) {
      return fullStr.replace(oldStrRegex, '');
    }
    cutFromString(/there /i , 'Hello THERE world!'); // "Hello world!"
    
filoxo
  • 8,132
  • 3
  • 33
  • 38
4
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);
}
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
bobince
  • 528,062
  • 107
  • 651
  • 834
3

string.substring() is what you want.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
1

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
  • 1,428
  • 14
  • 20
0

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

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
0

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
  • 8,600
  • 15
  • 47
  • 70
0

this works well

function stringCutter(str,cutCount,caretPos){
    let firstPart = str.substring(0,caretPos-cutCount);
    let secondPart = str.substring(caretPos,str.length);
    return firstPart + secondPart;
   }