English 中文(简体)
cut out part of a string
原标题:

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"
最佳回答

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);
}
问题回答

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

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!"
    
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);
}

string.substring() is what you want.

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.

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

Try the following:

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

You can check this link

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;
   }




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签