Javascript 如何删除最后一个逗号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2047491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Remove last Comma?
提问by Sanju
This code generates a comma separated string to provide a list of ids to the query string of another page, but there is an extra comma at the end of the string. How can I remove or avoid that extra comma?
此代码生成一个逗号分隔的字符串,以向另一个页面的查询字符串提供 id 列表,但字符串末尾有一个额外的逗号。我怎样才能删除或避免那个额外的逗号?
<script type="text/javascript">
$(document).ready(function() {
$('td.title_listing :checkbox').change(function() {
$('#cbSelectAll').attr('checked', false);
});
});
function CotactSelected() {
var n = $("td.title_listing input:checked");
alert(n.length);
var s = "";
n.each(function() {
s += $(this).val() + ",";
});
window.location = "/D_ContactSeller.aspx?property=" + s;
alert(s);
}
</script>
回答by Sam Doshi
Use Array.join
用 Array.join
var s = "";
n.each(function() {
s += $(this).val() + ",";
});
becomes:
变成:
var a = [];
n.each(function() {
a.push($(this).val());
});
var s = a.join(', ');
回答by Amarghosh
s = s.substring(0, s.length - 1);
回答by CMS
You can use the String.prototype.slicemethod with a negative endSliceargument:
您可以使用String.prototype.slice带有否定endSlice参数的方法:
n = n.slice(0, -1); // last char removed, "abc".slice(0, -1) == "ab"
Or you can use the $.mapmethod to build your comma separated string:
或者您可以使用该$.map方法来构建逗号分隔的字符串:
var s = n.map(function(){
return $(this).val();
}).get().join();
alert(s);
回答by Guffa
Instead of removing it, you can simply skip adding it in the first place:
您可以简单地跳过首先添加它,而不是删除它:
var s = '';
n.each(function() {
s += (s.length > 0 ? ',' : '') + $(this).val();
});
回答by Sender
Using substring
使用 substring
var strNumber = "3623,3635,";
document.write(strNumber.substring(0, strNumber.length - 1));
Using slice
使用 slice
document.write("3623,3635,".slice(0, -1));
Using map
使用 map
var strNumber = "3623,3635,";
var arrData = strNumber.split(',');
document.write($.map(arrData, function(value, i) {
return value != "" ? value : null;
}).join(','));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Use Array.join
用 Array.join
var strNumber = "3623,3635,";
var arrTemp = strNumber.split(',');
var arrData = [];
$.each(arrTemp, function(key, value) {
//document.writeln(value);
if (value != "")
arrData.push(value);
});
document.write(arrData.join(', '));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答by miku
Using 'normal' javascript:
使用“普通”javascript:
var truncated = s.substring(0, s.length - 1);
回答by o.k.w
A more primitive way is to change the eachloop into a forloop
更原始的方法是将each循环改为for循环
for(var x = 0; x < n.length; x++ ) {
if(x < n.length - 1)
s += $(n[x]).val() + ",";
else
s += $(n[x]).val();
}
回答by Joel Mueller
Sam's answer is the best so far, but I think mapwould be a better choice than eachin this case. You're transforming a list of elements into a list of their values, and that's exactly the sort of thing mapis designed for.
到目前为止,山姆的答案是最好的,但我认为在这种情况下,地图将是比每个都更好的选择。您正在将元素列表转换为它们的值列表,而这正是map设计的目的。
var list = $("td.title_listing input:checked")
.map(function() { return $(this).val(); })
.get().join(', ');
Edit: Whoops, I missed that CMS beat me to the use of map, he just hid it under a slicesuggestion that I skipped over.
编辑:哎呀,我错过了 CMS 击败我使用map,他只是在slice我跳过的建议下隐藏了它。
回答by Vikas Kottari
you can use below extension method:
您可以使用以下扩展方法:
String.prototype.trimEnd = function (c) {
c = c ? c : ' ';
var i = this.length - 1;
for (; i >= 0 && this.charAt(i) == c; i--);
return this.substring(0, i + 1);
}
So that you can use it like :
这样你就可以像这样使用它:
var str="hello,";
str.trimEnd(',');
Output: hello.
输出:你好。
for more extension methods, check below link: Javascript helper methods
有关更多扩展方法,请查看以下链接: Javascript 辅助方法
回答by Kaya M
Here is a simple method:
这是一个简单的方法:
var str = '1,2,3,4,5,6,';
strclean = str+'#';
strclean = $.trim(strclean.replace(/,#/g, ''));
strclean = $.trim(str.replace(/#/g, ''));

