Sitemap

String Manipulation in JavaScript.

4 min readDec 5, 2022

--

Holly’s Code Corner — Episode One!

A string is a sequence of one or more characters that may consist of letters, numbers, or symbols.

Example: “Hello World”, “Abdhfoefhe”, “ I went to the beach today!”, “245 ”

1. To locate a specific character of a string

Example #1: To get the first letter of a string.

const word = "basic";
const letter = word.charAt(0);
console.log(letter);
/* This would print "b" */

Example #2: To get the first letter of a string.

const word = "basic";
const letter = word[0];
console.log(letter);
/* This would print "b" */

Summary: string.charAt() or string[index]

2. To split a string into an array

Example#1: To split a string into an array.

const word = "I went to the beach today!";
let array = word.split(" ");
console.log(array);
/* This would print [ 'I', 'went', 'to', 'the', 'beach', 'today!' ] */

Example #2: To split a string into an array by a word in the string.

const word = "I went to the beach today!";
let array = word.split("the");
console.log(array);
/* This would print [ 'I went to ', ' beach today!' ] */
/* Notice that the word "the" is not in the array anymore.*/

Summary: string.split(“separator”)

3. To turn an array into a string

Example#1 To turn an array into a string.

const array = ["I", "went", "to", "the", "beach", "today!"];
const word = array.join(" ");
console.log(word);
/* This would print "I went to the beach today!" */
const word2 = array.join("");
console.log(word);
/* This would print "Iwenttothebeachtoday!" */

Summary: string.join(“separator”)

4. To reverse a string

( Unfortunately, .reverse()doesn’t work with strings)

Example#1: Using “split, reverse, join” to reverse a string.

const word = "hello";
const wordArray = word.split("");
/* To turn a string into an array */
/* This would return [ 'h', 'e', 'l', 'l', 'o' ] */
const revWordArray = wordArray.reverse();
/* To reverse an array */
/* This would return [ '0', 'l', 'l', 'e', 'h' ] */
const revWordArrayToStirng = revWordArray.join("");
/* To reverse a string */
/* This would return "olleh" */

Summary: string.split(“”).reverse().join(“”)

Example#2 To reverse each word in a string.

const word = "Have a great day!";
const wordToArr = word.split(" ");
console.log(wordToArr);
/*To turn a string into an array*/
/* This would return [ 'Have', 'a', 'great', 'day!'] */
const newArr = wordToArr.map(el => el.split("").reverse().join(""))
console.log(newArr);
/*To reverse every element in the wordToArr array*/
/* This would return [ 'evaH', 'a', 'taerg', '!yad' ] */
const result = newArr.join(" ")
/*To turn an array into a string*/
/* This would return "evaH a taerg !yad" */

Get Holly Ye’s stories in your inbox

Join Medium for free to get updates from this writer.

Summary: First, split the string into an array.

Then use map method on array.

array.map((word) => word.split(“”).reverse().join(“”)).join(“ ”)

5. To turn a string into a number

Example: To turn a string into a number (Note the string must be a valid number)

const word ="123";
const num = parseInt(word)
console.log(num)
/* This would print 123 */

Summary: parseInt(string)

6. To replace a word or a letter in a string

Example#1

const word = "Hello World"
const newWord = word.replace("Hello", "Hi");
console.log(newWord);
/* This would print "Hi World" */

Example #2 (To replace a part of a word in a string)

let word = "kneggow";
const newWord = word.replace("egg", "");
console.log(newWord);
/* This would print "know" */

Summary: string.replace(“part you would like to replace” “replacement”)

Example #3 (To replace the first letter of each word in a string)


const words = "Hello World"
const wordToArr = words.split(" ");
console.log(wordToArr);
/* This would print [ 'Hello', 'World' ]*/

const newWord = wordToArr.map((word) => word.charAt[0] = "T" + word.slice(1)));
console.log(newWord);
/* This would print [ 'Tello', 'Torld' ]*/
/* Using .map function to replace the first charcter of each word in the array.*/
const result = newWord.join(" ")
console.log(result);
/* This would print "Tello Torld" ]*/

Summary: First, split the string into an array.

Second, use map method to replace the first character.

Then, use slice method to keep the rest of the characters in the word.

Lastly, use join method to turn an array into a string.

7. To replace certain letters in a string using regex

Example #1: To remove the word “egg” from all the words in a string

const word = "kneggowegg keggnegg keggneggoegg keggneggoeggwegg";
const newWord = word.replace(/egg/g, "")
console.log(newWord);
/* This would print "know kn kno know" */

Example #2: To remove the first “egg” from the string

const newWord2 = word.replace(/egg/, "")
console.log(newWord2);
/* This would print "knowegg keggnegg keggneggoegg keggneggoeggwegg" */
/* Without the "g" after /egg/, the method will only replace the first "egg" it encounters.*/
/* More on Regex in the following section */

A little bit more on RegEx

/*
/(word or character or you would like to find)\w/ g
"w" here stands for word(any word character)
"g" here stands for global.
*/

const word = "kneggowegg, keggnegg ! keggneggoegg! keggneggoeggwegg";

1) word.replace(/egg/, "hi") will replace the first "egg" in the string with "hi"
/* console.log(word.replace(/egg/, "hi")) would print "knhiowegg, keggnegg ! keggneggoegg! keggneggoeggwegg" */

2) word.replace(/egg/g, "hi") will replace all the "egg" in the string with "hi"
/* console.log(word.replace(/egg/g, "hi")) would print "knhiowhi, khinhi ! khinhiohi! khinhiohiwhi" */

3) word.replace(/\w/g, "hi") will replace every letter with "hi"
/* console.log(word.replace(/\w/g, "hi")) would print "hihihihihihihihihihi, hihihihihihihihi ! hihihihihihihihihihihihi! hihihihihihihihihihihihihihihihi" */

4) word.replace(/\w+/g, "hi") will replace every word with "hi"
/* console.log(word.replace(/\w+/g, "hi")) would print "hi, hi ! hi! hi" */

5)word.replace(/[a-g]/g, "hi") will replace all the letters from "a" to "g" with "hi";
/* it is case sensitive */
/* console.log(word.replace(/[a-g]/g, "hi")) will print "knhihihiowhihihi, khihihinhihihi ! khihihinhihihiohihihi! khihihinhihihiohihihiwhihihi" */

6) word.replace(/[A-G]/g, "hi") will replace all the letters from "A" to "G" with "hi";
/* console.log(word.replace(/[A-G]/g, "hi")) will print "kneggowegg, keggnegg ! keggneggoegg! keggneggoeggwegg" */
/* Because there is no capita letter in the string word */

RegEx is a whole topic onto itself, please come back later for the next installment of Holly’s Code Corner!🌻

--

--