What is String and String methods in javascript?
What is String in javascript?
JavaScript string is an object that represents a sequence of characters.In JavaScript, strings are immutable. That means the characters of a string cannot be changed. JavaScript is case-sensitive
basically there are two ways to create a string in javascript.
By String literal
By string object (using new keyword)
By String literal
The string literal is created by using double quotes, single quotes and Backticks. The syntax of creating string using string literal is given below:
<script>
const name = 'Mahira';<br/>
const nickname = "Sana";<br/>
const result = `The names are ${name} and ${nickname}`;<br/>
var author = "Mahtab Habib";<br/>
document.write(author);<br/>
document.write(result);<br/>
</script>
Single quotes and double quotes are practically the same and you can use either of them.
Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression}
Multiline Strings
To use a multiline string, you can either use the + operator or the \ operator.
// using the + operator
var message1 = 'Hello ' + 'Mahira' + 'Mahtab.'
// using the \ operator
var message2 = 'Good \ Morning \ Sana.'
By string object (using new keyword)
<script>
var myname = new String("Sana Mahtab");
document.write(myname);
</script>
JavaScript String Methods
list of JavaScript string methods given below
String length
String slice()
String substring()
String substr()
String replace()
String replaceAll()
String toUpperCase()
String toLowerCase()
String concat()
String trim()
String trimStart()
String trimEnd()
String padStart()
String padEnd()
String charAt()
String charCodeAt()
String split()
String length :-
<p>The length of the string is:</p>
<p id="demo"></p>
<script>
let str = "mahira mahtab";
document.getElementById("demo").innerHTML = str.length;
</script>
const str1 = 'Sana';
console.log(str1.length); // 4
let kk = "I love ittutorial";
var len = kk.length;
JavaScript String() Function
The String() function is used to convert different data types to strings.
const a = 786; // number
const b = true; // boolean
//converting to string
const result1 = String(a);
const result2 = String(b);
console.log(result1); // "786"
console.log(result2); // "true"
concat() Function
concat() method combines two or more strings and returns a new string.
<script>
var x="ittutorial";
var y=".in";
document.writeln(x.concat(y));
var str1 = "Sana";
var str2 = "Mahtab";
var result = str1.concat(str2);
let result2 = str1.concat(" ", str2);
alert(result);
</script>
trim()
The trim() method removes whitespace from both sides of a string:
<p id="demo"></p>
<script>
let text1 = " Hello Mahira! ";
let text2 = text1.trim();
document.getElementById("demo").innerHTML =
"Length text1 = " + text1.length + "<br>Length text2 = " + text2.length;
</script>
split()
A string can be converted to an array using the split() method.
<p id="demo"></p>
<script>
let text = "a,b,c,d,e,f";
const myArray = text.split(",");
document.getElementById("demo").innerHTML = myArray[0];
</script>
valueOf()
The JavaScript string valueOf() method is used to find out the primitive value of String object.
<script>
var str="ittutorial";
document.writeln(str.valueOf());
</script>
Output :- ittutorial
var str=new String("Mahira");
document.writeln(str.valueOf());
substr()
substr() is similar to slice().
<p id="demo"></p>
<script>
let str = "Apple, Banana, Mango";
document.getElementById("demo").innerHTML = str.substr(7,6);
</script>
match()
match() method is used to match the string against a regular expression.
Syntax :- string.match(regexp)
<script>
var str="ittutorial";
document.writeln(str.match("it"));
</script>
indexOf()
The indexOf() method returns the index of (position of) the first occurrence of a string in a string:
<p id="demo"></p>
<script>
let text = "Please Enter your name";
document.getElementById("demo").innerHTML = text.indexOf("Enter");
</script>
output :- 7
lastIndexOf()
search()
matchAll()
includes()
startsWith()
endsWith()
Previous
Next