JavaScript String Object

A String can be defined as a collection of characters like alphabets, numbers, special and punctuation symbols. A JavaScript String Object is an object that allows storing character strings.

  • String values are stored in double quotes (“”) or single quotes (‘’) in JavaScript.
  • A String object that contains no characters is called an empty string.
  • The string can be imagined as an array of characters. The index of first character from left is 0 and index of last character is one less than the length of the string.

JavaScript String Constructor

The JavaScript String constructor is just like a function with the name String. The String constructor expects a string value in the parentheses. The syntax is

var string-obj=new String(string value);
var stu_name= new String(“Stella”);

JavaScript String Property

length

The most commonly used property of a string object is length. It represents the count of characters in the value of a string object. The count includes the spaces in the string value.

string-obj.length

The property length can be accessed by using the dot operator with the string object. You can store the value of length property in a variable, print it in the browser window, send it to an alert box or use it in a loop to access through the string characters.

Prototype

This property is used to extend an object by adding properties and methods to an object.

JavaScript String Methods

String objects are the most commonly used objects in web applications. A JavaScript String object can be manipulated by using the following methods.

charAt(integer)

This method is used to read a character at a given location in a string. One positive integer argument is passed to the method whose allowed value is in the range of 0 and length -1. Output is a character.

string_object. charAt(integer location )

charCodeAt (integer)

This method gives back the Unicode character at a given location in a string. One positive integer argument is passed to the method whose allowed value is in the range of 0 and length -1. Output is a Unicode character.

string_object. charCodeAt(integer location )

concat (string , string …..)

This method does the task of concatenation of multiple strings passed as arguments to the concat method. The output will a single string created by joining the string arguments in the sequence they are passed to concat method. Output is a string.

String_object. concat (string1, string2, …..)

indexOf (character)

This method is used to find the index or location of character in a string. The value returned is that of the first occurrence of the specified character. Output is a positive integer if the character is in the string object else -1.

String_object. indexOf (search character)

lastIndexOf (character)

This method is used to get the last index or location of a character in a string. The value returned is that of the last occurrence of the specified character. Output is a positive integer if the character is in the string object else -1.

String_object.lastIindexOf (search character)

localeCompare (compare string)

To compare the strings with relation to their alphabetic order localeCompare  method is used. It returns negative integer value  if the compare string is alphabetically earlier than the reference string, positive integer if the compare string is alphabetically after the reference string and 0 if they are equivalent.

String_object. localeCompare (string)

replace (Search String, Replace String)

replace method replaces a string with another string  in a given string object. If the search string is not found, the original string will remain unchanged. Replace method returns a String as output.

String_object. replace (string1, string2)

search (Search String)

The search method locates the string passed as argument in the string and returns location of its occurrence. Search method returns a String as output.

String_object. search (string1)

slice (slice_start, slice_end)

The slice method is used to break value  of the string object into a smaller string. The characters returned between the slice_start and  slice_end integer values. The output of slice method is a smaller string from original string.

  • The character at slice_end index will not be included in the sliced string.
  • The two arguments slice_start and slice_end must be valid index values between 0 to length -1.
  • slice_start must always be smaller than slice_end. If slice_start is greater than slice_end, no string will be returned by slice method.
  • If slice_end  is omitted, slice method will return slice from index value start_slice to the last character of the string object.
String_object. slice (index1, index2)

split (separator_character)

The split method accepts a character argument and returns an array of strings made from the original string object. The substrings array will contain all the strings separated by the given character. The output of split method is an array of substrings of the original string.

  • Original string remains intact.
  • If the separator character is an empty string, the array contains all the words stored in a string separated by blank spaces in the original string.
  • The separator character is omitted from the substrings thus formed.
String_object. split (character)

substr (start_index, number_of_character)

The substr method gets some characters from the string object value from start index counting the number_of_characters. The output of slice method is a smaller string.

  • The original string remains unchanged
  • The argument start_index must be a valid index value between 0 to length -1.
  • If number_of_characters argument is omitted  then all the remaining characters after the start_index are returned as substring
String_object. substr (index, count)

substring (start_index, end_index)

The substring method works like the slice method. The characters between the start_index and , end_index are returned. The output of slice method is a smaller string.

  • The character at end_index will not be included in the sliced string.
  • The two arguments start_index and end_index must be valid index values between 0 to length -1.
  • start_index must always be smaller value than end_index. If start_index is greater than end_index, no string will be returned.
  • If slice_end  is omitted, slice method will return slice from the start_slice to the last character of the string object.
String_object. substring (index1, index2)

toLowerCase ()

This method is called without any arguments. The empty parentheses are mandatory. This method converts the string object value into lower case. This method returns the string in lowercase.

String_object. toLowerCase()

toUpperCase ()

This string object method is also called without any arguments. The empty parentheses are mandatory. This method converts the string object value into uppercase. This method returns the string in uppercase.

String_object. toUpperCase()

toString ()

This string object method also does not need arguments when called. The empty parentheses are mandatory. This method returns the value of the string object.

String_object. toString ()

valueOf ()

The valueOf method does not need arguments when called. The empty parentheses are mandatory. This method returns the primitive value of the string object.

String_object. valueOf ()

Example – JavaScript String properties and methods

<TITLE> Welcome to CSVeda.com </TITLE>
<SCRIPT LANGUAGE="javascript">
var strObj= new String("Welcome to CSVeda.com");
str1="Hello";

document.write("<br> The original string is = "+strObj);
document.write("<br> length is = "+strObj.length);

document.write("<br> Character at = "+strObj.charAt(5));
document.write("<br> Character Code at = "+strObj.charCodeAt(5));
document.write("<br> Concat = "+strObj.concat(". Come", "Learn the easy way ", "At your pace"));

document.write("<br> Index of m = "+strObj.indexOf("m"));
document.write("<br> Last Index of  m= "+strObj.lastIndexOf("m"));

document.write("<br> localeCompare = "+str1.localeCompare("Good Bye"));
document.write("<br> localeCompare = "+str1.localeCompare("See You"));
document.write("<br> localeCompare = "+str1.localeCompare("Hello"));

document.write("<br> Replace = "+strObj.replace("com","edu"));
document.write("<br> Replace = "+strObj.replace("edu","eduin"));

document.write("<br> Search = "+strObj.search("com"));
document.write("<br> Search = "+strObj.search("edu"));

document.write("<br> Slice = "+strObj.slice(3));

str_arr= strObj.split(" ");
for( i=0 ;i< str_arr.length; i++)
{
document.write("<br> split "+i+"="+str_arr[i]);
}

str_arr= strObj.split("e");
for( i=0 ;i< str_arr.length; i++)
{
document.write("<br> separator character e split "+i+"="+str_arr[i]);
}

document.write("<br> substr = "+strObj.substr(4,7));//return 7 characters from 4th index

document.write("<br> Substring = "+strObj.substring(4,7));//return characters from 4th to 7th index

document.write("<br> conver to lower case = "+strObj.toLowerCase());

document.write("<br> conver to upper case = "+strObj.toUpperCase());

</SCRIPT>