我将Url字符串拆分为n个数字,现在我希望它以单个警报或更好的模块化方式显示而不用逗号分隔
My String可以携带n个错误,因此可以分割n个数组
它应该显示与OL列表相似,没有任何COMMA SEPARATOR
我的代码在
下面 var qtr="http://google.sd.asp?err=ID%20cannot%20be%20NULL/Zero.%0A%0D%20Id%20is%20not%20numeric%20-%202B.%20%0A%0D%20Company%20name%20for%20the%20id%20-%203%20is%20more%20than%20255%20characters.%20";
var uesp= unescape(qtr);
var splitqtr = uesp.split('?err=')[1].split('.');
alert(splitqtr);
for(i=0;i<splitqtr .length;i++)
{
alert(splitqtr[i]);
}
答案 0 :(得分:1)
如果要将数组值组合为单个字符串而不使用逗号作为分隔符,则可以将.join()
与任何参数一起使用。您可能想要使用空字符串:
> splitqtr.join("")
"ID cannot be NULL/Zero\n\r Id is not numeric - 2B \n\r Company name for the id - 3 is more than 255 characters "
要将它们显示为编号列表,您需要在每个列表之前添加数字;我也修剪了琴弦:
for (var i=1; i<=splitqtr.length; i++)
splitqtr[i] = i". "+splitqtr[i].trim();
然后用换行符加入他们:
> splitqtr.join("\n")
"1. ID cannot be NULL/Zero\n2. Id is not numeric - 2B\n3. Company name for the id - 3 is more than 255 characters\n4. "
答案 1 :(得分:1)
您可以为每个字符串添加数字,然后使用换行符加入它们:
for (i = 0; i < splitqtr.length; i++) {
splitqtr[i] = (i + 1) + ". " + splitqtr[i];
}
alert(splitqtr.join("\n"));