Welcome to another AJAX tutorial, This is our introductory mini series into JavaScript. Today we will be learning about the JavaScript String Objects.

JavaScript is an Object Oriented Programming (OOP) language. A JavaScript object has properties and methods. Properties are values that are tagged to the object. A Method of an object refers to the actions than can be performed by the object.

Example: The String JavaScript object has a length property and toUpperCase() method. The Length property of the string object returns the length of the string, that is in other words the number of characters present in the string. toUpperCase() method converts all the letters of the string to upper case. Similarly the toLowerCase() method converts all the letters of the string to lower case.
Code Block
Default.aspx
<script type="text/javascript">
    var txt = "Hello World!"
    document.write(txt.length);
    document.write("<br />");
    document.write(txt.toUpperCase());
</script>
If you run this the output should be:


There are built-in objects available in JavaScript. It is also possible for a JavaScript programmer to define his own objects and variable types. In this tutorial, you will learn how to make use of some of the  built-in objects available in JavaScript.

Built-in Objects
Some of the built-in objects available in JavaScript are:

• String
• Date
• Math
• Array
• Object

Of the above objects, the most widely used one is the String object. Objects are nothing but special types of data. Each object has Properties and Methods associated with it.

The JavaScript String Object
The String object is used to manipulate a stored piece of text. String objects are created with a new String() method.
Code Block
Default.aspx
Syntax: var txt = new String(string); or var txt = string;
String Object Properties

Property

Description

constructor

Returns the function that created the String object’s prototype

length

Returns the length of a string

prototype

Allows you to add properties and methods to an object


The main use of string object is to store text. The methods used in String Objects are:

• charAt()
• charCodeAt()
• concat()
• indexOf
• lastIndexOf
• substring()
• toUpperCase()
• toLowerCase()
• charAt()
• match()
• split()
• replace()

1) charAt()

The charAt() method returns the character at the specified index in a string.The index of the first character is 0, and the index of the last character in a string called “txt”, is txt.length-1.

Syntax: string.charAt(index)

Index: Required. An integer between 0 and string.length-1

Example
Code Block
Default.aspx
char()
<script type="text/javascript">
    var str = "Hello world!";
    document.write("First character: " + str.charAt(0) + "<br />");
    document.write("Last character: " + str.charAt(str.length - 1));
</script>
And when you run that the results will be 


2) charCodeAt()

The charCodeAt() method returns the Unicode of the character at the specified index of a string.The index of the first character is 0, and the index of the last character in a string called “txt”, is txt.length-1.

Syntax: string.charCodeAt(index)

index:Required. An integer between 0 and string.length-1

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Hello world!";
    document.write("First character: " + str.charCodeAt(0) + "<br />");
    document.write("Last character: " + str.charCodeAt(str.length - 1));
</script>
And here is what our output would be:


3) concat()

The concat() method is used to join two or more strings.This method does not change the existing strings, it only returns a copy of the joined strings.

Syntax: string.concat(string2, string3, …, stringX) string2, string3, …, stringX: Required.

The strings to be joined

Example 1
Code Block
Default.aspx
Hello World
<script type="text/javascript">
    var str1 = "Hello ";
    var str2 = "world!";
    document.write(str1.concat(str2));
</script>
Here is our output



Here is our 2nd example
Code Block
Default.aspx
Hello World
<script type="text/javascript">
    var str1 = "Hello ";
    var str2 = "world!";
    var str3 = " Have a nice day!";
    document.write(str1.concat(str2, str3));
</script>

4) indexOf
If we want to know the position of a character or group of characters in a String Object we use the indexOf method.

Syntax is indexOf(substr, [start])

‘substr’ is a character or group of characters whose position we want to determine. ‘start’ is an optional argument. It tells the position from where to begin the search. The order of the search is from beginning to the end of the string. If there are multiple occurrences of ‘substr’ it will return the position of the first occurrence. If no match is found, then it will return -1;

Example
Code Block
Default.aspx
indexOf
<script type="text/javascript">
    var exf = "This is cool";
    document.write(exf.indexOf("This ") + "<br />")
    document.write("<br />");
    document.write(exf.indexOf("Cool ") + "<br />")
</script>   
Here is our output from our  indexOf(substr, [start])



Since we searched the position of ‘Cool’ which is different from ‘cool’,it returned -1. We have already learned that javascript is case sensitive.

5) lastIndexOf

Syntax is lastIndexOf(substr, [start])

Everything is similar to the indexOf method except that the order of search is from the end to beginning of the string.

Example :- “character”.lastIndexOf(“c”) will give the output 5

Substr

Syntax is substr(start, [length])

Substr returns the characters in a string beginning at the “start” and through the specified number of characters, “length”. “Length” is optional, and if omitted, up to the end of the string is assumed.
Code Block
Default.aspx
<script type="text/javascript">
         var exf="excellent";
         document.write(exf.substring(0,4) + "<br />")
         document.write("<br />");
         document.write(exf.substring(2,4)) + "<br />")
</script> 
6) charAt()

syntax is charAt(position)

charAt returns the character at the specified position.

Example
Code Block
Default.aspx
<script type="text/javascript">
    var txt = "welcome";
    document.write(txt.charAt(1) + "<br />")
    document.write("<br />");
    document.write(txt.charAt(0) + "<br />")
</script>  
Here is the output


7) match()

syntax is string.match(regexp)
The match() method searches for a match between a regular expression and a string, and returns the matches.This method returns an array of matches, or null if no match is found.

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "The rain in SPAIN stays mainly in the plain";
    var exp = /ain/gi;
    document.write(str.match(exp));
</script>
This performs a global case insensitive search for “ain”.

