Below are string methods in Javascript. They produces a new string without altering the original string.
String length
String charAt()
String charCodeAt()
String at()
String [ ]
String slice()
String substring()
String substr()
String toUpperCase()
String toLowerCase()
String concat()
String trim()
String trimStart()
String trimEnd()
String padStart()
String padEnd()
String repeat()
String replace()
String replaceAll()
String split()
Let us explore some methods with an example.
JavaScript String Length:
The length property returns the length of a string.
let text = "Farukh";
console.log('text.length '+text.length);
Output:
text.length 6
JavaScript String charAt():
The charAt() method returns the character at a specified index in a string.
let text = "Farukh";
console.log('Result:'+text.charAt(0));
Output:
Result:F
JavaScript String slice():
slice() extracts a part of a string and returns the extracted part in a new string. This method takes 2 parameters:
start position, and end position.
Note: end position is not included .If a parameter is negative, the position is counted from the end of the string:
let text = "Farukh Haider";
console.log('Result:'+text.slice(3, 7));
Output:
Result:ukh
JavaScript String substring():
substring() is similar to slice().
The difference is that start and end values less than 0 are treated as 0 in substring().
let text = "Farukh Haider";
console.log('Result:'+text.substring(7, 12));
Output:
Result:Haide
JavaScript String toUpperCase(), JavaScript String toLowerCase():
let text = "Farukh Haider";
console.log('Result:'+text.toUpperCase());
console.log('Result2:'+text.toLowerCase());
Output:
Result:FARUKH HAIDER
Result:farukh haider
JavaScript String concat():
concat() joins two or more strings as shown in below example.
let text1 = "Farukh";
let text2 = "Haider";
console.log('Result:'+text1.concat(" ",text2));
Output:
Result:Farukh Haider
No comments:
Post a Comment