SoFunction
Updated on 2025-04-09

Collection of common JS string properties and methods

method

length determines the length of the string

example:

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string = ;
// string = 26;

method

slice() extracts a part of the string and returns the extracted part in the new string.

This method sets two parameters: start index (start position), and end index (end position).

This example clips the string from position 5 to position 14:

var str = "How old are you?";
var res = (5,14);
// res = ld are yo;

method

(Start index, ending index); Returns the intercepted string, string that does not contain the ending index);

substring() is similar to slice(). The difference is that substring() cannot accept negative indexes.

This example clips the string from position 6 to position 13:

var str = "How old are you?";
var res = (6,13);
// res = d are y;

method

Cut string

var str = "How old are you?";
var res = ('o');
// res = H,w ,ld are y,u?;

method

The indexOf() method returns the index (position) in which the specified text appears in the string:

indexOf (the string to be found, the index starting from a certain position); it returns the index value of this string, if not -1

var str = "The full name of the United States is the United States of America.";
var pos = ("United");
// pos = 21;

method

The lastIndexOf() method returns the index of the last occurrence of the specified text in the string:

lastIndexOf (the string to be found); search from back to front, but the index is still from left to right, if it cannot be found, it returns -1

var str = "The full name of the United States is the United States of America.";
var pos = ("United");
// pos = 42;

method

The charAt() method returns the string with the specified subscript (position) in the string:

When the index is exceeded, the result is an empty string

var str = "The full name of the United States is the United States of America.";
var pos = ("10");
// pos = a;

Attachment: Commonly used methods for strings

method describe
charAt(position) Returns the character at the specified position of the string
charCodeAt (position) Returns the Unicode encoded value of the character at the specified position of the string
indexOf (find string [,StartIndex]) Returns the location of the string that appears for the first time
lastlndexOf (find string [,StartIndex] ) Returns the last occurrence of the string to be found in the String object
match (regular expression) Find strings that match regular expressions in a string
replace (regular expression, new string) Replace the string matching the regular expression with a new string and return as a new string
search (regular expression) Search for matching to the regular expression specified by the parameter
split (separator [,len]) Separate strings into string arrays according to the delimiter specified by the parameter
slice (index value i [, index value j]) Extract and return the string between the string index value i and the index value j-1
substring (index value i [, index value j]) Extract and return the string between the string index value i and the index value j-1
toLowerCase() Convert all letters in the string to lowercase and return as a new string
toUpperCase() Convert all letters in the string to uppercase and return as a new string
toString() Returns the original string value of the string object. This is the method for string objects
valueOf() Returns the original string value of the string object. This is the method for string objects

The above are some methods of JS strings.

Summarize

This is the article about JS string attributes and methods highlights. For more related JS string attributes and methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!