New String Methods :
1. String.prototype.startsWith(searchString,position)
This method will check if the string starts with the specified character. And returns true if the start character is same as the search string.
Parameters :
searchString : The specified character to be searched from starting of a string.
position : It is the index position, which has to begin it's search for the search string and default value is 0.
It returns true if the start string matches else it returns false.
Example :
var str = "Hi, Good Morning!"
console.log(str.startsWith('Hi'));
output :
true
console.log(str.startsWith('Hi',0)); //true
console.log(str.startsWith('Hi',6)); //false
2. String.prototype.endsWith(searchString,endPosition = endPosition.length) :
This method validates whether the string ends with the character of another string.
Parameters :
searchString : The characters that the string must end and it is case-sensitive.
endPosition : The position is to the match the string and this is optional.
returns true if the character ends with the match string else it returns false.
Example :
var str = 'Hi, Good AfterNoon !!! ';
console.log(str.endsWith('Hi')); //false
console.log(str.endsWith('Hi',2)); // true
3. String.prototype.includes(searchString,position)
This string determines whether the string is a substring of a given string.
Parameters :
searchString : substring to search for
position : position is the index to start search from that specified index and default value is 0.
returns true if the string has the substring else it returns false.
Example :
var str = 'Hi, Good Morning!';
console.log(str.includes('hi')); // false
console.log(str.includes('Hi')); // true
console.log(str.includes('Morning')); // true
console.log(str.includes('Good',4)); // true
4. String.prototype.repeat(count) :
This method repeats the string for the specified number of times.
Syntax :
str.repeat(count);
Parameter :
count : number of times the string need to be repeated.
returns a new string
Example :
var myString = new String("JavaScript");
console.log(myString.repeat(3));
output:
JavaScriptJavaScriptJavaScript
Template Literals :
Template Literals are string literals which can allow the embedded expressions and the template strings use back ticks (` `) instead of single quotes and double quotes.
Template String Example :
var foo = `Hello World!`;
String Interpolation :
The placeholders for string substitution can be used by the following syntax :
${ }
Example :
var name = "blog";
console.log('Hi, ${name}');
output :
Hi, blog
String Methods :
1. charAt() :
This method returns the character from the specified index. The characters in the string are been indexed from left to right. The index will start from 0 and end up to the string length - 1.
Example :
var string = "Ambition";
console.log(string.charAt(3));
output : i
2. charCodeAt() :
This method returns the number for the given index in the form of Unicode value.
These Unicode values range from 0 to 1,114,111. The first 128 Unicode's are of ASCII values and the charCodeAt() returns a value which is less than 65,536.
It returns NaN if the given index i.e, number is not between 0 and length of the string - 1.
Example :
var string = "Ambition";
console.log(string.charCodeAt(3));
output : 105
3. indexOf() :
This method returns the index of the first occurrence of the string object. It starts searching from fromIndex or it can be -1 if the value is not found.
Example :
var string = "Ambition";
console.log(string.indexOf("b"));
output : 2
4. concat() :
concat() method is used to add two or more strings and returns a new string.
Example :
var string1 = "TRY AGAIN FAIL AGAIN ";
var string2 = "FAIL BETTER";
console.log(string1.concat(string2));
output : TRY AGAIN FAIL AGAIN FAIL BETTER
1. String.prototype.startsWith(searchString,position)
This method will check if the string starts with the specified character. And returns true if the start character is same as the search string.
Parameters :
searchString : The specified character to be searched from starting of a string.
position : It is the index position, which has to begin it's search for the search string and default value is 0.
It returns true if the start string matches else it returns false.
Example :
var str = "Hi, Good Morning!"
console.log(str.startsWith('Hi'));
output :
true
console.log(str.startsWith('Hi',0)); //true
console.log(str.startsWith('Hi',6)); //false
2. String.prototype.endsWith(searchString,endPosition = endPosition.length) :
This method validates whether the string ends with the character of another string.
Parameters :
searchString : The characters that the string must end and it is case-sensitive.
endPosition : The position is to the match the string and this is optional.
returns true if the character ends with the match string else it returns false.
Example :
var str = 'Hi, Good AfterNoon !!! ';
console.log(str.endsWith('Hi')); //false
console.log(str.endsWith('Hi',2)); // true
3. String.prototype.includes(searchString,position)
This string determines whether the string is a substring of a given string.
Parameters :
searchString : substring to search for
position : position is the index to start search from that specified index and default value is 0.
returns true if the string has the substring else it returns false.
Example :
var str = 'Hi, Good Morning!';
console.log(str.includes('hi')); // false
console.log(str.includes('Hi')); // true
console.log(str.includes('Morning')); // true
console.log(str.includes('Good',4)); // true
4. String.prototype.repeat(count) :
This method repeats the string for the specified number of times.
Syntax :
str.repeat(count);
Parameter :
count : number of times the string need to be repeated.
returns a new string
Example :
var myString = new String("JavaScript");
console.log(myString.repeat(3));
output:
JavaScriptJavaScriptJavaScript
Template Literals :
Template Literals are string literals which can allow the embedded expressions and the template strings use back ticks (` `) instead of single quotes and double quotes.
Template String Example :
var foo = `Hello World!`;
String Interpolation :
The placeholders for string substitution can be used by the following syntax :
${ }
Example :
var name = "blog";
console.log('Hi, ${name}');
output :
Hi, blog
String Methods :
1. charAt() :
This method returns the character from the specified index. The characters in the string are been indexed from left to right. The index will start from 0 and end up to the string length - 1.
Example :
var string = "Ambition";
console.log(string.charAt(3));
output : i
2. charCodeAt() :
This method returns the number for the given index in the form of Unicode value.
These Unicode values range from 0 to 1,114,111. The first 128 Unicode's are of ASCII values and the charCodeAt() returns a value which is less than 65,536.
It returns NaN if the given index i.e, number is not between 0 and length of the string - 1.
Example :
var string = "Ambition";
console.log(string.charCodeAt(3));
output : 105
3. indexOf() :
This method returns the index of the first occurrence of the string object. It starts searching from fromIndex or it can be -1 if the value is not found.
Example :
var string = "Ambition";
console.log(string.indexOf("b"));
output : 2
4. concat() :
concat() method is used to add two or more strings and returns a new string.
Example :
var string1 = "TRY AGAIN FAIL AGAIN ";
var string2 = "FAIL BETTER";
console.log(string1.concat(string2));
output : TRY AGAIN FAIL AGAIN FAIL BETTER
5. lastIndexOf() :
This method returns the index of the string i.e, the last occurrence of the specified value. It starts searching at fromIndex and -1 if the value is not found.
Example :
var string1 = "TRY AGAIN FAIL AGAIN ";
console.log(string1.lastIndexOf("AGAIN"));
output : 15
6. localeCompare() :
This method returns a number and indicates whether the string comes before or after or the same as the string.
It returns 0 if the string is same, returns 1 if there is no match and also if the parameter value contains in the string, returns -1 if there is no match and if the parameter value doesn't contains in the string.
Example :
var string = "This is a string";
console.log(string.localeCompare("xyz")); //-1
console.log(string.localeCompare("n")); //1
console.log(string.localeCompare("This is a string")); // 0
7. slice() :
It extracts a specific string and returns a new string.
If the result is successful then it returns the string of the specified index else it returns -1.
Example :
var string = "This is a string";
console.log(string.slice(2,7)); // is is
console.log(string.slice(3,-4)); // s is a st
negative value determines that from the end of the string it removes that amount of values from the string.
8. split() :
This method splits a string into a array of strings by separating it into the substrings.
Example :
var string = "This-is-a-string";
console.log(string.split("-"));
output : ["This", "is", "a", "string"]
9. substr() :
This method returns the characters in the string from the beginning of the specified string to the specified number of characters.
The substr() method returns the new substring based on the given parameters.
Example :
var str = "This is a string";
console.log(str.substr(1,2)); //hi
console.log(str.substr(-2,2)); // hg
console.log(str.substr(1)); // his is a string
10. substring() :
This method returns the subset the of a string.
It returns the new sub-string based on the given parameters.
Example :
var str = "This is a string";
console.log(str.substring(1,2)); //h
console.log(str.substring(-2,2)); //Th
console.log(str.substring(1)); // his is a string
11. toLowerCase() :
It converts the string into lowercase.
Example :
var string = "TRY AGAIN FAIL AGAIN FAIL BETTER";
console.log(string.toLowerCase());
output : try again fail again fail better
12. toUpperCase() :
This method converts the string into upper case.
var string = "try again fail again fail better";
console.log(string.toUpperCase());
output : TRY AGAIN FAIL AGAIN FAIL BETTER
13. toString() :
This returns a string with the specified object.
Example :
var string = "This is a string";
var numbers = "1234";
console.log(string.toString());
console.log(numbers.toString());
output :
This is a string
1234
14. valueOf() :
This method returns the string object of the primitive value.
Example :
var str = "This is a string";
console.log(str.valueOf());
output :
This is a string
No comments:
Post a Comment