Join Class

String methods in javascript

Learn JavaScript string methods that will help you to manipulate string values. there are many built-in methods to work with string in javascript.

javascript string functions

Here we learn how to work with string in javascript, string length, substring, slice, trim, find, concat, indexOf, replace, Upper and Lower Case etc.

built-in string methods in JavaScript

JavaScript substring method example

Learn how to use JavaScript substring method and string length example.

var _message = "I am learning javascript in WebTrainingRoom";            
var _resultString = _message.substring(4, 25);

4 is our start point
25 is our end point
now if you print the value of _resultString, you will see "learning javascript"

To find the length of any string, you need to use .length property, here is an example

var _message = "I am learning javascript string";
var _strLength=_message.length;
//_strLength will print the value string length

Note: while writing the above code, have you noticed that we used = equal sign? that’s not equal, that’s the way assigning the value to a variable. to compare two variable if equal, in JavaScript we use ==, learn all javaScript operators

Javascript Slice methdo example

Slice is almost same as Substring, but here you can specify negative number too

var _message = "I am learning javascript in WebTrainingRoom";            
var _resultString = _message.slice(4, -25);
Javascript trim function example

JavaScript trim() method used for removing the extra space in front and back

var _message = "    javascript trim function example      ";
var _resultString = _message.trim();

JavaScript String indexOf Method

JavaScript indexOf() method used for finding the position of a string within a string

var _message = "I am learning javascript indexof method";
var _resultPosition = _message.indexOf("learning");

in above example you see can see the position of word "learning" within that sentence
You also can sepcify the start index in case there are multiple location of string you are looking for.

var _message = "I am learning javascript indexof method, javascript is fun";
var _resultPosition = _message.indexOf("javascript",20);

Now in above example there are two "javascript" word , and this will tell you the index of second location.

Javascript string Upper and Lower case Example

Javascript has method to change case Upper or Lower Case

var _sampleString = "JavaScript String Methods";
var _uppercaseStringt = _sampleString.toUpperCase();
var _lowercaseStringt = _sampleString.toLowerCase();
// result will be "javascript string methods"
Javascript string replace method Example

Javascript replace method will replace the one string with another string

var _message = "I am learning javascript in WebTrainingRoom";
var _resultString = _message.replace("javascript","JSON")
Now _resultString will print  "I am learning JSON in WebTrainingRoom";

Notice, in above example, we have replaced _message.replace("find-value","replace-with") javascript with json.

JavaScript programming tutorials to learn JavaScript coding step by step, develop website using JavaScript and html.
Advanced JavaScript
Javascript String Methods
javascript Interview Questions Answers
Javascript String function examples
JavaScript Examples | JavaScript Online Course