String methods JavaScript

String methods

String length

JavaScript String length

let v = "Hello World".length;
console.log('v =',v);

Output

v = 11

Steps Description

  • Declare variable v
  • Initialize variable v with value of string v = “Hello World”.length
  • String .length Calculates the length of a string by counting its characters and output it as a number.
  • The value of v variable is displayed with the help of console.log inbuilt function

String concat

JavaScript String concat() Method

let b = "Hello".concat(" ","World");
console.log('b =',b);

Output

b = Hello World

Steps Description

  • Declare variable b
  • Initialize variable b with value of three string b = “Hello”.concat(” “,”World”)
  • Multiple strings are concatenated to the first string by the concat() method.
  • The value of b variable is displayed with the help of console.log inbuilt function

String replace

JavaScript String replace() Method

let l = "Python is best language".replace('Python','JavaScript');
console.log('l =',l);

Output

l = JavaScript is best language

Steps Description

  • Declare variable l
  • Initialize variable l with value of string l = “Python is best language”.replace(‘Python’,’JavaScritp’)
  • replace() method if a word or character is found in the string after searching, then it works to replace that word or character.
  • The value of l variable is displayed with the help of console.log inbuilt function

String charAt

JavaScript String .charAt() Method

let a = "Hello".charAt(4);
console.log('a =',a);

Output

a = o

Steps Description

  • Declare variable a
  • Initialize variable a with value of string a=”Hello”
  • The charAt() method returns the character of the given string.
  • The value of a variable is displayed with the help of console.log inbuilt function

String search

JavaScript String .search() Method

let m = "Hello World".search('r');
console.log('m =',m);

Output

m = 8

Steps Description

  • Declare variable m
  • Initialize variable m with value of string m = “Hello World”.search(‘r’)
  • search() method is used to search a string Will print the index number of a word or character if found in the given string word or character
  • The value of m variable is displayed with the help of console.log inbuilt function

String indexOf

JavaScript String .indexOf() Method

let f = "Hello World".indexOf("World");
console.log('f =',f);

Output

f = 6

Steps Description

  • Declare variable f
  • Initialize variable f with value of string f = “Hello World”.indexOf(“World”)
  • indexOf() method will print the number of the index if the word or character is found in the string. will print -1 if not found
  • The value of f variable is displayed with the help of console.log inbuilt function

String lastIndexOf

JavaScript String .lastIndexOf() Method

let g = "Hello in World in JavaScript".lastIndexOf("in");
console.log('g =',g);

Output

g = 15

Steps Description

  • Declare variable g
  • Initialize variable g with value of string g = “Hello in World in JavaScript”.lastIndexOf(“in”)
  • lastIndexOf() method is used to find a word or letter from the end of the string. When searching for a word or character in a string, if the word or character is found, its index number will be printed. will print -1 if not found
  • The value of g variable is displayed with the help of console.log inbuilt function

String includes

JavaScript String .includes() Method

let e = "Hello World".includes("World");
console.log('e =',e);

Output

e = true

Steps Description

  • Declare variable e
  • Initialize variable e with value of string “Hello World”.includes(“World”)
  • includes() method is used to search in a string. Returns true if the word or letter is found in the given string, otherwise false.
  • The value of e variable is displayed as a return boolean value with the help of console.log inbuilt function

String startsWith

JavaScript String .startsWith() Method

let c = "Hello my friend".startsWith("My");
console.log('c =',c);

Output

c = false

Steps Description

  • Declare variable c
  • Initialize variable c with value of string “Hello”.startsWith(“e”)
  • startsWith() method is used to find the first word or character of a string. returns boolean value
  • The value of c variable is displayed with the help of console.log inbuilt function

String endsWith

JavaScript String .endsWith() Method

let d = "World".endsWith("d");
console.log('d =',d);

Output

d = true

Steps Description

  • Declare variable d
  • Initialize variable d with value of string d = “World”.endsWith(“d”)
  • endsWith() method is used to find the last word or character of a string. returns boolean value
  • The value of d variable is displayed with the help of console.log inbuilt function

String trim

JavaScript String .trim() Method

let s = "  Hello World   ".trim();
console.log('s =',s);

Output

s = Hello World

Steps Description

  • Declare variable s
  • Initialize variable s with value of string s = ” Hello World “.trim()
  • trim() Method If there is one or more spaces between the first and last of the string, the trim method works to remove it.
  • The value of s variable is displayed with the help of console.log inbuilt function

String trim

JavaScript String .trim() Method

let s = "  Hello World   ".trim();
console.log('s =',s);

Output

s = Hello World

Steps Description

  • Declare variable s
  • Initialize variable s with value of string s = ” Hello World “.trim()
  • trim() Method If there is one or more spaces between the first and last of the string, the trim method works to remove it.
  • The value of s variable is displayed with the help of console.log inbuilt function

String trimStart

JavaScript String .trimStart() Method

let t = "  Hello World   ".trimStart();
console.log('t =',t);

Output

t = Hello World

Steps Description

  • Declare variable t
  • Initialize variable t with value of string t = ” Hello World “.trimStart()
  • trimStart() Method If a string contains one or more spaces at the beginning, the trimStart method serves to remove it.
  • The value of t variable is displayed with the help of console.log inbuilt function

