[英]How to Remove last Comma?
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.此代码生成一个逗号分隔的字符串,以向另一个页面的查询字符串提供 id 列表,但字符串末尾有一个额外的逗号。 How can I remove or avoid that extra comma?
我怎样才能删除或避免那个额外的逗号?
<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>
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(', ');
s = s.substring(0, s.length - 1);
You can use the String.prototype.slice
method with a negative endSlice
argument:您可以使用带有负
endSlice
参数的String.prototype.slice
方法:
n = n.slice(0, -1); // last char removed, "abc".slice(0, -1) == "ab"
Or you can use the $.map
method to build your comma separated string:或者您可以使用
$.map
方法来构建逗号分隔的字符串:
var s = n.map(function(){
return $(this).val();
}).get().join();
alert(s);
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();
});
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>
使用“普通”javascript:
var truncated = s.substring(0, s.length - 1);
A more primitive way is to change the each
loop into a for
loop更原始的方法是将
each
循环改为for
循环
for(var x = 0; x < n.length; x++ ) {
if(x < n.length - 1)
s += $(n[x]).val() + ",";
else
s += $(n[x]).val();
}
Sam's answer is the best so far, but I think map would be a better choice than each in this case.到目前为止,山姆的答案是最好的,但我认为在这种情况下,地图将是比每个都更好的选择。 You're transforming a list of elements into a list of their values, and that's exactly the sort of thing
map
is 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 slice
suggestion that I skipped over.编辑:哎呀,我错过了 CMS 击败我使用
map
,他只是将它隐藏在我跳过的slice
建议下。
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 辅助方法
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, ''));
s = s.TrimEnd(",".ToCharArray());
Write a javascript function :编写一个 javascript 函数:
var removeLastChar = function(value, char){
var lastChar = value.slice(-1);
if(lastChar == char) {
value = value.slice(0, -1);
}
return value;
}
Use it like this:像这样使用它:
var nums = '1,2,3,4,5,6,';
var result = removeLastChar(nums, ',');
console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.