String

A String object is used to manipulate a series of characters.

A String object is created with new String() or by assigning a string to a variable.

          var s = new String("Hello world!");
// or just:
var s = "Hello world!";

// Finding the length of a string
var message = "Hello World!";
var x = message.length; // x is 12

// Converting a string to uppercase
var message="Hello world!";
var x=message.toUpperCase(); // x is "HELLO WORLD!"

/* The indexOf()method returns the position (as a number) of the 
first found occurrence of a specified text inside a string */
var str="Hello world, welcome to OpenAir.";
var n=str.indexOf("welcome"); 

        

String 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 a String object.

String Methods

Method

Description

charAt()

Returns the character at the specified index.

charCodeAt()

Returns the Unicode of the character at the specified index.

concat()

Joins two or more strings, and returns a copy of the joined 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 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.

toLowerCase()

Converts a string to lowercase letters.

toUpperCase()

Converts a string to uppercase letters.

trim()

Removes white space from both ends of a string.

valueOf()

Returns the primitive value of a String object.