There is no built-in or default method in JavaScript that can directly change characters in strings, but we can use other string methods (such assubstring()
、split()
andjoin()
) to do this.
In this article, we will create custom functions that replace or change characters in strings wherever we want with the help of the default string methods of different examples.
Change string characters using substring() in JavaScript
substring()
The method is a predefined method in JavaScript, which we use on strings to extract the defined characters of the string with the help of indexes. It searches for the defined index from the string of the full declaration and extracts the beginning to end.
substring()
The method does not change the original string. It returns the new string.
grammar:
let string = "Hello world!"; let result = (1, 5); // result will be "ello"
Now, by usingsubstring()
Method, we will initialize the string to change the required characters at a specific location. We will need to provide the required characters and indexes we want to change.
let string = "jiyik is the b_st website to learn programming" // here we want to change "_" with "e" function changeChar() { let result = setCharOnIndex(string,20,'e'); ("Original string : "+string) ("Updated string : "+result) } function setCharOnIndex(string,index,char) { if(index > -1) return string; return (0,index) + char + (index+1); } changeChar()
Output:
"Original string : jiyik is the b_st website to learn programming"
"Updated string : jiyik is the best website to learn programming"
Sample code explanation:
- We initialized a string containing a misspelled string in the JavaScript source code above.
- We have declared a custom function setCharOnIndex() which takes a string, index, and characters as parameters.
- On the provided index, it will use the default substring() method to split the passed string into two parts.
- We then concatenate the characters passed in the middle and finalize the string.
- We show the updated string to view the results and distinguish how the method works.
- We have declared the changeChar() function that we call the setCharOnIndex() function.
- You can see the output in the console log box.
Change string characters using split() and join() in JavaScript
In JavaScript,split()
is a predefined method that splits declared strings into substring arrays. The original string will not besplit()
Method changes; it returns a new string character array.
join()
The method returns a string from the array. It does not change the original array.
We can use it on stringssplit()
Methods andjoin()
to change characters at any location. We will initialize the string with a misspelled string and testsplit()
andjoin()
Method to change any characters at the desired index or position.
Sample code:
let string = "jiyik is the b_st website to learn programming"; // here we want to change "_" with "e" let array = (''); // converting into an array array[20] = "e"; // added "e" in the place of "_" let result = (''); // created string again ("Original string : "+string) ("Updated string : "+result)
Output:
"Original string : jiyik is the b_st website to learn programming"
"Updated string : jiyik is the best website to learn programming"
Sample code explanation:
- Similarly, we initialized a string containing a misspelled string in the JavaScript source code above.
- We use the split() method to split the string into a substring array.
- We have specified the e character on index 20 to change with _.
- We then use the join() method to generate the string again from the changed final array.
- Finally, we display the updated string to see the results and distinguish how the method works.
- View the output in the console log box.
This is the end of this article about changing string characters in JavaScript. For more related js to change string characters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!