34

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

5
  • 13
    str=str.split(" ").join(); Commented Aug 10, 2013 at 6:18
  • 1
    @anikt that goes in an answer not a comment. Commented Aug 10, 2013 at 6:19
  • 2
    Do you want to remove new lines as well (if there are any) between characters? How about white spaces at start and end that not between characters ? Commented Aug 10, 2013 at 6:33
  • 1
    @anubhava That's exactly what we need to know, great way to word the question! Commented Aug 10, 2013 at 6:35
  • 5
    @bugwheels94 str=str.split(" ").join(); should be str=str.split(" ").join(''); your version adds comma in between Commented Sep 6, 2019 at 17:40

11 Answers 11

79

This should do the trick:

var str = "PB 10 CV 2662";
str = str.replace(/ +/g, "");
Sign up to request clarification or add additional context in comments.

Comments

13

Try this:

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

OR

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

4 Comments

and then you replace all whitespace with... a whitespace :)
This is faulty answer, 1. It removes new lines also and 2. It puts whitespace back so OP's string will remain "as is".
@anubhava in this question there is no specification like new line should be remain as it is. the user just wanna remove the whitespace from his string my answer is for that only.
True but your substitute string is a space, which is wrong
7
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("")

1 Comment

this only removes the first white space
5

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.

2 Comments

@Gel which browser are you using? It works just fine in Chrome latest. Can you post your code?
max some reason it didnt work a while back. Its working! Deleting that comment. Thanks.
2

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.

3 Comments

This only replaces the first " " occurrence
@anubhava replace expects a string or regex
@Ian Yeah, I know . . . it's late here . . . not thinking straight. Fixed it.
2
var str = "PB 10 CV 2662";
var cleaned = str.replace(/\s+/g, "");

7 Comments

\s targets more than just spaces
This will remove new lines as well.
@anubhava Actually, I realized the OP said I just want to remove all white spaces, so technically this is correct. My first comment was wrong. Although it should use +. Even if the example only has spaces
@anubhava Sorry, I edited my comment and mentioned it should use +. My main point was that the target should be \s, not [ ], based solely on the fact that the OP said "all white spaces", even if the example only uses spaces. I'm betting the OP really only needs to target [ ], but who knows
@nikhitadkslfslg I hope it's okay - I just edited your answer too. Simply calling str.replace() doesn't modify the string in place (strings are immutable)...it returns a new string. So it needs to be assigned to something. And your example used string.replace, but the variable was str, so I just changed that too
|
1
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)

Comments

1
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.

Comments

0

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.

Comments

0

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

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

Example

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.