String Methods and Properties
String Length
The length property returns the length of a string:
Example
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
//returns 26
indexOf()
The indexOf() method returns the index of (the position of) the first occurrence
of a specified text in a string: if not found returns -1.
Example
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
//returns 7
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate",15);
//returns 21
lastIndexOf()
The lastIndexOf() method returns the index of the last occurrence of a
specified text in a string: if not found returns -1.
Example:
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
//returns 21
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate",20);
//returns 7
substring(start, end)
substring() extracts a part of a string and returns the extracted part in a new
string. substring() cannot accept negative indexes.
Example
var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13);
//retuns Banana
substr()
substr() is similar to substring(). The difference is that the second parameter
specifies the length of the extracted part.
Example:
var str = "Apple, Banana, Kiwi";
var res = str.substr(7, 6);
//returns Banana
toLowerCase()
Converts a string to lowercase.
toUpperCase()
Converts a string to uppercase.
trim()
The trim() method removes whitespace from both sides of a string.
Example:
var str = myTrim(" Hello World! ");
alert(str);
More Methods
Method Description
charAt() Returns the character at the specified index (position)
endsWith() Checks whether a string ends with specified string/characters
includes() Checks whether a string contains the specified
string/characters
indexOf() Returns the position of the first found occurrence of a
specified value in a string
lastIndexOf() Returns the position of the last found occurrence of a
specified value in a string
repeat() Returns a new string with a specified number of copies of an
existing string. Syntax :string.repeat(count)
replace() Searches a string for a specified value, or a regular
expression, and returns a new string where the specified
values are replaced. Syntax:
string.replace(searchvalue, newvalue)
startsWith() Checks whether a string begins with specified characters
substr() Extracts the characters from a string, beginning at a specified
start position, and through the specified number of character
substring() Extracts the characters from a string, between two specified
indices
toLowerCase() Converts a string to lowercase letters
toUpperCase() Converts a string to uppercase letters
trim() Removes whitespace from both ends of a string
Date Object Methods
Method Description
getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)
getTime() Returns the number of milliseconds since midnight Jan 1
1970, and a specified date
getYear() Returns the year in 2 digit
now() Returns the number of milliseconds since midnight Jan 1,
1970
parse() Parses a date string and returns the number of
milliseconds since January 1, 1970
setDate() Sets the day of the month of a date object
setFullYear() Sets the year of a date object
setHours() Sets the hour of a date object
setMilliseconds() Sets the milliseconds of a date object
setMinutes() Set the minutes of a date object
setMonth() Sets the month of a date object
setSeconds() Sets the seconds of a date object
setTime() Sets a date to a specified number of milliseconds
after/before January 1, 1970
toString() Converts a Date object to a string
toTimeString() Converts the time portion of a Date object to a string