24
loading...
This website collects cookies to deliver better user experience
"Hello!"
123456 (number represents position)
"Hello!"
012345 character positions
"Hello!".length - 1
Length is a property of a string.
"Hello!".indexOf("!")
Find the first position of a character searching from left-to-right.
"Hello!".lastIndexOf("!")
Find the last position of a character searching from right-to-left.
"Hello!".length - 1
Find the last character in a string.
5
as the result. You can do the opposite with the charAt()
method which returns the character from a string specified by the position."Hello!".includes("!")
True
"Hello!".startsWidth("!")
False
"Hello!".endsWith("!")
True
"Hello!".toUpperCase()
Result "HELLO!"
"Hello!".toLowerCase()
Result "hello!"
LooksLikeThis
it is called camel case. This is because it has humps and no spaces. You will have to traverse and recognize this type some day. We do this to make text easier to read for humans because who likes to read "a sEnTEnCe liKE ThiS!?"
Actually, this method is also useful for web apps like blogs which take an article title and create a url known as a slug."Mastering String Manipulation"
"domain.com/mastering-string-manipulation/"
"Hello" + "World"
Result "HelloWorld"
"Hello".concat("World")
Result "HelloWorld"
"12" + 12
Result "1212", not 24.
var myString = "Hello"
Var string2 = "World"
`${myString} ${World}`
Result "Hello World"
`" Hello World. ".trim()`
Result "Hello World."
padStart
and padEnd
methods for this. For example, a credit card number is saved in the app but you only want to show the last four digits prefixed with the * symbol."4444".padStart(8, "*")
Result "********4444"
"1234".padStart(4, "0")
Result "00001234"
"Hello-".repeat(3)
Result "Hello-Hello-Hello"
"Hello World".slice(6)
Result "World"
"Hello World".slice(6, 8)
Result "Wo"
"Hello World".slice(-3)
Result "rld"
replace
method compared to the previous methods in this article is that replace accepts strings and regular expressions as search queries. It also accepts a function as a second parameter but we won't go into custom functions at this time. With replace, you don't need to rely on using index positions but you need to be familiar with regular expressions (regexp or regex for short) because it is how you can replace multiple instances of the search query. Note the usage of a regular expression with the forward slashes surround the search term."Very bad word".replace("bad", "good")
Result "Very good word"
"Very bad bad word".replace("bad", "good")
Result "Very good bad word"
"Very bad bad word".replace("bad", "good")
"Very bad bad word".replace(/bad/, "good")
Result "Very good bad word"
"Very bad bad word".replace(/bad/g, "good")
Result "Very good good word"
indexOf()
and replace()
for faster function execution speed and when searching for one instance of a word.^
and dollar sign $
. Their purpose is to restrict the search to the beginning and ending of a string respectively. This is also known as anchoring so they can be called anchors. In this case, it means if "abc" is in the middle of "xyzabczyx", it will be ignored. ^
means the string must start with "abc" and $
means that the string must end with "abc". You can apply one or both.\n Find a newline
\t Find a tab
\r Find a carriage return
/^\$10\.\d\d/
\w Matches any word
\d Matches any digit
\s Matches empty space
\W don't match a word
\D don't match a digit
\S don't match empty space
/term/g Finds multiple instances, not just the first
/term/i Finds uppercase and lowercase characters
/[abc]/ Matches any of the letter a, b, or c.
/[0-7]/ Matches numbers 0-7 anywhere in the string.
/[^0-7]/ Don't match numbers 0-7 anywhere in the string.
/[abc](123)/ Matches a, b, or c, followed by 123
/gr[ae]y/ Matches gray or grey
/(gray|grey)/ Matches gray or grey
/a*/ Matches 0 or more letter a
/a+/ Matches 1 or more letter a
/a?/ Matches 0 or 1 letter a
/a{4}/ Matches exactly 4 consecutive letters a.
/a{2,3}/ Matches between 2-3 letters a.
/<[A-Za-z][A-Za-z0-9]*>/g Matches html tags
(?=
and the negative lookahead using (?!
/\$30(?=USD)/ Matches $30 from "The product costs $30USD"
/\$30(?!USD)/ Matches $30 from "The USD value is $30"
24