Blog
JavaScript

JavaScript String Methods and Manipulation

Powerful Built-in Methods for Text Operations

String length

html

Result:

String charAt()

html

Result:

String at()

html

Result:

String charcodeAt()

html

Result:

JavaScript codePointAt()

Get code point value at the first position in a string:

javascript

JavaScript String concat()

concat() joins two or more strings:

javascript

The concat() method can be used instead of the plus operator. These two lines do the same:

javascript
All string methods return a new string. They don't modify the original string. Formally said: Strings are immutable: Strings cannot be changed, only replaced.

Extracting String Parts

There are 3 methods for extracting a part of a string:

  • slice(start, end)
  • substring(start, end)
  • substr(start, length)

JavaScript String slice()

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

The method takes 2 parameters: start position, and end position (end not included).

Example: Slice out a portion of a string from position 7 to position 13.

javascript
JavaScript counts positions from zero. First position is 0, Second position is 1

Example: If you omit the second parameter, the method will slice out the rest of the string.

javascript

If a parameter is negative, the position is counted from the end of the string:

javascript

This example slices out a portion of a string from position -12 to position -6:

javascript

JavaScript String substring()

substring() is similar to slice(). The difference is that start and end values less than 0 are treated as 0 in substring().

javascript

If you omit the second parameter, substring() will slice out the rest of the string.

JavaScript String substr()

substr() is similar to slice(). The difference is that the second parameter specifies the length of the extracted part.

The substr() method is removed (deprecated) in the latest JavaScript standard. Use substring() or slice() instead.
javascript

If you omit the second parameter, substr() will slice out the rest of the string.

javascript

If the first parameter is negative, the position counts from the end of the string.

javascript

Converting to Upper and Lower Case

A string is converted to upper case with toUpperCase():

A string is converted to lower case with toLowerCase():

JavaScript String toUpperCase()

javascript

JavaScript String toLowerCase()

javascript

JavaScript String isWellFormed()

The isWellFormed() method returns true if a string is well formed.

Otherwise it returns false. A string is not well formed if it contains lone surrogates.

javascript
javascript

JavaScript String toWellFormed()

The String method toWellFormed() returns a new string where all "lone surrogates" are replaced with the Unicode replacement character (U+FFFD).

javascript

JavaScript String trim()

The trim() method removes whitespace from both sides of a string:

javascript

JavaScript String trimStart()

ECMAScript 2019 added the String method trimStart() to JavaScript. The trimStart() method works like trim(), but removes whitespace only from the start of a string.

javascript

JavaScript String trimEnd()

ECMAScript 2019 added the string method trimEnd() to JavaScript. The trimEnd() method works like trim(), but removes whitespace only from the end of a string.

javascript

JavaScript String Padding

ECMAScript 2017 added two new string methods to JavaScript: padStart() and padEnd() to support padding at the beginning and at the end of a string.

JavaScript String padStart()

The padStart() method pads a string from the start. It pads a string with another string (multiple times) until it reaches a given length.

Pad a string with "0" until it reaches the length 4:

javascript

Pad a string with "x" until it reaches the length 4:

javascript
The padStart() method is a string method. To pad a number, convert the number to a string first. See the example below.
javascript

JavaScript String padEnd()

The padEnd() method pads a string from the end. It pads a string with another string (multiple times) until it reaches a given length.

javascript
javascript
The padEnd() method is a string method. To pad a number, convert the number to a string first. See the example below.
javascript

JavaScript String repeat()

The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string.

javascript
javascript

avatar

0 Comments

No comments

Leave a Reply

avatar

Recent Post

Related Topics

    Feeds

      Don't miss what's next 👋

      Enjoyed this content? Leave your email to get notified when we publish new insights, tutorials, and updates — no spam, ever.