JS was influenced by many languages and was designed to look like Java, but be easier for non-programmers to work with. Although best known for its use in websites (as client-side scripting language), JS is also used to enable scripting access to objects embedded in other applications.
Strings in JS are actually objects with a bunch of properties and functions (also called methods) that can be accessed in the following general way:
var myString = "Hello World!";
//This is how we would access a property
var result1 = myString.property;
//This is how we would access a function
var result2 = myString.function(argument1, argument2);
Actually the String object is used to manipulate a stored piece of text. As we can see, the most important part to accessing string properties and functions is to first create them. In this case, myString is our sample string.
A property is just some basic information about the object. For example, a string object has a length property which stores the number of characters in the string. The string’s functions are useful for finding out more about our string. For example, the string function split lets us take a string and chop it into pieces whenever characters that we supply, appear.
It is important to note that these functions do not actually change the string itself. Rather, they return new strings that we can store for use elsewhere. In our example, we stored the result of our make-believe function into result2.
String Object Properties
Returns a reference to the function that created the object
Returns the number of characters in a string
Allows us to add properties and methods to an object
String Object Methods
Returns the character at the specified position in a string
Returns the Unicode of the character at the specified position in a string
Joins two or more strings
Converts Unicode values to characters
Returns the position of the first found occurrence of a specified value in a string
Returns the position of the last found occurrence of a specified value in a string
Searches for a match between a regular expression and a string, and returns the matches
Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
Searches for a match between a regular expression and a string, and returns the position of the match
Extracts a part of a string and returns a new string
Splits a string into an array of substrings
Extracts a specified number of characters in a string
Extracts the characters in a string between two specified indices
Converts a string to lowercase letters
Converts a string to uppercase letters
Represents the source code of an object
Returns the primitive value of a String object
The following JS code shows the example of the String object.
Example: Illustration of String Object
var str = "Hello Internet Technology!";
document.write(str.length + "<br />");
document.write(str.indexOf("Hello") + "<br />");
document.write(str.indexOf("INTERNET") + "<br />");
document.write(str.indexOf("Internet") + "<br />");
document.write(str.indexOf("Technology") + "<br />");
document.write(str.toUpperCase() + "<br />");
document.write(str.match("Technology") + "<br />");
document.write(str.match("technology") + "<br />");
document.write(str.match("Technoloogy") + "<br />");
document.write(str.match("Technology!") + "<br />");
document.write(str.replace("Hello","Hi") + "<br />");
HELLO INTERNET TECHNOLOGY!
Hi Internet Technology!