cut out part of a string

前端 未结 8 1548
北荒
北荒 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).

提交回复
热议问题