Replace last character of string using JavaScript

后端 未结 3 1494
天涯浪人
天涯浪人 2021-01-31 01:10

I have a very small query. I tried using concat, charAt, slice and whatnot but I didn\'t get how to do it.

Here is my string:

var str1 = \"Notion,Data,I         


        
3条回答
  •  天涯浪人
    2021-01-31 01:48

    You can remove the last N characters of a string by using .slice(0, -N), and concatenate the new ending with +.

    var str1 = "Notion,Data,Identity,";
    var str2 = str1.slice(0, -1) + '.';
    console.log(str2);
    
    Notion,Data,Identity.
    

    Negative arguments to slice represents offsets from the end of the string, instead of the beginning, so in this case we're asking for the slice of the string from the beginning to one-character-from-the-end.

提交回复
热议问题