The output of the code above will be: ain,AIN,ain,ain

8) split()
The split() method is used to split a string into an array of substrings, and returns the new array.

The syntax is string.split(separator, limit) Both parameters are optional. The separator is the character to use for splitting. The Limit is the integer parameter which specifies the number of splits.

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Are you free today?";
    document.write(str.split() + "<br />");
    document.write(str.split(" ") + "<br />");
    document.write(str.split("") + "<br />");
    document.write(str.split(" ", 3));
</script>

9) replace()
The replace() method searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring.

The syntax is string.replace(regexp/substr,newstring)

Both parameters are required. regexp/substr is a substring or a regular expression. Newstring is the string to replace the found value in parameter 1.

Examples

Perform a case-sensitive search:
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Visit Microsoft!";
    document.write(str.replace("Microsoft""W3Schools"));
</script>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.


Perform a case-insensitive search:
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Visit Microsoft!";
    document.write(str.replace(/microsoft/i, "W3Schools"));
</script> 

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!


Perform a global, case-insensitive search (the word Microsoft will be replaced each time it is found):
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Welcome to Microsoft! ";
    str = str + "We are proud to announce that Microsoft has ";
    str = str + "one of the largest Web Developers sites in the world.";
    document.write(str.replace(/microsoft/gi, "W3Schools"));
</script> 

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.



10) Search()

The search() method searches for a match between a regular expression and a string. This method returns the position of the match, or -1 if no match is found.

Syntax: string.search(regexp)

Examples
Perform a case-sensitive search:
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Visit W3Schools!";
    document.write(str.search("W3SCHOOLS"));
</script> 

We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.

OUTPUT

Perform a case-insensitive search:
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Visit W3Schools!";
    document.write(str.search(/w3schools/i));
</script> 

I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.

OUTPUT



11) Slice()

The slice() method extracts a part of a string and returns the extracted part in a new string. Otherwise it returns -1.
Syntax: string.slice(begin,end)

begin: Required. The index where to begin the extraction. First character is at index 0 end: Optional. Where to end the extraction. If omitted, slice() selects all characters from the begin position to the end of the string

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Hello happy world!";
    // extract all characters, start at position 0:
    document.write(str.slice(0) + "<br />");
    // extract all characters, start at position 6:
    document.write(str.slice(6) + "<br />");
    // extract from the end of the string, and to position -6:
    document.write(str.slice(-6) + "<br />");
 
    // extract only the first character:
    document.write(str.slice(0, 1) + "<br />");
    // extract the characters from position 6 to position 11:
    document.write(str.slice(6, 11) + "<br />");
</script>

Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!

OUTPUT



12) Split()

The split() method is used to split a string into an array of substrings, and returns the new array. If an empty string (“”) is used as the separator, the string is split between each character.

Syntax: string.split(separator, limit)

separator: Optional. Specifies the character to use for splitting the string. If omitted, the entire string will be returned.
limit: Optional. An integer that specifies the number of splits.

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "How are you doing today?";
    document.write(str.split() + "<br />");
    document.write(str.split(" ") + "<br />");
    document.write(str.split("") + "<br />");
    document.write(str.split(" ", 3));
</script>

Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.

OUTPUT


13) substr()

The substr() method extracts the characters from a string, beginning at “start” and through the specified number of character, and returns the new sub string.

Syntax: string.substr(start,length)

Start: Required. The index where to start the extraction. First character is at index 0. length: Optional. The number of characters to extract. If omitted, it extracts the rest of the string
Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Hello world!";
    document.write(str.substr(3) + "<br />");
    document.write(str.substr(3, 4));
</script>

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

OUTPUT

14) substring()

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string between “from” and “to”, not including “to” itself.

Syntax: string.substring(from, to)

from: Required. The index where to start the extraction. First character is at index 0. to: Optional. The index where to stop the extraction. If omitted, it extracts the rest of the string.

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Hello world!";
    document.write(str.substring(3) + "<br />");
    document.write(str.substring(3, 7));
</script>

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting

OUTPUT


15) toLowerCase() and toUpperCase()

The toLowerCase() method converts a string to lowercase letters. The toUpperCase() method converts a string to uppercase letters.

Syntax: string.toLowerCase() and string.toUpperCase()

Example
Code Block
Default.aspx
<script type="text/javascript">
    var txt = "Hello World!";
    document.write(txt.toLowerCase() + "<br />");
    document.write(txt.toUpperCase());
</script>
OUTPUT


16) valueOf()

The valueOf() method returns the primitive value of a String object. This method is usually called automatically by JavaScript behind the scenes, and not explicitly in code.

Syntax: string.valueOf()

Example
Code Block
Default.aspx
<script type="text/javascript">
    var str = "Hello world!";
    document.write(str.valueOf());
</script>

We chose Server Intellect for its cloud hosting, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

OUTPUT

String HTML Wrapper Methods
The HTML wrapper methods return the string wrapped inside the appropriate HTML tag.

Method

Description

anchor()

Creates an anchor

big()

Displays a string using a big font

blink()

Displays a blinking string

bold()

Displays a string in bold

fixed()

Displays a string using a fixed-pitch font

fontcolor()

Displays a string using a specified color

fontsize()

Displays a string using a specified size

italics()

Displays a string in italic

link()

Displays a string as a hyperlink

small()

Displays a string using a small font

strike()

Displays a string with a strikethrough

sub()

Displays a string as subscript text

sup()

Displays a string as superscript text


And that about completes our tutorial on the basics of JavaScript Objects. 

We are looking forward to you learning AJAX and JavaScript so you may fully utilize these tools to better your WEB 2.0 website.

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

Examples.zip