In this javascript string methods tutorial. We will explain the most useful and important javascript string methods. Javascript string methods help to work with strings in javascript. We will demonstrate javascript string methods like javascript string replace, javascript string contains, javascript string length, javascript string concatenation, javascript string substring, remove the character from string javascript, javascript string uppercase, javascript string lowercase, string trim, tostring method, a match in the string, split string in javascript, javascript string substr, trim string, array to comma-separated string, etc
In this javascript string methods tutorial. We will explain the most useful and important javascript string methods. Javascript string methods help to work with strings in javascript.
We will demonstrate javascript string methods like javascript string replace, javascript string contains, javascript string length, javascript string concatenation, javascript string substring, remove the character from string javascript, javascript string uppercase, javascript string lowercase, string trim, tostring method, a match in the string, split string in javascript, javascript string substr, trim string, array to comma-separated string, etc
We need to know some of the most important and useful javascript string methods.
It is used to count the number of characters in a string javascript.
var str = "Hello Morioh";
var x = str.length;
Output
// return
12
The javascript toLocaleLowerCase() is used to changed string into lower case.
var str = "Hello Morioh";
var res = str.toLocaleLowerCase();
Output
// return
hello morioh
The javascript toLocaleUpperCase () is used to changed string into upper case.
var str = "Hello Morioh";
var res = str.toLocaleUpperCase();
Output
// return
HELLO MORIOH
The indexof () method returns the first position of a specified value in a string.
var txt = "Connecting with Programmers and Developers all over the World";
var test = txt.indexOf("over");
Output
// return
47
The javascript search () method searches a string for the specified value, and returns the status of the match.
var str = "Hello Morioh";
var res = str.search("Morioh");
Output
//Return
6
The slice () method removes the parts of a string and returns the extracted parts to a new string.
Use the Start and End Ultimate to specify the part of the string that you want to remove.
var str = "JavaScript String Methods";
var res = str.slice(0, 10);
Output
// return
JavaScript
The Javascript substring() method is used to removes the characters from one string, between two specified indices, and returns the new substring.
var str = "JavaScript String Methods";
var res = str.substring(1, 4);
Output
//return
ava
A string substr() method begins on the character in the specified position, and returns the specified number of characters.
var str = "JavaScript String Methods";
var res = str.substr(1, 4);
Output
//return
avaS
The Javascript replace() changes the defined value to an another value:
var str = "JavaScript String Methods";
var res = str.replace("Methods", "Tutorial");
Output
// return
JavaScript String Tutorial
The includes() method is used to determine whether a string contains the characters of a specified string or not. If is exist return true or not return false.
var str = "Hello world, my name is morioh.";
var res = str.includes("name");
Output
// return
True
The concat() method is used for join two or more strings.
var str1 = "Hello ";
var str2 = "morioh!";
var res = str1.concat(str2);
Output
// return
Hello morioh!
The Javascript charAt() is used to take a character to a described index location:
var txt = "Hello Dev";
var res = txt.charAt(0);
Output
// return
H
The charCodeAt()
method returns the Unicode of the character at a specified index in a string:
var str = "TEST";
str.charCodeAt(0);
Output
// return
84
The lastIndexOf()
the method returns the index of the last occurrence of a specified text in a string:
var str = "Your are talented dev";
var pos = str.lastIndexOf("dev");
Output
// return
18
The javascript trim() method removes whitespace from both sides of a given string:
var str = " javascript trim() method removes whitespace ";
alert(str.trim());
Output
// return
javascript trim() method removes whitespace
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
var str = "lopersum lopersum lopersum lopersum.";
var res = str.match(/sum/g);
Output
// return
sum,sum,sum,sum
The javascript split method, which is used to convert string to an array:
var str = "1,2,3,4,5";
var arr = str.split(",");
document.write(arr[0]);
document.write("<br>");
document.write(arr[1]);
Output
// return
1
2
The javascript toString() method returns the value of a String object.
var str = "javaScript World!";
var res = str.toString();
Output
// return
javaScript World!
The javascript valueOf() method, which is used to gets the primitive value of a String object.
var str = "javaScript World!";
var res = str.valueOf();
Output
// return
javaScript World!
JavaScript | String indexOf() Method
#javascript #programming