/    Sign up×
Community /Pin to ProfileBookmark

Help me replace a string.

Just a quick question.

I want to replace +’s in a string to whitespace, for example:

hello+how+are+you

becomes

hello how are you

I cant do replace(/+/,” “) for obvious reasons.

Thanks for the help.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@jamApr 16.2008 — why not use String.split() ?

e.g.

[CODE]
var myString = "hello+world";
var stringPieces = myString.split("+");
var finalString = "";
for(var a=0; a<stringPieces.lengh; a++){
finalString = stringPieces[a]+" ";
}
//final string is now "hello world"
[/CODE]


something liek that would work?

probably not the best way to do it though
Copy linkTweet thisAlerts:
@TaboauthorApr 16.2008 — why not use String.split() ?

e.g.

[CODE]
var myString = "hello+world";
var stringPieces = myString.split("+");
var finalString = "";
for(var a=0; a<stringPieces.lengh; a++){
finalString = stringPieces[a]+" ";
}
//final string is now "hello world"
[/CODE]


something liek that would work?

probably not the best way to do it though[/QUOTE]

thanks, I was going to do that, but I was wondering if there is a short hand way. In the code im creating it uses a tonne of for and while statements and I want to cut down on them as much as possible.
Copy linkTweet thisAlerts:
@ckeyrouzApr 16.2008 — instead of using the loop issue do this:

var myString = "hello+world";

var stringPieces = myString.split("+");

var result = stringPieces.join(" ");
×

Success!

Help @Tabo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...