Professional Documents
Culture Documents
CSS Lec-14 Methods of String.3819d99
CSS Lec-14 Methods of String.3819d99
String length:
The length property has the string length.
Please note that str.length is a numeric property, not a function. There is no
need to add parenthesis after it.
Accessing Character:
To get a character at position pos, use square brackets [pos] or call the
method str.charAt(pos). The first character starts from the zero position:
var str = `Hello`; // the first character
alert( str[0] ); // H
alert( str.charAt(0) ); // H
Iterating String :
We can also iterate over characters using for..of:
for (var char of "Hello") {
alert(char); // H,e,l,l,o (char becomes "H", then "e", then "l" etc)
}
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
It looks for the substr in str, starting from the given position pos, and
returns the position where the match was found or -1 if nothing can be
found.
If we’re interested in all occurrences, we can run indexOf in a loop. Every
new call is made with the position after the previous match:
var s = 'I felt happy because I saw the others were happy and because I knew I
should feel happy, but I wasn’t really happy.';
var pos=0;
while(true){
pos=s.indexOf("happy",pos);
if(pos==-1) break;
alert(`found at ${pos}`);
pos++; // continue the search from the next position
}
Output: found at 7
found at 43
found at 82
found at 109
2) search() method
Syntax: str.search(searchvalue);
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
This method is just same as indexOf(), only difference is here search value
can be string or regular expression.
3) includes() method
The more modern method
str.includes(substr, pos)
returns true/false depending on whether str contains substr within.
It’s the right choice if we need to test for the match, but don’t need its
position:
Getting substring
There are 3 methods in JavaScript to get a substring:
substring(), substr() and slice().
1) slice( )
Syntax: str.slice(start [, end])
Returns the part of the string from start to (but not including) end.
Example:
var str = "Info Planet”;
alert( str.slice(0, 3) ); // 'Inf', the substring from 0 to 3 (not including 3)
alert( str.slice(0, 1) ); // 'I', from 0 to 1, but not including 1, so only character at 0
Negative values for start/end are also possible. They mean the position is
counted from the string end:
var str = "stringify";
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
// start at the 4th position from the right, end at the 1st from the
right
alert( str.slice(-4, -1) ); // gif
2) substring( )
Syntax: str.substring(start [, end])
3) substr ( )
Syntax: str.substr(start [, length])
Returns the part of the string from start, with the given length. (Allows
negative start also)
In contrast with the previous methods, this one allows us to specify
the length instead of the ending position:
var str = "stringify";
alert( str.substr(2, 4) ); // ring, from the 2nd position get 4 characters
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
Joining a String:
In JavaScript we have two ways to concatenate (join) strings
1. Using + operator
2. Using concat() function of String object.
Syntax : string.concat(string1, string2,….,stringN);
Syntax: string.split(separator,[limit]);
For example,
var s = "Info Planet, Opp. M. J. College, Below Axis ATM, Jalgaon";
var a = s.split(',');
console.log(a);
limit (optional) parameter limits the number of elements returned in the
array.
var size=’56inch’;
var integer =parseInt(size,10); // 56
Simillary parseFloat()
2) Number()
The Number() method converts a string to a number. If string consist of
only number then you will get expected outcome otherwise it will return
NaN.
var size=’56inch’; var size=’56’;
var integer =Number (size); //NaN var integer =Number(size); //56
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
Syntax: string.charCodeAt([position]);
If the position parameter is not provided, then it will use 0 position by
default. Means returns the code of first character of string.
2) fromCharCode( )
It is a String method that is used to create a string from a sequence of
Unicode Values.
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260