String methods in javascript

String methods in javascript

As we all know similar to other programming languages javascript also have string as one of the primitive datatype. In javascript string is built as sequence of one or more characters that may consists of letters, numbers or symbols, string is immutable in javascript i.e once it is created then you can't update.

How can we create strings?

Strings can be created as primitives by using literals or as Objects by using String() constructor.
For example,

const string1="hello world"
const string2=`hello world`
const string3= new String("hello world")

These are the different ways of creating string, ES6 introduced template literals where we can incorporate expressions inside string. Let's see how it can be done.

const temp="literal";
const string1= `This is to display template ${temp}`
console.log(string1) // This is to display template literal

Different string methods available in javascript.

1. charAt()

This method is used to get character at specified index. This method takes one argument that is index.
Example:

const string="hello world"
string.charAt(4) // o
"hello".charAt(1) //e

2. toUpperCase() and toLowerCase()

These 2 methods are used to convert character or string to uppercase or to lowercase.
Example:

const string1= "hello world"
const string2="HELLO WORLD"
console.log(string1.toUpperCase()) // HELLO WORLD
console.log(string2.toLowerCase()) // hello world
console.log(string1[0].toUpperCase()) // H

3. trim()

This method is used to remove spaces from both ends of the string and it returns new string without any spaces at beginning and end of the string.
Example:

const string ="  hello    "
string.trim() // 'hello'

4. concat()

This method concatenates the arguments to the calling string, It will take any number of arguments and concatenates to the calling string.
Syntax:

concat( str1);
concat( str1, str2, ..., strN);

Example:

const string1="hello"
const string2="world"
string1.concat(string2) // helloworld
string1.concat(" ",string2) // hello world
string1.concat(",",string2) // hello,world

5. indexOf()

This method is used to get the index of specified string. Based on the requirement it takes argument, If we need first occurance of the string then pass single string as argument and if we need position of string after some position/index then we need to pass 2 arguments first one as string to be searched and second one as index after which the string should be searched.
Syntax:

indexOf( string );
indexOf( string, index );

Example:

const string= "hello, this is blog on string methods blog"
string.indexOf("blog"); // 15
string.indexOf("blog",16); // 38

6. slice()

This method is used to get the substring from the source string, it returns substring without modifying source string. It takes 2 arguments start index and end index. If start index not specified then substring starts from begining and if end index not specified the substring goes till end of the string. Substring counted from start till end-1.
Syntax:

slice( start, end)

Example:

const string="this is blog on string methods blog";
string.slice( 8, 12) // 'blog'
string.slice( -12, -4) // 'methods'
string.slice( -12,  30) // 'methods'

here in the examples, If -ve index specified then position count starts from the end, and whenever +ve index specified the position count starts from the begining.

7. split()

This method is used to split the string into array of substrings at the specified seperator pattern provided in the argument. If the seperator pattern not provided or seperator pattern does not exist in the string then it gives array with one element containing entire string.
Syntax:

split( seperator );
split( seperator, limit );

here limit specifies how many substring should be taken from the string Example:

const string="this is blog on string methods blog"
string.split() // ['this is blog on string methods blog'] 
string.split("") // ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'b', 'l', 'o', 'g', ' ', 'o', 'n', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'm', 'e', 't', 'h', 'o', 
                            'd', 's', ' ', 'b', 'l', 'o', 'g']
string.split(" ") // ['this', 'is', 'blog', 'on', 'string', 'methods', 'blog']
string.split(" ",3) // ['this', 'is', 'blog']

8. charCodeAt()

This method is used to get the unicode of the character present at the specified position. This returns integer between 0 and 65535.
Syntax:

charCodeAt(index)

Example:

"javascript".charCodeAt(3); // 97
"javascript".charCodeAt(15); // NaN

index: value for index should be between 0 and length of the string, If index given value greater than length of string then returns NaN

These are the few important string methods we are using frequently in our code.

Thank you for reading...

Refererences: