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.

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.

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

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

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

Here is our output

Here is our 2nd example

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

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.

6) charAt()

syntax is charAt(position)

charAt returns the character at the specified position.

Example

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:

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

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:

Perform a case-insensitive search:

Perform a global, case-insensitive search (the word Microsoft will be replaced each time it is found):

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:

Perform a case-insensitive search:

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

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

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

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

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

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

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.