Replacing Values in a String

string replace python
string replace java
python string replace multiple
python replace character in string at index
python replace multiple characters in string
python string replace regex
string.replace c#
python 3 string replace

Forgive me if this question has already been asked; I've searched the site for this question and haven't stumbled upon it yet.

I'm creating a word-guessing game, and I'm having trouble with the last of my functions. The function arguments are a user-inputted character ("character"), a randomly generated word ("word"), and a scrambled version of that word ("scrmbldword"). As an example, the word could be "chilly", and the scrambled version would have a corresponding number of underscores "______". The role of this function is to take the user input, scan the "word" for that letter, and if it finds that letter in the word, to replace an underscore of the "scrmbldword" with the corresponding letter.

For instance, the word would be "chilly", and the user input would be the character "l"; I need the scrmbldword to become "___ll_".

function unscrambledWord(character, scrmbldword, word) {
  for (k = 0; k < word.length; k++) {
    if (character == word[k]) {
      var tempLetter = word[k];
      console.log(tempLetter)
      tempWord = scrmbldword.replace(scrmbldword[k], character);
      console.log(tempWord);
    }
  }
}

Thank you for all the answers, but when I copy and paste them into my code, they are not working. I'm trying to understand the code behind your various answers so I can edit them myself, but for the most part I don't get it. Perhaps it would help you all if I gave more context, so here's my file entirely...

<!DOCTYPE html>
<html lang="en-us">

<head>
  <meta charset="UTF-8">
  <title>Word Guess Game</title>
</head>

<body>

  <div>
    <p id="directions-text">Type any letter to start playing</p>
    <p id="userchoice-text"></p>
    <p id="userguesslist-text"></p>
    <p id="unscrambledword-text"></p>
  </div>

  <script type="text/javascript">
    var userGuesses = [];
    var unknownWord = "";

function wordGenerator() {
  var computerChoices = ["lowly", "start", "chilly", "bun", "bead", "friend", "return", "view", "cloth", "frogs", "celery", "basin", "stand", "special", "broad", "abaft", "plead", "quartz", "mark", "tempt", "shop", "stone", "scorch", "taboo", "hoax", "spiffy", "insure"];
  var cpuWord = computerChoices[Math.floor(Math.random() * computerChoices.length)];
  console.log(cpuWord);
  return cpuWord;
};
var computerWord = wordGenerator();

function scrambledWord(string) {
  var knownWord = ""
  if (string.length == 3) {
    knownWord = "___"
  } else if (string.length == 4) {
    knownWord = "____"
  } else if (string.length == 5) {
    knownWord = "_____"
  } else if (string.length == 6) {
    knownWord = "______"
  } else if (string.length == 7) {
    knownWord = "_______"
  }
  return knownWord;
}
var unknownWord = scrambledWord(computerWord);

var directionsText = document.getElementById("directions-text");
var userChoiceText = document.getElementById("userchoice-text");
var userGuessList = document.getElementById("userguesslist-text");
var unscrambledWordText = document.getElementById("unscrambledword-text");

document.onkeyup = function (event) {

  var userGuess = event.key;

  if ((userGuess === "a") || (userGuess === "b") || (userGuess === "c") || (userGuess === "d") || (userGuess === "e") || (userGuess === "f") || (userGuess === "g") || (userGuess === "h") || (userGuess === "i") || (userGuess === "j") || (userGuess === "k") || (userGuess === "l") || (userGuess === "m") || (userGuess === "n") || (userGuess === "o") || (userGuess === "p") || (userGuess === "q") || (userGuess === "r") || (userGuess === "s") || (userGuess === "t") || (userGuess === "u") || (userGuess === "v") || (userGuess === "w") || (userGuess === "x") || (userGuess === "y") || (userGuess === "z")) {
    userGuesses.push(userGuess);
    directionsText.textContent = "";

    userChoiceText.textContent = "You chose: " + userGuess;
    userGuessList.textContent = "You have guessed: " + userGuesses;
    unscrambledWordText.textContent = "The word is: " + unknownWord;
    wordChecker(userGuess)
  } else {
    alert("You did not enter an alphabetical character.")
  }
};

function wordChecker(input) {
  if (computerWord.includes(input)) {
    alert("You guessed a correct character")
    unscrambledWord(input, unknownWord, computerWord)
  } else {
    alert("You guessed an incorrect character")
  }
}

function unscrambledWord(character, scrmbldword, word) {
  for (k = 0; k < word.length; k++) {
    if (character == word[k]) {
      var tempLetter = word[k];
      console.log(tempLetter)
      tempWord = scrmbldword.replace(scrmbldword[k], character);
      console.log(tempWord);
    }
  }
}

As per my understanding, you can try this

function unscrambledWord(character, word, scrmbldword = "_".repeat(word.length)) {
	return [...scrmbldword].map((d, i) => d == '_' && word[i] == character ? character : d).join('')
}

console.log(unscrambledWord('l', 'chilly'))

JavaScript String replace() Method, JavaScript String replace() Method​​ The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. In the above code, I have declared an extra three variables. Dim MyString As String Dim FindString As String Dim ReplaceString As String. For these variables I have assigned values, instead of supplying the Expression String, Find String, and Replace String we will supply only variable to the Replace function.

I think Nitish Narang is really a nice answer, but if you really want to use your existing function, you can try to define and use a replaceAt function, like this :

function unscrambledWord(character, scrmbldword, word) {
  for (k = 0; k < word.length; k++) {
    if (character == word[k]) {
      var tempLetter = word[k];
      console.log(tempLetter)
      tempWord = scrmbldword.replaceAt(k, character);
      console.log(tempWord);
    }
  }
}

String.prototype.replaceAt=function(index, replacement) {
    return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}

unscrambledWord("l", "______", "chilly")

Python String replace() Method, max − If this optional argument max is given, only the first count occurrences are replaced. Return Value. This method returns a copy of the string with all  Parameter Description; oldvalue: Required. The string to search for: newvalue: Required. The string to replace the old value with: count: Optional. A number specifying how many occurrences of the old value you want to replace.

You can use the replace method with a RegExp that has the global flag, which tells us to replace all instances of that RegExp.

RegExp(`[^${character}]`, "g")

We're making a regex that matches any character except the provided character.

function unscrambledWord(character, word) {
    const notCharacter = RegExp(`[^${character}]`, "g")
    return word.replace(notCharacter, "_")
}

Python String replace(), Return Value from replace(). The replace() method returns a copy of the string where the old substring is replaced with the new substring. The original string is  Output: After replacing: In the following example, all the null values in College column has been replaced with “No college” string. Firstly, the data frame is imported from CSV and then College column is selected and fillna() method is used on it.

Java String replace() method, Since JDK 1.5, a new replace() method is introduced, allowing you to replace a sequence of char values. Internal implementation. public String replace(char  Java String replace() The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence.. Since JDK 1.5, a new replace() method is introduced, allowing you to replace a sequence of char values.

String.prototype.replace(), The replace() method returns a new string with some or all matches of a The match or matches are replaced with newSubstr or the value  Class notes on replacing values and strings. Here is everything that was written on the board for class on June 19. There will definitely be more things you need to complete the notebooks, so ask in Slack!

str_replace - Manual, This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. If you don't need fancy replacing rules  Replace(String, String, Boolean, CultureInfo) Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string, using the provided culture and case sensitivity.