Replacing charachter by row number in a string [duplicate] - javascript

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
So to practice javascript I'm making a little game and I ran into a problem that I have not idea how to solve.
I want to replace the character/letter in the word by a array or index number.
For example "letter"
0: l
1: e
2: t
3: t
4: e
5: r
So in this case I want to change 3th character to character "b" making it to be "letber"
var newStr = myStr.replace(/_/t, "b"); This approach is not optional for me, it would ruin the purpose of my game.
I also took a look into .slice and .replace options but i couldn't figure out how to use it the way I explained.

Substring replace character. substring(start,end) is inclusive on start and exclusive on end. So "hello".substring(1,3) will be 'el'.
var x = "letter"
var index = 3
x = x.substr(0, index) + 'x' + x.substr(index + 1);
console.log(x);
output:
letxer

Actually you are trying to change a character from a string. Which is a primitive type. Since they are immutable values you can't directly change them like array items.
You need to create a new string to get that working.
const word = 'letter';
const changedWord = word.substring(0, 3) + 'b' + word.substring(4);
console.log(changedWord)

Related

How come the following code doesn't change the retrieved letters into upper case? [duplicate]

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Replace method doesn't work
(4 answers)
Closed 23 days ago.
The goal of this was to change the retrieved letters into upper case. I know this works if I stored lines 6 and 7 inside a variable then replace the letters inside the variable called string, but I wanted to know why this code doesn't work instead.
JavaScript strings are not mutable, meaning you can't change the value of a character at a certain index. You'll need to make a new string made of these values.
let string = 'lowercasestring'; // Creates a variable and stores a string.
let letterOne = string.indexOf('l'); // Should store as 0.
let letterTwo = string.indexOf('s'); // Should store as 7.
// mutiple lines for readability
string = string[letterOne].toUpperCase() +
string.slice(letterOne + 1, letterTwo) +
string[letterTwo].toUpperCase() +
string.slice(letterTwo + 1);
console.log(string);
Output:
"LowercaSestring"

Convert Form input from upper to lower cases [duplicate]

This question already has answers here:
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Converting php string to Title Case
(3 answers)
Closed 4 months ago.
I would need a Custom JavaScript for a WordPress form, converting upper cases to lower cases. I found some entries here in the community, but not the one I need.
What should be converted ?
For example I get a form entry 'PETER' and I want that the first digit 'P' stays in upper cases and the rest should be converted to lower cases. From 'PETER' to 'Peter'
Who can give a helping hand, please ? Thanks
You can get the sting first char with str[0], the other parts sliced with -1 argument.
const name = "PETER";
function PETERToPeter(str) {
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}
const nameChanged = PETERToPeter(name);
console.log(nameChanged);

What is the easier way to convert single string word without spaces into Title case within less code? [duplicate]

This question already has answers here:
How to capitalize first letter and lowercase the rest of the string
(8 answers)
Closed 2 years ago.
I have single string and I want to convert this to title case.
JS is not providing built in function.
var difficulty = "easy"; // medium, hard
difficulty[0].toUpperCase();
document.write(difficulty) // It is printing in small.
If you don't want to repeat the code multiple times, you can add a method to the String prototype, that would allow you to easily reuse the functionality many times
String.prototype.titleCase = function () {
var sentence = this.toLowerCase().split(" ");
for(var i = 0; i< sentence.length; i++){
sentence[i] = sentence[i][0].toUpperCase() + sentence[i].slice(1);
}
return sentence.join(" ");
}
var difficulty = "easy";
document.write(difficulty.titleCase());
document.write("<br/>")
document.write("medium".titleCase());
document.write("<br/>")
document.write("hard".titleCase());
This will also work on words with spaces, so very easy // would give "Very Easy"

Javascript: Getting last few characters of a string [duplicate]

This question already has answers here:
How can I get last characters of a string
(25 answers)
Closed 5 years ago.
This is a trivial problem if we are storing string in a variable. But out of curiosity, I need to understand how to achieve this without storing string in variable?
// To get first few characters of a string, we can do something like this:
var x = "amangupta".substring(0,7); //amangup
// How to get last 7 characters
var x = "amangupta".substring(this.length - 7, this.length); // does not work, looking for similar approach
var str = "amangupta";
var x = str.substring(str.length - 7, str.length); // will work fine
How to get last 7 characters
Try
"amangupta".slice(-7)
Without an extra variable you need to use the string again
"amangupta".substring("amangupta".length - 7, "amangupta".length);

Difference between * and + in regex javascript [duplicate]

This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 7 years ago.
Both giving the same result so what is the difference between "*" and "+" symbol.
How would I know which one to use.
var str= "oofo fooloo"
var StarSymbol= str.match(/fo*/g);
var PlusSymbol= str.match(/fo+/g)
console.log(StarSymbol) // ["fo", "foo"]
console.log(PlusSymbol) // ["fo", "foo"]
fiddle
You are asking the same question again. So let me explain.
var str= "oofo fooloo"
var StarSymbol= str.match(/fo*/g);
var PlusSymbol= str.match(/fo+/g)
console.log(StarSymbol) // ["fo", "foo"]
console.log(PlusSymbol) // ["fo", "foo"]
Ya, both gives the same result here(for this input) but fo* would match f alone where fo+ would not. * repeats the previous token zero or more times where + repeat the previous token one or more times. So this expects the previous token to be repeated atleast one time.
Example:
> var str= "f"
undefined
> str.match(/fo*/g);
[ 'f' ]
> str.match(/fo+/g);
null
>
o* search's for zero or more o's
o+ search's for one or more o's
go through tutorials
* means between 0 and infinite amount of times, while + means at least one (cannot be zero amount of times)
To be more verbose, * would be like writing {0,}, while + would be like writing {1,}

Categories

Resources