Subscribe via RSS Feed

String Object in JavaScript

April 9, 2014 2 Comments

The String object is used to manipulate characters and wraps Javascript’s string primitive data type with a number of helper methods. Because Javascript automatically converts between string primitives and String objects, we are able to call any of the helper methods of the String object on a string primitive. String objects are created with new String().
Basic syntax for creating a String object is as follows:

var val = new String(string);

The string parameter is series of characters that has been properly encoded.
In javascript, implementing string object can be very useful if we can correctly introduce its properties and methods . To use them correctly we also have to know about them. In the following few tables we are discussing a few of those properties and methods.
In the following table we can see that special characters can be encoded using escape notation. We can try to use this escape notations to manage the output.

escape notation:

Code

Output

\’

single quote

\”

double quote

\\

backslash

\n

new line

\r

carriage return

\t

tab

\b

backspace

\f

backspace

\u….

unicode codepoint

In the following we have a few of the string object properties.

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

In the following we are discussing about some of the methods we use for string object.

String Object Methods:

Method

Description

charAt()

Returns the character at the specified index

concat()

Joins two or more strings, and returns a copy of the joined strings

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 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

substr()

Extracts the characters from a string, beginning at a specified start position, and through the specified number of character

substring()

Extracts the characters from a string, between two specified indices

valueOf()

Returns the primitive value of a String object

trim()

Removes whitespace from both ends of a string

toUpperCase()

Converts a string to uppercase letters

toLowerCase()

Converts a string to lowercase letters

toString()

Returns the value of a String object

In the following table we are discussing about wrapper methods.These are not standard methods, and may not work as expected in all browsers.

String HTML Wrapper Methods:

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

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

sub()

Displays a string as subscript text

sup()

Displays a string as superscript text

In javascript we have a function named “String.fromcharcode Function”. With this function we are able to get a string from a number of Unicode character values.
In the following we are going to use some code snippets to understand the different prospects of string object.

Character access:

In javascript we can access an individual character in a string with more than one method. The first is the charat method:

return 'cat'.charAt(1);

The other way is to treat the string as an array-like object.in an array we can place individual characters in indivdual numeric index.

return 'cat'[1];

The properties involved are neither writable nor configurable.

Comparing strings:

In JavaScript, we can use the less-than and greater-than operators for Comparing strings:

var a = "a";
var b = "b";
if (a < b)    print(a + " is less than " + b); else if (a > b)
  print(a + " is greater than " + b);
else
  print(a + " and " + b + " are equal.");

Difference between string primitives and String objects:

Primitive strings are returned from String calls in a non-constructor context. Javascript automatically converts primitives to String objects.this process uses String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup.javascript will automatically wrap the string primitive and call the method or perform the property lookup.

var prim = "foo";var obj = new String(prim);
console.log(typeof prim); // logs "string"
console.log(typeof obj); // logs"object"

String primitives and String objects also give different results when using eval. Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object.

s1 = "2 + 2";               
s2 = new String("2 + 2");   
console.log(eval(s1));      
console.log(eval(s2)); 

A String object can always be converted to its primitive counterpart with the valueOf method.

console.log(eval(s2.valueOf()));

If you find this article helpful, you can connect us in Google+ and Twitter.

Enter your email address:Delivered by FeedBurner

Filed in: Open Source Technologies • Tags: