Top JavaScript String Methods

Top JavaScript String Methods
----------------------------------------------

We are going to share Top javsscript string manipulation method by which you can speed up your development.

List of Top JavaScript String Methods we will talk.

  • toUpperCase()
  • toLowerCase()
  • concat()
  • String.trim()
  • charAt()
  • String Property Access 
  • Converting a String to an Array(split())
  • String Length
  • Finding a String in a String(indexOf() and lastIndexOf())
  • search() 
  • slice()
  • substring() 
  • substr() 
  • replace()

Lets take a look in each one of them in detailed way.

1) toUpperCase() 

By this method string will be converted to uppar case like "abc" will be "ABC"

coderuck-String-toUpparCase

2)toLowerCase()

A string is converted to lower case with toLowerCase() like "AbCD" -> "abcd"

coderuck-String-toLowerCase

3)concat()

concat method is used to join two string

coderuck-String-conca

4)trim()

trim() method is used to remove whitespace from both side of string.Sometime its better to remove whitespace because cannot be make sure frontuser will give input without space.

coderuck-String-trim

5)charAt()

charAt() method is used to return the character at a specified index (position) in a string.

coderuck-String-chatAt

6) String Property Access 

If you want to access string property with respect to array index like.

coderuck-String-stringAccess

7)split()

If you wan to convert string to array you can use  method to make it work.

coderuck-String-split

 

8)length 

length property of javsacript will return the length of given string.This property is usefull is you want to check if string is not empty.

coderuck-String-length

9)Finding a String inside a String(indexOf() and lastIndexOf())

If you want to check if one string contain another string in javascript you can use indexOf() and lastIndexOf() methods.But the major difference between two method is The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string and lastIndexOf() method returns the index of the last occurrence of a specified text in a string

coderuck-String-indexOf-lastIndexOf

Both method will return -1 is checked string is not found.

10)search()

search() is also similar to indexOf() method but the major difference between two method is 

  • The search() method cannot take a second start position argument.
  • The indexOf() method cannot take regular expressions.

coderuck-String-search

11)Extracting String Parts

  • slice(startend)
  • substring(startend)
  • substr(startlength)

slice(startend)

coderuck-String-slice

substring(startend)

coderuck-String-substring

substr(startlength)

coderuck-String-substr

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

12) replace()

replace() method replace given string with new string as 

coderuck-String-replace

As above example you can see replace() method does not change the original string instead of that it return the update string.Most important replace() method is case sensitive so you need to take care while using it.

coderuck-String-replace-2

There are others methods in javascript for string.But the above are most used.

 

Categories: Java Script Tags: #ES6, #JavaScript,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.