user2660267
user2660267

Reputation: 991

Remove white space between the string using javascript

I have string. I just want to remove all white spaces between all characters.Please reply "PB 10 CV 2662" to "PB10CV2662"

Upvotes: 34

Views: 114874

Answers (11)

Aqib Iqbal Ansari
Aqib Iqbal Ansari

Reputation: 11

let str1="Lorem    Ipsum is simply       dummy text of    the printing and typesetting     industry.";
    let str1Arr = str1.split(" ");
    str1Arr = str1Arr.filter(item => {
        if (item !== " ")
            return item;
    })
    str1 = str1Arr.join(" ");
    console.log(str1);

We can remove spaces in between the string or template string by splitting the string into array. After that filter out the text element and skip element having space. And Finally, join the array to string with single spaces.

Upvotes: 1

Govind Wadhwa
Govind Wadhwa

Reputation: 987

should not allowed to add space in the end or in between use replaceAll(' ', '') instead of trim().

onChangeText={value => onChange(value.replaceAll(' ', ''))}

Example

Upvotes: 0

maxshuty
maxshuty

Reputation: 10710

2022 - use replaceAll

Most of these answers were done before the replaceAll method was implemented.

Now you can simply do:

let str = "PB 10 CV 2662";
str = str.replaceAll(' ', '');

Which will replace all instances and is much more readable than the regex to make the .replace a global-replace on a target.

Upvotes: 5

iamabs2001
iamabs2001

Reputation: 131

let text = "space    is    short  but      tab   is  long"
let arr = text.split(" ").filter(x => x !== "")
let finalText = "";

for (let item of arr) {
  finalText += item + " "
}

console.log(finalText)

Upvotes: 1

Sekhar Boyina
Sekhar Boyina

Reputation: 1

value = value.replace(/(\r\n\s|\n|\r|\s)/gm, '');

Upvotes: -1

JackDev
JackDev

Reputation: 5062

var str = "PB 10 CV 2662"; 
str = str.split(" ") //[ 'PB', '10', 'CV', '2662' ]
str = str.join("") // 'PB10CV2662'

OR in one line:
str = str.split(" ").join("")

Upvotes: 7

Answer
Answer

Reputation: 131

Try this:

var s = "PB 10 CV 2662";
s.replace(/\s+/g, '');  

OR

s.replace(/\s/g, '');

Upvotes: 13

Bryan
Bryan

Reputation: 2088

Try:

var sample_str =  "PB 10 CV 2662" 
var new_str = sample_str.split(" ").join("")

Or you could use .replace with the global flag like so:

   var sample_str =  "PB 10 CV 2662" 
   var new_str = sample_str.replace(" ","","g")

Both will result in new_str being equal to "PB10CV2662". Hope this is of use to you.

Upvotes: 0

geekchic
geekchic

Reputation: 2441

var str = "PB 10 CV 2662";
var cleaned = str.replace(/\s+/g, "");

Upvotes: 2

talemyn
talemyn

Reputation: 7960

The easiest way would be to use the replace() method for strings:

var stringVal = "PB 10 CV 2662";
var newStringVal = stringVal.replace(/ /g, "");

That will take the current string value and create a new one where all of the spaces are replaced by empty strings.

Upvotes: 2

jh314
jh314

Reputation: 27812

This should do the trick:

var str = "PB 10 CV 2662";
str = str.replace(/ +/g, "");

Upvotes: 79

Related Questions