String Manipulation
Strings are vital in any programming language. String manipulation techniques help developers easily edit code. In JavaScript, strings are immutable and help to store text that includes characters, numbers, and Unicode. JavaScript includes many built-in functions for creating and manipulating strings in various ways. This blog will illustrate some of the functions that developers use as tools to write more efficient code. I will first illustrate string methods and then conclude with the plus operator.
String Methods:
.length() : returns the length of a string
ex. let name = "Darryl";
let length = name.length => //6
.slice() : extracts a part of a string and returns it into a new string
ex. let places = "Los Angeles, Chicago, New York";
let city = places.slice(13, 20) => // "Chicago"
.substring() : similar to slice but start and end values less than 0 are treated as 0
ex. let places = "Los Angeles, Chicago, New York";
let city = places.substring(-2, 11) => // "Los Angeles"
.substr() : similar to slice but the second parameter specifies the length of the extracted part
ex. let places = "Los Angeles, Chicago, New York";
let city = places.substr(13, 7) => // "Chicago"
.concat() : joins two or more strings
ex. let text1 = "Be you";
let text2 = "they'll adjust!";
let confidence = text1.concat(" ", text2) => // "Be you they'll adjust!"
.toUpperCase() : converts all values in string to uppercase
ex. let text1 = "Be you";
let text2 = text1.toUpperCase() => // "BE YOU"
.toLowerCase(): converts all values in strings to lowercase
ex. let text1 = "They'll Adjust!";
let text2 = text1.toLowerCase() => // "they'll adjust!"
.trim() : removes white space from both sides of a string
ex. let text1 = " Gator belts ";
let text2 = text1.trim() => // "Gator belts"
.trimStart() : removes white space from the start of a string
ex. let text1 = " patty melts ";
let text2 = text1.trimStart() => // "patty melts "
.trimEnd() : removes white space from the end of a string
ex. ex. let text1 = " and Monte Carlos ";
let text2 = text1.trimEnd() => // " and Monte Carlos"
.padStart()/.padEnd() : pads a string with another string
ex. let text = "10.00 - I'm broke";
let padded = text.padStart(4,"x") => // "xxx10.00 I'm broke"
ex. let text = "Ballin! $100,";
let padded = text.padEnd(4,"0") => // "Ballin! $100,000"
.charAt() : returns the character at a specified index (position) in a string
ex. let text = "Trekkie";
let char = text.charAt(0) => // "T"
.charCodeAt() : returns the unicode of the character at a specified index in a string
ex. let text = "A Milli";
let char = text.charCodeAt(0) => // 65
.replace() : replaces the first match in a string
ex. let text = "Ain't no thang like a chicken thigh";
let newText = text.replace("thigh", "wang") => // "Ain't no thang like a chicken wang"
.replaceAll() : replaces all characters within a string
ex. let text = "I play cards. Cards can be challenging. I'm very competitive when I play cards!"
text = text.replaceAll("Cards","Games");
let text = text.replaceAll("cards","games") => // "I play games. Games can be challenging. I'm very competitive when I play games!"
.split() : Divides a string into an ordered list of two or more substrings and returns it
ex. let text = "Dallas";
let myCity = text.split("") => // [D, a, l, l, a, s]
The + Operator:
the plus operator concatenates values.
ex. let string1 = "Ain't nobody dope as me,";
let string2 = " I'm just so fresh, so clean!"
let lyric = string1 + string2 => // "Ain't nobody dope as me, I'm just so fresh, so clean!"
Top comments (0)