Go to comments

js 去掉字符串第一个与最后一个字符

 删除最后一个逗号


人家写的完善,还判断了一下最后删除的是不是逗号,避免删错

var str = ProductDetail_Id; // 要处理的字符串“1234,3333,2222,555,”

str=(str.substring(str.length-1)==',')?str.substring(0,str.length-1):str;


js删除字符串的最后一个字符三种方法

var basic = "abc,def,ghi,";

// 第一种
basic = basic.substr(0, basic.length - 1);

// 第二种
//basic = basic.substring(0, basic.length - 1);

// 第三种
//basic = basic.substring(0, basic.lastIndexOf(','));


收录一个去除多余逗号的方法

function delfh(str){
  str=str.replace(",,",",");
  if(str.substring(str.length-1,str.length)==","){
    str2=str.substring(0,str.length-1);
    delfh(str2);
  }else{
    str2 = str;
  }
  return str2;
}
 
var s2="1,,,2,,,,3,,,,4,54,454,,,,,,,,,,,,,,,,";
var s="415929,415930,415931,415932,415933,415934,415935,415936,415937,415938,415939,415940,415941,415942,415943,415944,415945,415946,415947,415948,1,2,3";
alert(delfh(s2));


参考学习

https://www.cnblogs.com/stjwy/p/5970387.html

https://www.cnblogs.com/soundcode/p/6737571.html

https://zhidao.baidu.com/question/48371104.html



Leave a comment 0 Comments.

Leave a Reply

换一张