从字符串中删除最后一个逗号

时间:2022-05-09 22:30:36

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 working fiddle. But it has a bug.

使用JavaScript,如何删除最后一个逗号,但仅当逗号是最后一个字符或逗号后面只有空格时?这是我的代码。我有一个工作小提琴。但它有一个错误。

var str = 'This, is a test.'; 
alert( removeLastComma(str) ); // should remain unchanged

var str = 'This, is a test,'; 
alert( removeLastComma(str) ); // should remove the last comma

var str = 'This is a test,          '; 
alert( removeLastComma(str) ); // should remove the last comma

function removeLastComma(strng){        
    var n=strng.lastIndexOf(",");
    var a=strng.substring(0,n) 
    return a;
}

5 个解决方案

#1


251  

This will remove the last comma and any whitespace after it:

这将删除最后一个逗号和后面的任何空格:

str = str.replace(/,\s*$/, "");

It uses a regular expression:

它使用正则表达式:

  • The / mark the beginning and end of the regular expression

    /标记正则表达式的开头和结尾

  • The , matches the comma

    ,匹配逗号

  • The \s means whitespace characters (space, tab, etc) and the * means 0 or more

    \ s表示空白字符(空格,制表符等),*表示0或更多

  • The $ at the end signifies the end of the string

    最后的$表示字符串的结尾

#2


7  

function removeLastComma(str) {
   return str.replace(/,(\s+)?$/, '');   
}

#3


5  

you can remove last comma from a string by using slice() method, find the below example:

你可以使用slice()方法从字符串中删除最后一个逗号,找到下面的例子:

var strVal = $.trim($('.txtValue').val());
var lastChar = strVal.slice(-1);
if (lastChar == ',') {
    strVal = strVal.slice(0, -1);
}

#4


3  

long shot here

在这里远射

var sentence="I got,. commas, here,";
var pattern=/,/g;
var currentIndex;
while (pattern.test(sentence)==true)  {    
  currentIndex=pattern.lastIndex;
 }
if(currentIndex==sentence.trim().length)
alert(sentence.substring(0,currentIndex-1));
else
 alert(sentence);

#5


1  

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.

问题是你删除字符串中的最后一个逗号,而不是逗号,如果它是字符串中的最后一个东西。所以你应该设置一个if来检查最后一个char是否为','并且如果是,则更改它。

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.

您的代码从字符串中查找最后一个逗号并仅存储“This”,因为,最后一个逗号位于字符串末尾的“This”之后。

#1


251  

This will remove the last comma and any whitespace after it:

这将删除最后一个逗号和后面的任何空格:

str = str.replace(/,\s*$/, "");

It uses a regular expression:

它使用正则表达式:

  • The / mark the beginning and end of the regular expression

    /标记正则表达式的开头和结尾

  • The , matches the comma

    ,匹配逗号

  • The \s means whitespace characters (space, tab, etc) and the * means 0 or more

    \ s表示空白字符(空格,制表符等),*表示0或更多

  • The $ at the end signifies the end of the string

    最后的$表示字符串的结尾

#2


7  

function removeLastComma(str) {
   return str.replace(/,(\s+)?$/, '');   
}

#3


5  

you can remove last comma from a string by using slice() method, find the below example:

你可以使用slice()方法从字符串中删除最后一个逗号,找到下面的例子:

var strVal = $.trim($('.txtValue').val());
var lastChar = strVal.slice(-1);
if (lastChar == ',') {
    strVal = strVal.slice(0, -1);
}

#4


3  

long shot here

在这里远射

var sentence="I got,. commas, here,";
var pattern=/,/g;
var currentIndex;
while (pattern.test(sentence)==true)  {    
  currentIndex=pattern.lastIndex;
 }
if(currentIndex==sentence.trim().length)
alert(sentence.substring(0,currentIndex-1));
else
 alert(sentence);

#5


1  

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.

问题是你删除字符串中的最后一个逗号,而不是逗号,如果它是字符串中的最后一个东西。所以你应该设置一个if来检查最后一个char是否为','并且如果是,则更改它。

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.

您的代码从字符串中查找最后一个逗号并仅存储“This”,因为,最后一个逗号位于字符串末尾的“This”之后。