JavaScript String Methods - What are string methods in Javascript



JavaScript String Methods

javascript-string-methods
  • String length
  • String slice()
  • String substring()
  • String substr()
  • String replace()
  • String replaceAll()
  • String toUpperCase()
  • String toLowerCase()
  • String concat()
  • String trim()
  • String trimStart()
  • String trimEnd()
  • String padStart()
  • String padEnd()
  • String charAt()
  • String charCodeAt()
  • String split()

JavaScript String Length Method

  • In JavaScript, you can find the length of a string by using the .length property.
  • The .length property is used to find the length of the text variable, which contains the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ". The output will be 26, which is the number of characters in the string.

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Strings</h1>
<h2>The length Property</h2>

<p>The length of the string is:</p>
<p id="demo"></p>

<script>
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = text.length;
</script>

</body>
</html>

Output

JavaScript Strings
The length Property
The length of the string is:
26

JavaScript Slice Method Example 1

  • In JavaScript, the slice() method is used to extract a portion of a string, array or typed array, and return a new array or string without modifying the original one.
  • The slice() method takes two parameters: start and end. The start parameter specifies the starting index of the slice (inclusive), and the end parameter specifies the ending index of the slice (exclusive).

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>

<p>The sliced (extracted) part of the string is:</p>
<p id="demo"></p>

<script>
let text = "venkat, kaashiv, infotech";
let part = text.slice(7,13);
document.getElementById("demo").innerHTML = part; 
</script>

</body>
</html>

Output

JavaScript Strings
The slice() Method
The sliced (extracted) part of the string is:
Kaash

JavaScript Slice Method Example 2

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>

<p>Extract a part of a string from position 7:</p>
<p id="demo"></p>

<script>
let text = "kishore, nizar, barath";
let part = text.slice(7)
document.getElementById("demo").innerHTML = part;
</script>

</body>
</html>

Output

JavaScript Strings
The slice() Method
Extract a part of a string from position 7:
, nizar, barath

JavaScript String substring() Method

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

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript String Methods</h1>
<p>The substring() method extract a part of a string and returns the extracted parts in a new string:</p>

<p id="demo"></p>

<script>
let str = " venkat, kaashiv, infotech ";
document.getElementById("demo").innerHTML = str.substring(7,13);
</script>

</body>
</html>

Output

JavaScript String Methods
The substring() method extract a part of a string and returns the extracted parts in a new string:
kaash

Related Searches to JavaScript String Methods - What are string methods in Javascript