Difference between revisions of "Javascript String Functions"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:javascript == Listing of String functions == === .length === Returns the number of characters in the String. === charAt(index) === Returns the character at the...")
 
(replace(regExp, value))
 
(One intermediate revision by one other user not shown)
Line 29: Line 29:
 
=== replace(regExp, value) ===
 
=== replace(regExp, value) ===
 
Finds a match for the regular expression within the String and replaces the resulting subString with a new value.
 
Finds a match for the regular expression within the String and replaces the resulting subString with a new value.
 +
 +
==== Replace All ====
 +
// method 1: literal notation
 +
str.replace(/hello/g, 'hi');
 +
 +
// method 2: RegExp object
 +
str.replace(new RegExp('hello', 'g'), 'hi');
  
 
=== search(regExp) ===
 
=== search(regExp) ===

Latest revision as of 13:44, 30 May 2018


Listing of String functions

.length

Returns the number of characters in the String.

charAt(index)

Returns the character at the specified index.

charCodeAt(index)

Returns the Unicode of the character at the specified index.

concat(String1, ..., StringN)

Joins Strings and returns a copy of the result.

fromCharCode()

Converts Unicode to characters.

indexOf(value)

Returns the position of the first occurrence of the value in the String. Returns -1 if it does not exist.

lastIndexOf(value)

Returns the position of the last occurrence of the value in the String. Returns -1 if it does not exist.

match(regExp)

Finds a match for the regular expression within the String, returning the matches as an array.

replace(regExp, value)

Finds a match for the regular expression within the String and replaces the resulting subString with a new value.

Replace All

// method 1: literal notation
str.replace(/hello/g, 'hi');

// method 2: RegExp object
str.replace(new RegExp('hello', 'g'), 'hi');

search(regExp)

Finds a match for the regular expression within the String and returns the position of the match.

slice(startPos, endPos)

Extracts the part of the String between the specified start and end positions.

split(token)

Splits the String into an array of subStrings, using the specified token as the delimiter.

substr(startPos, numChars)

Extracts a subString from the String, beginning at the start position through the specified number of characters.

subString(startPos, endPos)

Extracts a subString from the String, between the specified start position and end position.

toLowerCase()

Returns the String in all lowercase characters.

toUpperCase()

Returns the String in all uppercase characters.

trim()

Returns the String without any of the whitespace at either end.

valueOf()

Returns the primitive value of the String.