Strings are a sequence of zero or more characters written inside quotes used to represent text.
Strings may consist of letters, numbers, symbols, words, or sentences.
Strings are immutable, they cannot be changed.
Each character in a string has an index.
The first character will be index 0 the second character will be index 1 and so on.
There are two ways to access an individual character in a string.
charAt() method
const str1 = "abc"; string
str1.charAt(0); character at index 0 → "a"
str1.charAt(1); character at index 1 → "b"
str1.charAt(2); character at index 2 → "c"
str1.charAt(3); character at index 3 → "" index not found
Alternatively use at() or slice() methods
bracket notation []
const str2 = "abc"; string
str2[0]; character at index 0 → "a"
str2[1]; character at index 1 → "b"
str2[2]; character at index 2 → "c"
str2[3]; character at index 3 → undefined index not found
Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Convert string to title case using ↴
replace() method → returns a new string with the value(s) replaced.
Regular Expression → patterns used to match character combinations in strings.
toUpperCase() method → returns the value of the string converted to upper case.
replace() method searches a string for a value or a regular expression and returns a new string with the value replaced.
If pattern is a string, only the first occurrence will be replaced. The original string is unchanged.
syntax
string.replace(pattern, replacement)
const str3 = "hello world";
str3.replace("hello", "HELLO"); replace "hello" with "HELLO"
returns ↴
"HELLO world"
const str4 = "ABCDEF";
str4.replace("B", ""); replace "B" with "" empty string
returns ↴
"ACDEF" → "B" removed from string
Regular expressions are patterns used to match character combinations in strings.
The regular expression /\b\w/g is used to match the first character of each word.
It matches the position between a word character (like letters and digits) and a non-word character (like spaces or punctuation).
\b denotes a word boundary
\w matches any alphanumeric character (letters, digits, or underscores)
Expression \w is equivalent to [a-zA-Z0-9_]
g global flag that indicates the search should continue through the entire string.
Replace the first character of each word with an underscore character "_"
const str5 = "jack of all trades";
str5.replace(/\b\w/g, "_") returns ↴
"_ack _f _ll _rades" first letter of each word replaced with an underscore
toUpperCase() method returns a new string with all letters converted to upper case. The original string is unchanged.
const str6 = "hELlo wORLd";
str6.toUpperCase(); returns ↴
"HELLO WORLD" → uppercase
Initialize a variable to hold the string to tranform to title case.
const string1 = "jack of all trades"; → user input
Define a function toTitleCase to tranform a string into title case.
function toTitleCase(str) {}
The function takes a string as input str and returns a new string that is transformed into title case. The original string remains unchanged.
return str.replace(/\b\w/g, (firstLetter) => firstLetter.toUpperCase())
/\b\w/g regular expression used to match the first character of each word.
\b denotes a word boundary.
\w matches any alphanumeric character (letters, digits, or underscores).
firstLetter → first letter converted to capital letter.
The string is returned converted to title case.
Call the function with ↴
toTitleCase(string1);
Convert string to title case.
const string1 = "jack of all trades";
function toTitleCase(str) {
return str.replace(/\b\w/g, (firstLetter) => firstLetter.toUpperCase());
}
call function
toTitleCase(string1); returns ↴
"Jack Of All Trades"