1

文字列内のスペースを別の文字に変換したい。

元:
var country = "United States"

スペースを「-」にしたいので:
var country = "Unites-States"

これは私が試したものです:

var country ="United States";
var countryArray = country.split(" ");
var newCountry = function(){
for(i=0;i<countryArray.length;i++){
    if(countryArray[i]===","){
    return countryArray[i]="-";}
}
4

3 に答える 3

2

string.replace関数の使用:

var country = "United States";
//var newCountry = country.replace(' ', '-'); //first space only
var newCountry = country.replace(/\s+/g, '-'); //this uses regexp if there is more than just 1 space / tab character.
于 2013-09-01T04:36:11.217 に答える
1

これを試して

country.replace(/ /g, ",");
于 2013-09-01T04:37:03.097 に答える
1

文字列の置換方法を検討しましたか?

例:

newCountry = country.replace(" ", "-");
于 2013-09-01T04:38:33.827 に答える