Remove Last Comma from a string

前端 未结 10 1210
轻奢々
轻奢々 2020-11-30 19:52

Using JavaScript, how can I remove the last comma, but only if the comma is the last character or if there is only white space after the comma? This is my code. I got a wor

10条回答
  •  感情败类
    2020-11-30 20:01

    The problem is that you remove the last comma in the string, not the comma if it's the last thing in the string. So you should put an if to check if the last char is ',' and change it if it is.

    EDIT: Is it really that confusing?

    'This, is a random string'

    Your code finds the last comma from the string and stores only 'This, ' because, the last comma is after 'This' not at the end of the string.

提交回复
热议问题