Keep only first n characters in a string?

前端 未结 7 1800
逝去的感伤
逝去的感伤 2020-11-29 16:37

Is there a way in JavaScript to remove the end of a string?

I need to only keep the first 8 characters of a string and remove the rest.

7条回答
  •  误落风尘
    2020-11-29 17:32

    You are looking for JavaScript's String method substring

    e.g.

    'Hiya how are you'.substring(0,8);
    

    Which returns the string starting at the first character and finishing before the 9th character - i.e. 'Hiya how'.

    substring documentation

提交回复
热议问题