JavaScript String Methods JAVASCRIPT
- JavaScript Introduction
- JavaScript Syntax
- JavaScript innerHTML
- JavaScript document.write()
- Javascript - window.alert()
- JavaScript - console.log()
- JavaScript Comments
- JavaScript Variables
- JavaScript Operators
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Conditions
- JavaScript Switch
- JavaScript Loop For
- JavaScript While Loop
- JavaScript Break and Continue
- JavaScript Type Conversion
- JavaScript Errors
- JavaScript Scope
- JavaScript this Keyword
- JavaScript Classes
- JavaScript Debugging
- JavaScript - Changing CSS
- JavaScript JSON
JavaScript String Methods
String Length
The length
property returns the length of a string:
Finding a String in a String
The indexOf()
method returns the index of (the position of) the first
occurrence of a specified text in a string:
Note: JavaScript counts positions from zero.
JavaScript - lastIndexOf()
The lastIndexOf()
method returns the index of the last occurrence of a specified text in a string.
Both indexOf()
, and lastIndexOf()
return -1 if the text is not found.
indexOf(), and lastIndexOf()
Both indexOf()
, and lastIndexOf()
return -1 if the text is not found.
var pos = str.lastIndexOf("Deepak");
Both methods accept a second parameter as the starting position for the search.
var pos = str.indexOf("locate", 15);
The lastIndexOf()
methods searches backwards (from the end to the beginning), meaning: if the second parameter is 15
, the search starts at position 15, and searches to the beginning of the string.
Searching for a String in a String
The search()
method searches a string for a specified value and returns the position of the match.
The two methods, indexOf()
and search()
, are equal?
They accept the same arguments (parameters) and return the same value. The two methods are NOT equal. These are the differences:
- The
search()
method cannot take a second start position argument. - The
indexOf()
method cannot take powerful search values (regular expressions).
Extracting String Parts
There are 3 methods for extracting a part of a string:
slice(start, end)
substring(start, end)
substr(start, length)
The slice() Method
slice()
extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the start position, and the end position (end not included).
If a parameter is negative, the position is counted from the end of the string.
This example slices out a portion of a string from position -12 to position -6.
var res = str.slice(-12, -6);
JavaScript - slice() Method with single parameters
If you omit the second parameter, the method will slice out the rest of the string.
or, counting from the end.
The substring() Method
substring()
is similar to slice()
.
The difference is that substring()
cannot accept negative indexes.
If you omit the second parameter, substring()
will slice out the rest of the string.
The substr() Method
substr()
is similar to slice()
.
The difference is that the second parameter specifies the length of the extracted part.
If you omit the second parameter, substr()
will slice out the rest of the string.
Replacing String Content
The replace()
method replaces a specified value with another value in a string:
Converting to Upper and Lower Case
A string is converted to upper case with toUpperCase().
var text2 = text1.toUpperCase(); // text2 is text1 converted to upper
A string is converted to lower case with toLowerCase().
var text2 = text1.toLowerCase(); // text2 is text1 converted to lower
Example
var text1 = "Hello World!"; // String
var text2 = text1.toUpperCase(); // text2 is text1 converted to uppervar text1 = "Hello World!"; // String
var text2 = text1.toLowerCase(); // text2 is text1 converted to lower
The concat() Method
concat()
joins two or more strings.
String.trim()
The trim()
method removes whitespace from both sides of a string.
Example
var str = " Hello World! ";
alert(str.trim());You can use
replace()
with a regular expression instead:var str = " Hello World! ";
alert(str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''));You can also use the replace solution above to add a trim function to the JavaScript
String.prototype
:
Extracting String Characters
There are 3 methods for extracting string characters:
charAt(position)
charCodeAt(position)
- Property access [ ]
The charAt() Method
The charAt()
method returns the character at a specified index (position) in a string.
The charCodeAt() Method
The charCodeAt()
method returns the unicode of the character at a specified index in a string:
The method returns a UTF-16 code (an integer between 0 and 65535).
Property Access
ECMAScript 5 (2009) allows property access [ ] on strings:
Property access might be a little unpredictable:
- It does not work in Internet Explorer 7 or earlier
- It makes strings look like arrays (but they are not)
- If no character is found, [ ] returns undefined, while charAt() returns an empty string.
- It is read only. str[0] = "A" gives no error (but does not work!)
Example
str[0] = "A"; // Gives no error, but does not work
str[0]; // returns H
Converting a String to an Array
A string can be converted to an array with the split()
method.
txt.split(","); // Split on commas
txt.split(" "); // Split on spaces
txt.split("|"); // Split on pipe
If the separator is omitted, the returned array will contain the whole string in index [0].
If the separator is "", the returned array will be an array of single characters.
txt.split(""); // Split in characters
Example
var txt = "a,b,c,d,e"; // String
txt.split(","); // Split on commas
txt.split(" "); // Split on spaces
txt.split("|"); // Split on pipevar txt = "Hello"; // String
txt.split(""); // Split in characters