DEV Community

Cover image for JS Blog - String Manipulation
ddmcdona06
ddmcdona06

Posted on • Updated on

JS Blog - String Manipulation

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
Enter fullscreen mode Exit fullscreen mode

.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"
Enter fullscreen mode Exit fullscreen mode

.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"
Enter fullscreen mode Exit fullscreen mode

.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"
Enter fullscreen mode Exit fullscreen mode

.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!"
Enter fullscreen mode Exit fullscreen mode

.toUpperCase() : converts all values in string to uppercase

ex. let text1 = "Be you";
    let text2 = text1.toUpperCase() => // "BE YOU"
Enter fullscreen mode Exit fullscreen mode

.toLowerCase(): converts all values in strings to lowercase

ex. let text1 = "They'll Adjust!"; 
    let text2 = text1.toLowerCase() => // "they'll adjust!"
Enter fullscreen mode Exit fullscreen mode

.trim() : removes white space from both sides of a string

ex. let text1 = "      Gator belts      ";
    let text2 = text1.trim() => // "Gator belts"
Enter fullscreen mode Exit fullscreen mode

.trimStart() : removes white space from the start of a string

ex. let text1 = "      patty melts      ";
    let text2 = text1.trimStart() => // "patty melts    "
Enter fullscreen mode Exit fullscreen mode

.trimEnd() : removes white space from the end of a string

ex. ex. let text1 = "      and Monte Carlos      ";
    let text2 = text1.trimEnd() => // "    and Monte Carlos"
Enter fullscreen mode Exit fullscreen mode

.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"
Enter fullscreen mode Exit fullscreen mode

.charAt() : returns the character at a specified index (position) in a string

ex. let text = "Trekkie";
    let char = text.charAt(0) => // "T"
Enter fullscreen mode Exit fullscreen mode

.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
Enter fullscreen mode Exit fullscreen mode

.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"
Enter fullscreen mode Exit fullscreen mode

.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!"
Enter fullscreen mode Exit fullscreen mode

.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]
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)