AngularJS (54 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (44 Blogs)
  • The Complete WebDeveloper (34 Blogs)
  • ReactJS (10 Blogs)
  • JavaScript and JQuery Essentials Training (2 Blogs)
SEE MORE

What are the top 20 Javascript String Functions and how to use them?

Last updated on Apr 17,2024 23.7K Views

A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.
19 / 31 Blog from JavaScript

Javascript is a high-level programming language which is having curly bracket syntax, dynamic typing & prototype based on object-orientation. In Java programming, the strings are treated as articles. Java stage gives the String class to create and control strings. This JavaScript String Functions will list out some of the most commonly used string functions.

Some of the important javascript string functions include:

Now let’s move ahead and have a look the string functions.

1.charAt(x)

This function will return the character at the x position within the string.

//charAt(x)
var myString = 'jQuery FTW!!!';
console.log(myString.charAt(7));
//output: F

Moving on with this article on Javascript String functions, Let’s have a look at the next one.

2.charCodeAt(x)

This function will return the unicode value of the character at position ‘x’ within the string.

//charAt(position)
var message="jquery4u"
//alerts "q"
alert(message.charAt(1)

3.concat(v1,v2..)

This function combines one or more strings(argv1,v2 etc) into existing one.

//concat(v1, v2,..)
var message="Sam"
var final=message.concat(" is a"," hopeless romantic.")
//alerts "Sam is a hopeless romantic."
alert(final)

 

4.fromCharcode(c1,c2)

Function will return a string created by using specified sequence of unicode values(argc1,c2).

//fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97,98,99,120,121,122))
//output: abcxyz
console.log(String.fromCharCode(72,69,76,76,79))
//output: HELLO

5.indexOf(substr, [start])

Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is 0.

//indexOf(char/substring)
var sentence="Hi, my name is Sam!"
if (sentence.indexOf("Sam")!=-1)
alert("Sam is in there!")

Moving on with this article on Javascript String functions

6.lastIndexOf(substr, [start])

Searches and (if found) returns the index number of the searched character or substring within the string. Searches the string from end to the beginning. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is string.length-1.

//lastIndexOf(substr, [start])
var myString = 'javascript rox';
console.log(myString.lastIndexOf('r'));
//output: 11

7.match(regexp)

Executes a search for a match within a string based on a regular expression. It returns an array of information or null if no match is found.

//match(regexp) //select integers only
var intRegex = /[0-9 -()+]+$/;  

var myNumber = '999';
var myInt = myNumber.match(intRegex);
console.log(isInt);
//output: 999

var myString = '999 JS Coders';
var myInt = myString.match(intRegex);
console.log(isInt);
//output: null

Moving on with this article on Javascript String functions, let’s understand replace function.

8.replace(regexp/substr, replacetext)

Searches and replaces the regular expression (or sub string) portion (match) with the replaced text instead.

//replace(substr, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(/JavaScript/i, "jQuery"));
//output: 999 jQuery Coders

//replace(regexp, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(new RegExp( "999", "gi" ), "The"));
//output: The JavaScript Coders

9.search(regexp)

Tests for a match in a string. It returns the index of the match, or -1 if not found.

//search(regexp)
var intRegex = /[0-9 -()+]+$/;  

var myNumber = '999';
var isInt = myNumber.search(intRegex);
console.log(isInt);
//output: 0

10.slice(start, [end])

This function returns a substring of the string based on the “start” and “end” index arguments, NOT including the “end” index itself. “End” is optional, and if none is specified, the slice includes all characters from “start” to end of the string.

//slice(start, end)
var text="excellent"
text.slice(0,4) //returns "exce"
text.slice(2,4) //returns "ce"

Moving on with this article on Javascript String functions

11.split(delimiter, [limit])

This will split a string into many according to the specified delimiter, and returns an array containing each element. The optional “limit” is an integer that lets you specify the maximum number of elements to return.

//split(delimiter)
var message="Welcome to jQuery4u"
//word[0] contains "We"
//word[1] contains "lcome to jQuery4u"
var word=message.split("l")

12.substr(start, [length])

This function returns the characters in a string beginning at “start” and through the specified number of characters, “length”. “Length” is optional, and if omitted, up to the end of the string is assumed.

//substring(from, to)
var text="excellent"
text.substring(0,4) //returns "exce"
text.substring(2,4) //returns "ce"

 

13.substring(from, [to])

It returns the characters in a string between “from” and “to” indexes, NOT including “to” itself. “To” is optional, and if omitted, up to the end of the string is assumed.

//substring(from, [to])
var myString = 'javascript rox';
myString = myString.substring(0,10);
console.log(myString)
//output: javascript

14.toLowerCase()

This will return the string with all of its characters converted to lowercase.

//toLowerCase()
var myString = 'JAVASCRIPT ROX';
myString = myString.toLowerCase();
console.log(myString)
//output: javascript rox

 

15.toUpperCase()

This will return the string with all of its characters converted to uppercase.

//toUpperCase()
var myString = 'javascript rox';
myString = myString.toUpperCase();
console.log(myString)
//output: JAVASCRIPT ROX

 

16. includes()

It is used to check whether a string contains the specified string or characters.

//includes()
var mystring = "Hello, welcome to edureka";
var n = mystring.includes("edureka");
//output: True

 

17. endsWith()

This function checks whether a string ends with specified string or characters.

//endsWith()
var mystr = "List of javascript functions";
var n = mystr.endsWith("functions");
//output: True

 

18. repeat()

This returns a new string with a specified number of copies of an existing string.

//repeat()
var string = "Welcome to Edureka";
string.repeat(2);
//output: Welcome to Edureka Welcome to Edureka

 

19. valueOf() 

It is used to return the primitive value of a String object.

//valueOf()
var mystr = "Hello World!";
var res = mystr.valueOf();
//output: Hello World!

 

20. trim()

This function removes whitespace from both ends of a string.

//trim()
var str = "     Hello Edureka!     ";
alert(str.trim());

These were some of the most commonly used JavaScript String functions. With this, we have come to the end of our article.

Now that you know about JavaScript String Functions, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Check out the Online Angular Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.

If you want to get trained in React and wish to develop interesting UI’s on your own, then check out the React Certification by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of “JavaScript String Functions” and we will get back to you.

Upcoming Batches For Angular Course Online with Certification
Course NameDateDetails
Angular Course Online with Certification

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Angular Course Online with Certification

Class Starts on 29th June,2024

29th June

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What are the top 20 Javascript String Functions and how to use them?

edureka.co