String Method example in JavaScript

The blog provides the JavaScript String method examples. The blog helps you understand the available JavaScript String methods and how to be used in your programming.

JavaScript String charAt()

The JavaScript String charAt() method returns the character at specified position /index
Integer range : between 0 and 65535
Parameters : index which indicates the index value of the character. Default value is 0

Syntax: String.CharAt(index)
JavaScript String charCodeAt()

The JavaScript String charCodeAt() method returns the UTF-16 code for the integer value.
Integer range : between 0 and 65535
Parameters : index which indicates the character position. Default value is 0.

Syntax: String.CharCodeAt(index)
JavaScript String codePointAt()

The JavaScript String codePointAt() method returns an integer denoting the Unicode point value
Parameters : index which indicates the character position

Return Value: Number when processed else undefined value

Syntax: String.codePointAt(index)
JavaScript String fromCodePoint()

The JavaScript String codePointAt() method returns the string created from given sequence of code points

Syntax: String.fromCodePoint(num1, ..., numN)
<script>
let name = "Mohit Sharma!";

let utf16Value1 = name.charCodeAt(0);
document.writeln(`Converted UTF-16 code for '${name.charAt(0)}': ${utf16Value1}` +"<br>");

let utf16Value2 = name.charCodeAt(1);
document.writeln(`Converted UTF-16 code for '${name.charAt(1)}': ${utf16Value2}` +"<br>");

let utf16Value3 = name.charCodeAt(2);
document.writeln(`Converted UTF-16 code for '${name.charAt(2)}': ${utf16Value3}` +"<br>");

let utf16Value4 = name.charCodeAt(3);
document.writeln(`Converted UTF-16 code for '${name.charAt(3)}': ${utf16Value4}` +"<br>");

let utf16Value5 = name.charCodeAt(4);
document.writeln(`Converted UTF-16 code for '${name.charAt(4)}': ${utf16Value5}` +"<br>");

let utf16Value6 = name.charCodeAt(name.length - 1);
document.writeln(`Converted UTF-16 code for '${name.charAt(name.length - 1)}': ${utf16Value6}` +"<br>");

document.writeln("String codePointAt() example"+"<br>");

let codepointValue7 = name.codePointAt(6);
document.writeln(`CodePoint value for '${name.charAt(6)}': ${codepointValue7}` +"<br>");

document.writeln("String fromCodePoint() example" + "<br>");
let StringFromCodePoint = String.fromCodePoint(77, 111, 104, 105, 116);
document.writeln(`String fromCodePoint()value is  : ${StringFromCodePoint}` +"<br>");

</script>

Output:

Converted UTF-16 code for 'M': 77
Converted UTF-16 code for 'o': 111
Converted UTF-16 code for 'h': 104
Converted UTF-16 code for 'i': 105
Converted UTF-16 code for 't': 116
Converted UTF-16 code for '!': 33

String codePointAt() example
CodePoint value for 'S': 83

String fromCodePoint() example
String fromCodePoint()value is : Mohit
JavaScript String toLowerCase(), toUpperCase()

The JavaScript String toLowerCase() method returns the string in Lower Case

Sytax: String.toLowerCase()

The JavaScript String toUpperCase() method returns the string in Upper Case

Sytax: String.toUpperCase()
JavaScript String startWith() , endsWith()

The JavaScript String startsWith() method validates if the given string starts with the specified characters

Syntax: String.startsWith(searchString, position)

The JavaScript String endsWith() method validates if the given string ends with the specified characters

Syntax: String.endsWith(searchString, position)
JavaScript String replace()

The JavaScript replace() method replaces the first occurrence of the pattern with the specified string and returns the modified string. The pattern could be the string value or the Regex value which is considered for replacement.

Syntax: String.replace(pattern, replacement)
pattern: string which is to be considered for replacement
replacement: actual string value to be used for replacement
JavaScript String replaceAll()

The JavaScript replaceAll() method replaces all the occurrences of the pattern with the specified string and returns the modified string. The pattern could be the string value or the Regex value which is considered for replacement.

Syntax: String.replace(pattern, replacement)
pattern: string which is to be considered for replacement
replacement: actual string value to be used for replacement
JavaScript String padStart()

The JavaScript String padStart() method allows to add padding at the start of the string

Syntax: String.padStart(StringLength, padString)
JavaScript String padEnd()

The JavaScript String padEnd() method allows to add padding at the end of the string

Syntax: String.padEnd(StringLength, padString)
 <script>
let name = "  Mohit Sharma!  ";
let paddedString ="JavaScript";
let title =" This is JavaScript String Example. String is easy to understand";
let pattern = "String";
document.writeln("------- String trim() example -------" +"<br>");
document.writeln(`Name after trim()': ${name.trim()}` +"<br>");

document.writeln(" ------- String UPPERCASE example -------" +"<br>");
document.writeln(`Name after toUpperCase()': ${name.toUpperCase()}` +"<br>");

document.writeln(" -------String lowercase example -------" +"<br>");
document.writeln(`Name after toLowerCase()': ${name.toLowerCase()}` +"<br>");

document.writeln("------- String replace example ------- " +"<br>");
let replaced_text = title.replace(pattern, "Replace String" );
document.writeln(`Modified Title': ${replaced_text}` +"<br>");

document.writeln("------- String replaceAll example -------" +"<br>");
let replaceAll_text = title.replaceAll(pattern, "Replace String");
document.writeln(`Modified Title': ${replaceAll_text}` +"<br>");

document.writeln("-------String padStart example ------- " +"<br>");
document.writeln(`PadStart String Modification': ${paddedString.padStart(15,"#")}` +"<br>");


document.writeln(" -------String padEnd Example -------" +"<br>");
document.writeln(`PadEnd String Modification': ${paddedString.padEnd(15, "#")}` +"<br>");


</script>

Output:

------- String trim() example -------
Name after trim()': Mohit Sharma!
------- String UPPERCASE example -------
Name after toUpperCase()': MOHIT SHARMA!
-------String lowercase example -------
Name after toLowerCase()': mohit sharma!
------- String replace example -------
Modified Title': This is JavaScript Replace String Example. String is easy to understand
------- String replaceAll example -------
Modified Title': This is JavaScript Replace String Example. Replace String is easy to understand
-------String padStart example -------
PadStart String Modification': #####JavaScript
-------String padEnd Example -------
PadEnd String Modification': JavaScript#####