如何将字符串中的字母替换为短划线而不是空格

时间:2018-01-18 03:22:02

标签: javascript

我正在创建hangman并使用此代码我用字符串替换字符串中的内容,但是一些字符串是两个单词 - “hello world” - 输出为 - - - - - - - - - - - 11字符而不是十个。如何避免更换空间?

var stateNames = ["alabama", "alaska", "arizona", "california", 
  "colorado", "connecticut", "delaware", "florida", "georgia", 
  "hawaii",  
  "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", 
  "louisiana", "maine", "maryland", "massachusetts", "michigan", 
  "minnesota", "mississippi", "missouri", "montana", "nebraska", 
  "nevada", "new hampshire", "new jersey", "new mexico", "new york", 
  "north carolina", "north dakota", "ohio", "oklahoma", "oregon", 
  "pennsylvania", "rhode island", "south carolina", "south dakota", 
  "tennessee", "texas", "utah", "vermont", "virgina", "washington", 
  "west virgina", "wisconsin", "wyoming"];

function beginGame() {
    var randomPick = stateNames[Math.floor(Math.random() * 
    stateNames.length)];

    var replaceWithDash = [];

    for (i = 0; i < randomPick.length; i++){
    replaceWithDash[i] = "_";
}
console.log(randomPick);
console.log(replaceWithDash);
}

beginGame();

2 个答案:

答案 0 :(得分:0)

要回答您的问题,基本上您需要检查插入数组的值是否为空格。如果您想在空格时执行其他操作,可以添加else语句来执行此操作。在这里,您将迭代该数组的元素,然后使用filter_array进行清理。

  var stateNames = ["alabama", "alaska", "arizona", "california", 
      "colorado", "connecticut", "delaware", "florida", "georgia", 
      "hawaii",  
      "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", 
      "louisiana", "maine", "maryland", "massachusetts", "michigan", 
      "minnesota", "mississippi", "missouri", "montana", "nebraska", 
      "nevada", "new hampshire", "new jersey", "new mexico", "new york", 
      "north carolina", "north dakota", "ohio", "oklahoma", "oregon", 
      "pennsylvania", "rhode island", "south carolina", "south dakota", 
      "tennessee", "texas", "utah", "vermont", "virgina", "washington", 
      "west virgina", "wisconsin", "wyoming"];

    function beginGame() {
        var randomPick = stateNames[Math.floor(Math.random() * 
        stateNames.length)];

        var replaceWithDash = [];

        for (i = 0; i < randomPick.length; i++){
                    if(randomPick.charAt(i-1) != " "){
                       replaceWithDash[i] = "_";
                     }
                 }




    console.log(randomPick);
    console.log(filter_array(replaceWithDash));
}
beginGame();   


 function filter_array(test_array) {
        var index = -1,
            arr_length = test_array ? test_array.length : 0,
            resIndex = -1,
            result = [];

        while (++index < arr_length) {
            var value = test_array[index];

            if (value) {
                result[++resIndex] = value;
            }
        }

        return result;
    }        

答案 1 :(得分:0)

当你在字母上循环时,你会想要检查目标字母是否是空格。如果它不是空格,请用短划线替换它。但是,如果 是一个空格,则应该用空格替换它。

请注意,考虑到您正在使用第二个数组replaceWithDash而不是简单地替换字母,您需要明确声明新数组中的字母应包含空格,而不仅仅是没有空间时运行逻辑。因此,以下需要else(否则您最终会得到undefined个索引):

for (i = 0; i < randomPick.length; i++) {
  if (randomPick[i] !== " ") {
    replaceWithDash[i] = "_";
  }
  else {
    replaceWithDash[i] = " ";
  }
}

这可以在以下内容中看到:

var stateNames = ["alabama", "alaska", "arizona", "california",
  "colorado", "connecticut", "delaware", "florida", "georgia",
  "hawaii",
  "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky",
  "louisiana", "maine", "maryland", "massachusetts", "michigan",
  "minnesota", "mississippi", "missouri", "montana", "nebraska",
  "nevada", "new hampshire", "new jersey", "new mexico", "new york",
  "north carolina", "north dakota", "ohio", "oklahoma", "oregon",
  "pennsylvania", "rhode island", "south carolina", "south dakota",
  "tennessee", "texas", "utah", "vermont", "virgina", "washington",
  "west virgina", "wisconsin", "wyoming"
];

function beginGame() {
  var randomPick = stateNames[Math.floor(Math.random() *
    stateNames.length)];

  var replaceWithDash = [];

  for (i = 0; i < randomPick.length; i++) {
    if (randomPick[i] !== " ") {
      replaceWithDash[i] = "_";
    }
    else {
      replaceWithDash[i] = " ";
    }
  }
  console.log(randomPick);
  console.log(replaceWithDash);
}

beginGame();

希望这有帮助! :)