12

i have comma separated string like

var test = 1,3,4,5,6,

i want to remove particular character from this string using java script

can anyone suggests me?

Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
Aarif Qureshi
  • 474
  • 1
  • 3
  • 13
  • 3
    What is your expected result? – nhahtdh Nov 09 '12 at 06:54
  • you want to remove the separator or some number in your string? What do you want to output: an array or a string? – Gaël Barbin Nov 09 '12 at 06:55
  • Possible duplicate of [Javascript - remove character from a string](http://stackoverflow.com/questions/9932957/javascript-remove-character-from-a-string) – MT0 Nov 22 '16 at 11:17

7 Answers7

42

JavaScript strings provide you with replace method which takes as a parameter a string of which the first instance is replaced or a RegEx, which if being global, replaces all instances.

Example:

var str = 'aba';
str.replace('a', ''); // results in 'ba'
str.replace(/a/g, ''); // results in 'b'

If you alert str - you will get back the same original string cause strings are immutable. You will need to assign it back to the string :

str = str.replace('a', '');
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
6

Use replace and if you want to remove multiple occurrence of the character use

replace like this

var test = "1,3,4,5,6,";
var newTest = test.replace(/,/g, '-');

here newTest will became "1-3-4-5-6-"

yogi
  • 19,175
  • 13
  • 62
  • 92
4

you can make use of JavaScript replace() Method

var str="Visit Microsoft!";
var n=str.replace("Microsoft","My Blog");
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1
var test = '1,3,4,5,6';​​

//to remove character
document.write(test.replace(/,/g, '')); 

//to remove number
function removeNum(string, val){
   var arr = string.split(',');
   for(var i in arr){
      if(arr[i] == val){
         arr.splice(i, 1);
         i--;
      }
  }            
 return arr.join(',');
}

var str = removeNum(test,3);    
document.write(str); // output 1,4,5,6
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
1

You can also

var test1 = test.split(',');

delete test1[2];

var test2 = test1.toString();

Have fun :)

Community
  • 1
  • 1
Leron
  • 9,546
  • 35
  • 156
  • 257
0

you can split the string by comma into an array and then remove the particular element [character or number or even string] from that array. once the element(s) removed, you can join the elements in the array into a string again

  

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

Saasira
  • 739
  • 8
  • 14
0

You can use this function

function removeComma(inputNumber,char='') {

        return inputNumber.replace(/,/g, char);
    }

Update

   function removeComma(inputNumber) {
        inputNumber = inputNumber.toString();
        return Number(inputNumber.replace(/,/g, ''));
    }
Ali
  • 47
  • 5