JS Strings
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:
Pseudo JavaScript Code
<script>
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);
</script>
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
Property | Description |
constructor | Returns a reference to the function that created the object |
length | Returns the number of characters in a string |
prototype | Allows us to add properties and methods to an object |
String Object Methods
Method | Description |
charAt() | Returns the character at the specified position in a string |
charCodeAt() | Returns the Unicode of the character at the specified position in a string |
concat() | Joins two or more strings |
fromCharCode() | Converts Unicode values to 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 |
match() | Searches for a match between a regular expression and a string, and returns the matches |
replace() | Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring |
search() | Searches for a match between a regular expression and a string, and returns the position of the match |
slice() | Extracts a part of a string and returns a new string |
split() | Splits a string into an array of substrings |
substr() | Extracts a specified number of characters in a string |
substring() | Extracts the characters in a string between two specified indices |
toLowerCase() | Converts a string to lowercase letters |
toUpperCase() | Converts a string to uppercase letters |
toSource() | Represents the source code of an object |
valueOf() | Returns the primitive value of a String object |
The following JS code shows the example of the String object.
Example: Illustration of String Object
<html>
<body>
<script>
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 />");
</script>
</body>
</html>
Output:
26
0
-1
6
15
HELLO INTERNET TECHNOLOGY!
Technology
null
null
Technology!
Hi Internet Technology!