Replace all spaces in a string with '+'

JavascriptString

Javascript Problem Overview


I have a string that contains multiple spaces. I want to replace these with a plus symbol. I thought I could use

var str = 'a b c';
var replaced = str.replace(' ', '+');

but it only replaces the first occurrence. How can I get it replace all occurrences?

Javascript Solutions


Solution 1 - Javascript

Here's an alternative that doesn't require regex:

var str = 'a b c';
var replaced = str.split(' ').join('+');

Solution 2 - Javascript

You need the /g (global) option, like this:

var replaced = str.replace(/ /g, '+');

You can give it a try here. Unlike most other languages, JavaScript, by default, only replaces the first occurrence.

Solution 3 - Javascript

var str = 'a b c';
var replaced = str.replace(/\s/g, '+');

Solution 4 - Javascript

You can also do it like:

str = str.replace(/\s/g, "+");

Have a look at this fiddle.

Solution 5 - Javascript

Use global search in the string. g flag

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

source: replaceAll function

Solution 6 - Javascript

Use a regular expression with the g modifier:

var replaced = str.replace(/ /g, '+');

From Using Regular Expressions with JavaScript and ActionScript:

> /g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.

Solution 7 - Javascript

You need to look for some replaceAll option

str = str.replace(/ /g, "+");

this is a regular expression way of doing a replaceAll.

function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = temp.indexOf(stringToFind);

    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }

    return temp;
}

String.prototype.ReplaceAll = function (stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);

    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }

    return temp;

};

Solution 8 - Javascript

NON BREAKING SPACE ISSUE

In some browsers

(MSIE "as usually" ;-))

replacing space in string ignores the non-breaking space (the 160 char code).

One should always replace like this:

myString.replace(/[ \u00A0]/, myReplaceString)

Very nice detailed explanation:

http://www.adamkoch.com/2009/07/25/white-space-and-character-160/

Solution 9 - Javascript

Do this recursively:

public String replaceSpace(String s){
    if (s.length() < 2) {
        if(s.equals(" "))
            return "+";
        else
            return s;
    }
    if (s.charAt(0) == ' ')
        return "+" + replaceSpace(s.substring(1));
    else
        return s.substring(0, 1) + replaceSpace(s.substring(1));
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveDevView Question on Stackoverflow
Solution 1 - JavascriptDagg NabbitView Answer on Stackoverflow
Solution 2 - JavascriptNick CraverView Answer on Stackoverflow
Solution 3 - JavascriptepascarelloView Answer on Stackoverflow
Solution 4 - JavascriptJitendra PancholiView Answer on Stackoverflow
Solution 5 - JavascriptAndrewView Answer on Stackoverflow
Solution 6 - JavascriptMark ByersView Answer on Stackoverflow
Solution 7 - Javascriptsushil bharwaniView Answer on Stackoverflow
Solution 8 - JavascriptMotlicek PetrView Answer on Stackoverflow
Solution 9 - JavascriptRickView Answer on Stackoverflow