javascript-metodos-del-tipo-string

String Methods in JavaScript

  • 7 min

The String object provides a variety of methods for working with text. Let’s look at some of the most useful ones.

Transformation Methods

MethodDescription
toUpperCase()Converts all characters to uppercase
toLowerCase()Converts all characters to lowercase
trim()Removes whitespace from both ends

Converts all characters in the string to uppercase.

const str1 = "Hello";
const str2 = "World";
const result = str1.concat(" ", str2);

console.log(result); // "Hello World"
Copied!

Converts all characters in the string to lowercase.

const str = "JavaScript";
const result = str.slice(0, 4);

console.log(result); // "Java"
Copied!

Removes whitespace from both ends of the string.

const str = "JavaScript";
const result = str.substring(4, 10);

console.log(result); // "Script"
Copied!

Manipulation Methods

MethodDescription
concat()Combines two or more strings
slice()Extracts a section of a string
substring()Returns a part of the string between indices
substr()Returns a part of the string with a length
split()Splits the string into an array using a delimiter
replace()Replaces a part of the string

Combines two or more strings and returns a new string.

const str = "JavaScript";
const result = str.substr(4, 6);

console.log(result); // "Script"
Copied!

Extracts a section of a string and returns a new string without modifying the original.

const str = "Hello World";
const result = str.split(" ");

console.log(result); // ["Hello", "World"]
Copied!

Returns a part of the string between the specified indices.

const str = "Hello World";
const result = str.replace("World", "JavaScript");

console.log(result); // "Hello JavaScript"
Copied!

Similar to substring(), but with the second parameter specifying the length of the substring.

const str = "Hello World";
const result = str.toUpperCase();

console.log(result); // "HELLO WORLD"
Copied!

Splits the string into an array of substrings using a specified delimiter.

const str = "Hello World";
const result = str.toLowerCase();

console.log(result); // "hello world"
Copied!

Replaces a part of the string with another string or regular expression.

const str = "   Hello World   ";
const result = str.trim();

console.log(result); // "Hello World"
Copied!

Search Methods

MethodDescription
indexOf()Returns the index of the first occurrence
lastIndexOf()Returns the index of the last occurrence
includes()Determines if a substring is present
startsWith()Determines if it starts with a string
endsWith()Determines if it ends with a string

Returns the index of the first occurrence of a substring, or -1 if not found.

const str = "Hello World";
const index = str.indexOf("World");

console.log(index); // 6
Copied!

Returns the index of the last occurrence of a substring, or -1 if not found.

const str = "Hello World World";
const index = str.lastIndexOf("World");

console.log(index); // 12
Copied!

Determines if a substring is present within a string, returning true or false.

const str = "Hello World";
const result = str.includes("World");

console.log(result); // true
Copied!

Determines if a string starts with the characters of a substring, returning true or false.

const str = "Hello World";
const result = str.startsWith("Hello");

console.log(result); // true
Copied!

Determines if a string ends with the characters of a substring, returning true or false.

const str = "Hello World";
const result = str.endsWith("World");

console.log(result); // true
Copied!

Comparison Methods

MethodDescription
localeCompare()Compares two strings in a specific locale
normalize()Returns the Unicode normalized form of the string

Compares two strings in a specific locale and returns a number indicating the comparison.

const str1 = "a";
const str2 = "b";
const result = str1.localeCompare(str2);
console.log(result); // -1 (because "a" is less than "b" in lexicographic order)
Copied!

Returns the Unicode normalized form of the string, useful for comparing Unicode characters.

const str = "\u00F1";
const normalizedStr = str.normalize("NFD");
console.log(normalizedStr); // "ñ"
Copied!

Conversion Methods

MethodDescription
toString()Converts and returns the string as a text representation.
valueOf()Returns the primitive value of a String object.

Converts and returns the string as a text representation.

const num = 123;
const str = num.toString();
console.log(str); // "123"
Copied!

Returns the primitive value of a String object.

const str = new String("Hello World");
const primitiveValue = str.valueOf();
console.log(primitiveValue); // "Hello World"
Copied!

Access Methods

MethodDescription
charAt()Returns the character at the specified index
charCodeAt()Returns the Unicode value of a character
codePointAt()Returns the Unicode value of a character (16 bits)

Returns the character at the specified index.

const str = "Hello World";
const char = str.charAt(6);

console.log(char); // "W"
Copied!

Returns the Unicode value of the character at the specified index.

const str = "Hello World";
const code = str.charCodeAt(6);

console.log(code); // 87 (Unicode code for "W")
Copied!

Returns the Unicode value of a character at the specified index, including characters over 16 bits.

const str = "😊";
const codePoint = str.codePointAt(0);

console.log(codePoint); // 128522
Copied!

Template Methods

MethodDescription
padStart()Pads the beginning to reach a determined length
padEnd()Pads the end to reach a determined length
repeat()Repeats a string a number of times

Pads the string at the beginning with another string until it reaches a determined length.

const str = "5";
const paddedStr = str.padStart(3, "0");

console.log(paddedStr); // "005"
Copied!

Pads the string at the end with another string until it reaches a determined length.

const str = "5";
const paddedStr = str.padEnd(3, "0");

console.log(paddedStr); // "500"
Copied!

Returns a new string with a specified number of copies of the original string concatenated.

const str = "abc";
const repeatedStr = str.repeat(3);

console.log(repeatedStr); // "abcabcabc"
Copied!

Regex Methods

MethodDescription
match()Searches for matches between a string and a regular expression
matchAll()Returns an iterator with all matches of a regular expression

Searches for matches between a string and a regular expression, returning an array or null.

const str = "Hello World";
const result = str.match(/World/);
console.log(result); // ["World"]
Copied!

Returns an iterator with all matches of a regular expression in the string.

const str = "Hello World World";
const iterator = str.matchAll(/World/g);
console.log([...iterator]); // [["World"], ["World"]]
Copied!