String trimEnd

JavaScript String .trimEnd() Method

let u = "  Hello World   ".trimEnd();
console.log('u =',u);

Output

u =   Hello World

Steps Description

  • Declare variable u
  • Initialize variable u with value of string u = ” Hello World “.trimEnd()
  • trimEnd() Method If a string contains one or more spaces at the end, the trimEnd method serves to remove it.
  • The value of u variable is displayed with the help of console.log inbuilt function

String padStart

JavaScript String .padStart() Method

let i = "Hello World".padStart(12,"1");
console.log('i =',i);

Output

i = 1Hello World

Steps Description

  • Declare variable i
  • Initialize variable i with value of string i = “Hello World”.padStart(12,”1″)
  • padStart() method can add a character, number, or symbol to the beginning of a string.
  • The value of i variable is displayed with the help of console.log inbuilt function

String padEnd

JavaScript String .padEnd() Method

let j = "Hello World JavaScript".padEnd(23,"|");
console.log('j =',j);

Output

j = Hello World JavaScript|

Steps Description

  • Declare variable j
  • Initialize variable j with value of string j = “Hello World JavaScript”.padEnd(23,”|”)
  • padEnd() method adds a character, number, or symbol to the end of a string.
  • The value of i variable is displayed with the help of console.log inbuilt function

String toUpperCase

JavaScript String .toUpperCase() Method

let r = "Hello World".toUpperCase();
console.log('r =',r);

Output

r = HELLO WORLD

Steps Description

  • Declare variable r
  • Initialize variable r with value of string r = “Hello World”.toUpperCase()
  • toUpperCase() method converts all words or characters in a string to uppercase.
  • The value of r variable is displayed with the help of console.log inbuilt function

String toLowerCase

JavaScript String .toLowerCase() Method

let q = "Hello World".toLowerCase();
console.log('q =',q);

Output

q = hello world

Steps Description

  • Declare variable q
  • Initialize variable q with value of string q = “Hello World”.toLowerCase()
  • toLowerCase() method Converts all words or characters in a string to lowercase.
  • The value of q variable is displayed with the help of console.log inbuilt function

String slice

JavaScript String .slice() Method

let n = "Hello World JavaScript Language".slice(1,5);
console.log('n =',n);

Output

n = ello

Steps Description

  • Declare variable n
  • Initialize variable n with value of string n = “Hello World JavaScript Language”.slice(1,5)
  • slice() method can pick up words, letters, or words from one set position to another set position in a string.
  • The value of n variable is displayed with the help of console.log inbuilt function

String split

JavaScript String .split() Method

let o = "Hello World JavaScript Language".split("World");
console.log('o =',o);

Output

o = [ 'Hello ', ' JavaScript Language' ]

Steps Description

  • Declare variable o
  • Initialize variable o with value of string o = “Hello World JavaScript Language”.split(“World”)
  • split() method will search for a given word, character, or symbol in a string and convert it into an array of commas.
  • The value of o variable is displayed with the help of console.log inbuilt function

String substring

JavaScript String .substring() Method

let p = "Hello World".substring(1,7);
console.log('p =',p);

Output

p = ello W

Steps Description

  • Declare variable p
  • Initialize variable p with value of string p = “Hello World”.substring(1,7)
  • substring() Method method can pick up words, letters, or words from one set position to another set position in a string
  • The value of p variable is displayed with the help of console.log inbuilt function

String match

JavaScript String .match() Method

let h = "Hello World in JavaScript Hello World".match(/World/g);
console.log('h =',h);

Output

h = [ 'World', 'World' ]

Steps Description

  • Declare variable h
  • Initialize variable h with value of string h = “Hello World in JavaScript Hello World”.match(/World/g)
  • If a word or character is found in the string, the match() method will create an array. else will print null
  • The value of h variable is displayed with the help of console.log inbuilt function

String repeat

JavaScript String .repeat() Method

let k = "Hello World ".repeat(3);
console.log('k =',k);

Output

k = Hello World Hello World Hello World

Steps Description

  • Declare variable k
  • Initialize variable k with value of string k = “Hello World “.repeat(3)
  • repeat() Method Whatever words or letters are written in the string, they can be repeated again and again.
  • The value of k variable is displayed with the help of console.log inbuilt function

String charCodeAt

JavaScript String .charCodeAt() Method

let w = "Hello World".charCodeAt(4);
console.log('w =',w);

Output

w = 111

Steps Description

  • Declare variable w
  • Initialize variable w with value of string w = “Hello World”.charCodeAt(4)
  • charCodeAt() method returns the ascii value of the character at the index of the character in the string.
  • The value of w variable is displayed with the help of console.log inbuilt function

String fromCharCode

JavaScript String fromCharCode() Method

let x = String.fromCharCode(65,120)
console.log('x =',x);

Output

x = Ax

Steps Description

  • Declare variable x
  • Initialize variable x with value of number x = String.fromCharCode(65,120)
  • String fromCharCode() method prints the given ascii number in character
  • The value of x variable is displayed with the help of console.log inbuilt function