JavaScript string

Strings are useful for holding data that can be represented in text form . A string literal is zero or more characters enclosed in single or double quotation marks. Variable whose value is a string primitive , formed by assigning a string literal, another string variable, or a string expression. A String object is created by using the new Operator , and has a data type of Object.
string str = "This is a string"; String str = new String("This is a string");

JavaScript String Operations

String length property

JavaScript String length property return the number of characters in a string.

var str = "ABCDE"; var len = str.length; alert(len);

Return: 5

charAt(index)

charAt() is a method that returns the character from the specified index.

var str = "ABCDE"; alert(str.charAt(0)); alert(str.charAt(4))

Return: A and D

localeCompare()

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

Return Value

  1. 0 - If the two strings are equal
  2. 1 - no match, When the referenceStr occurs after compareStr
  3. -1 - no match,When the referenceStr occurs before compareStr
var str1 = "ABCD"; var str2 = "EFGH"; var n = str1.localeCompare(str2); alert(n);

Return: -1

toLowerCase()

Convert string str to lower case.

var str = "ABCD EFGH"; alert(str.toLowerCase());

Return: abcd efgh

toUpperCase()

Convert string str to upper case.

var str = "abcd efgh"; alert(str.toUpperCase());

Return : ABCD EFGH

indexOf(substr)

Returns the position of the first occurrence of a specified value in a string.

var str = "JavaScript String Tutorial"; var n = str.indexOf("String"); alert(n)

Return: 11

split(separator)

Splits a string between string separators.

var str = "JavaScript Split() Test"; var arr = str.split(""); alert(arr);

Return: JavaScript,Split(),Test

trim()

Trim whitespace from beginning and end of string str.

var str = " JavaScript Trim "; alert(str.trim());

Return: JavaScript Trim

startsWith(str)

This method returns true if the string begins with the characters, and false if not.

var str = "JavaScript String StartWith"; alert(str.startsWith('JavaScript'));

Return : true

concat(str)

The concat() method is used to join two or more strings.

var str1 = "JavaScript"; var str2 = " Tutorial"; alert(str1.concat(str2));

Return: JavaScript Tutorial

substr(start, length)

This method returns the characters in a string beginning at the specified location through the specified number of characters.

var str = "JavaScript substring test"; alert(str.substr(11,9));

Return : substring

replace(regexp, str)

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match.
var str = "JavaScript Tutorial"; alert(str.replace('Tutorial','replace method'));

Return: JavaScript replace method

str.search(regexp)

Gets the index of the first occurrence of a substring and returns the position of the match.. This method returns -1 if no match is found. The search value can be string or a regular expression.
var str = "JavaScript Tutorial"; alert(str.search('Tutorial'));

Return : 11

toString (String )

Return the string representation of a String object.

var num = 100; alert(num.toString());

Return : 100