cut out part of a string

前端 未结 8 1553
北荒
北荒 2021-02-18 16:17

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

相关标签:
8条回答
  • 2021-02-18 16:45

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

    0 讨论(0)
  • 2021-02-18 16:48

    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.

    0 讨论(0)
  • 2021-02-18 16:50
    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);
    }
    
    0 讨论(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

    0 讨论(0)
  • 2021-02-18 16:59

    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

    0 讨论(0)
  • 2021-02-18 17:02

    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);
    }
    
    0 讨论(0)
提交回复
热议问题