17
JavaScript String
String represents and manipulates a sequence of characters. A string is an array-like object but not an array.
JavaScript String Methods and Properties
charAt()
To get a specified index of a character.
Syntax:
let text = "Hello JavaScript";
let letter = text.charAt(0);
console.log(letter)
/* Output:
H
*/
charCodeAt()
To get a specified index Unicode of the character
Syntax:
let text = "Hello JavaScript";
let code = text.charCodeAt(0);
console.log(code)
/* Output:
72
*/
concat()
To joins two or more string
Syntax:
let text1 = "Hello";
let text2 = "JavaScript";
let result = text1.concat(text2);
console.log(result)
/* Output:
Hello JavaScript
*/
endsWith()
To Check if a string ends with specified string/characters it returns true or false.
Syntex:
let text = "Hello world";
let result = text.endsWith("Hello");
console.log(result)
/* Output:
false
*/
fromCharCode()
Converts Unicode values to characters.
Syntex:
let text = String.fromCharCode(65);
console.log(text)
/* Output:
A
*/
includes()
To check if a string contains the specified string/characters reuters ture and false.
Syntex:
let text = "Hello JavaScript, welcome to the JavaScript world.";
let result = text.includes("welcome");
console.log(text)
/* Output:
true
*/
indexOf()
Check if first found occurrence of a specified value in a string return if exist 0 if not exist -1.
Syntex:
let text = "Hello JavaScript, welcome to the JavaScript world.";
let result = text.indexOf("JavaScript");
console.log(text)
/* Output:
0
*/
lastIndexOf()
Returns the position of the last found occurrence of a specified value in a string.
Syntax:
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("planet");
console.log(text)
/* Output:
36
*/
localeCompare()
Compare two strings in the current locale.
Syntex:
let text1 = "abc";
let text2 = "def";
let result = text1.localeCompare(text2);
console.log(result)
/* Output:
-1
*/
match()
Searches a string for a match against a regular expression, and returns the matches.
`let text = "Hello JavaScript, welcome to the JavaScript world.";
let result = text.match(/rip/g);
console.log(result);
/* Output:
rip, rip
*/
let text = "Hello JavaScript! ";
repeat()
Returns a new string with a specified number of copies of an existing string.
let result = text.repeat(2);
console.log(result);
/* Output:
Hello JavaScript! Hello JavaScript!
*/`
replace()
Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
Syntax:
let text = "Hello PHP, welcome to the PHP world.";
let result = text.replace("PHP", "JavaScript");
console.log(result)
/* Output:
Hello JavaScript, welcome to the JavaScript world.
*/
search()
Searches a string for a specified value, or regular expression, and returns the position of the match.
Syntax:
let text = "Hello JavaScript, welcome to the JavaScript world."
let position = text.search("world");
console.log(position )
/* Output:
44
*/
slice()
Extracts a part of a string and returns a new string.
Syntax:
let text = "Hello JavaScript";
let result = text.slice(0, 10);
console.log(result )
/* Output:
Hello Java
*/
split()
Splits a string into an array of substrings.
Syntax:
let text = "Hello JavaScript, welcome to the JavaScript world."
const myArray = text.split(" ");
console.log(myArray)
/* Output:
[ "Hello", "JavaScript,", "welcome", "to", "the", "JavaScript", "world." ]
*/
toLowerCase()
Converts a string to Lowercase letters
Syntax:
let text = "Hello World!";
let result = text.toLowerCase();
console.log(result);
/*
Output:
hello world
*/
toUpperCase()
Converts a string to uppercase letters
Syntax:
let text = "Hello World!";
let result = text.toUpperCase();
console.log(result);
/*
Output:
HELLO WORLD
*/
trim()
Removes whitespace from both ends of a string
Syntax:
let text = " Hello World! ";
let result = text.trim();
console.log(result);
/*
Output:
Hello World
*/
17