A String is a grouping of characters sorrounded by double quotes such as "this is a string". The String object is used
to work with such entitites.
This tutorial focuses on:
- Instantiating a String object
- Properties of the String object
- Methods of the String object
Instantiating a String object
A String object is instantiated using the new keyword.
var aString = new String("Here is some text");
Alternatively, a String object can be instantiated by sorrounding text in double quotes and assigning it to a variable.
var anotherString = "Here is some more text";
Properties of the String object
length - Returns the numbers of characters in a string.
var aString = "World wide web"
//print the number of characters in aString
Methods of the String object
toLowerCase() - Displays a String in lowercase letters.
var message = "Hello, It Is A Sunny DAY.";
//print the message string in lowercase letters
hello, it is a sunny day.
concat() - Joins two or more strings.
var description2 = "used to provide dynamic and interactive content on webpages.";
//join the description1 and description2 strings
//and print them
substr(startPoint, numToExtract) - Extracts a substring from a string.
//print the first four characters of the message string
The substr() method takes two parameters. The first parameter is a numeric value indicating where in the string to begin extracting characters from. The second parameter is also a numeric value indicating how many characters in the string to extract.
italics() - Displays a string in italics.
var text = "This text will be displayed in italics"
//print the "text" string in italics
This text will be displayed in italics