Replace last character of string using JavaScript

后端 未结 3 1485
天涯浪人
天涯浪人 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:56

    This isn't elegant but it's reusable.

    term(str, char)

    str: string needing proper termination

    char: character to terminate string with

    var str1 = "Notion,Data,Identity,";
    
    function term(str, char) {
      var xStr = str.substring(0, str.length - 1);
      return xStr + char;
    }
    
    console.log(term(str1,'.'))

提交回复
热议问题