Strings - Javascript
- Basic
- Commands
let variable = "value"
let variable = 'value'
let variable = new String("value")
variable.toString()
=> Type conversion
variable[N]
=> String slicing
variable1 + variable2 + variable3
=> Concatenate strings and prints S1S2S3
${variable} value
=> Template literal, String interpolation
variable.length
=> Returns length of string
variable.charAt(N)
=> Returns character at nth index
variable.charCodeAt(N)
=> Returns character code UTF value
variable.indexOf("subS")
=> Returns index of first occurrence of given sub-string, Returns -1 if subS not present
variable.lastIndexOf("subS")
=> Returns index of last occurrence of given sub-string
variable.substring(N1, N2)
=> Returns substring from n1 to n2-1 index
variable.substring(N1)
=> Returns substring from n1 to end of the string
variable.substr(N1, N2)
=> Like substring but n1 tells the starting index and n2 tells length
variable.slice(N1, N2)
=> N2 not included, Like substring but can take -ve values
variable.slice(N1)
=> N1 to end
variable.replace("value1", "value2")
=> Replace substring1 with substring2
variable.toUpperCase()
=> Return with every letter of string into uppercase
variable.toLowerCase()
=> Return with every letter of string into lowercase
variable.trim()
=> Trims white spaces from start and end
variable.includes("subS")
=> Returns true if S includes subS
variable.split("subS")
=> Splits string based on given subS and makes a array out of it
variable.endsWith("subS")
=> Returns true if S ends with subS
variable1.concat("value")
=> Appends S1 & S2, Can use + for concatenation
for (let var in variable) {}
=> Iterate over string and returns index, variable[var]
gives characters
for (let var of variable) {}
=> Returns elements of array & characters of string in var directly