Logo

dev-resources.site

for different kinds of informations.

JS Blog - String Manipulation

Published at
4/29/2023
Categories
stringsmanipulation
strings
manipulatestrings
Author
ddmcdona06
Author
10 person written this
ddmcdona06
open
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
strings Article's
30 articles in total
Favicon
trimMiddle() - the missing String trim method
Favicon
Interpolação, Verbatim String Literals, Múltiplas Linhas – Tudo Sobre Strings no C#
Favicon
Leetcode — 3110. Score of a String
Favicon
Tipos Básicos em Kotlin - String Literals
Favicon
Tipos Básicos em Kotlin - Strings
Favicon
Quick Zig and C String Conversion Conundrums
Favicon
Code Smell 261 - DigiCert Underscores
Favicon
JavaScript: Strings, String Methods, and String Properties
Favicon
JavaScript: String Template Literals
Favicon
Top 7 PHP Strings Interview Questions and Answers
Favicon
Strings in java language
Favicon
Strings in java language
Favicon
Unraveling the Power of Strings in Python
Favicon
What to use instead of `@ember/string`
Favicon
JS Challenge: Check if a string is a palindrome
Favicon
5 Interesting Things About Strings in Java
Favicon
Efficient String Splitting in Go: A Solution for Gophers Dealing with API Responses
Favicon
Javascript : 12 méthodes essentielles pour les chaînes de caractères
Favicon
anagram program in java
Favicon
How to convert String to Integer in java
Favicon
JS Blog - String Manipulation
Favicon
Find the Index of the First Occurrence in a String (Naive and KMP Solutions)
Favicon
In search of subsequence
Favicon
383. Ransom Note
Favicon
Split and Join: The Dichotomy of Strings and Arrays
Favicon
How do Bytes live in Solidity and coexist with Strings?
Favicon
"str and String in Rust confuses a lot of people in the beginning"
Favicon
W3Schools Strings Method With Example
Favicon
Javascript Tagalog - Strings
Favicon
Resources(R.string) in viewModel in Android

Featured ones: