JavaScript String Format
Posted by Superadmin on May 03 2023 02:41:29

JavaScript String Format

By Payal UdhaniPayal Udhani
  

JavaScript String Format

Introduction to JavaScript String Format

In javascript, strings are considered as a series of characters that can be specified either in double or single quotes. The indexing in string begins from zero. The first character is placed at the zeroth position, second on 1 st position and so on. For example,

var example = "Sample String";
var example = 'Sample String'; //Alternatively single quotes can be used

The format can be used to declare the string.

A basic value “Sample String” will not have any properties and methods available for it. But a variable example will be treated as objects, and objects in javascript do have access to methods or properties.

Strings are immutable in Javascript as in many other languages. Whenever any above operation or method is used to manipulate the string, a new string object is always created with specified changes while the original string literal remains unchanged. If you want to include a single or double quote as a part of the string, then you can use a backslash before the quote as an escape sequence.

Properties:

Methods Of JavaScript String

Here are different methods of the javascript string given below:

Examples of JavaScript String Format

Let’s study some of the examples of string format in java are given below:

Example #1

Let’s see the example of string comparison, which is as follows.

Code:

<!DOCTYPE html>
<html>
<body>
<h1>String Comparison In Javascript</h1>
<p id="temporary1"></p>
<p id="temporary2"></p>
<p id="temporary3"></p>
<p id="temporary4"></p>
<p id="temporary5"></p>
<script>
var string1 = new String('Strings In Javascript');
var string2 = new String('Strings In Javascript');
var string3 = 'Strings In Javascript';
var string4 = string1;
document.getElementById("temporary1").innerHTML = string1 == string2;
document.getElementById("temporary2").innerHTML = string1 == string3;
document.getElementById("temporary3").innerHTML = string1 === string4;
document.getElementById("temporary4").innerHTML = typeof(string1);
document.getElementById("temporary5").innerHTML = typeof(str3);
</script>
</body>
</html>

Output:

JavaScript String Format eg1

Example #2

Let’s see an example that will contain the usage of the most frequent methods from the above-mentioned methods.

Code:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<p id="demo5"></p>
<p id="demo6"></p>
<p id="demo7"></p>
<p id="demo8"&gt;</p>
<p id="demo9"></p>
<p id="demo10"></p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
//charAt(position)
var myString = "Here We Go!";
document.getElementById("demo1").innerHTML = myString.charAt(9);
//concat(v1, v2,..)
var message="Everyone";
var final=message.concat(" is "," happy.");
document.getElementById("demo2").innerHTML = final;
//output:Everyone is happy."
//fromCharCode(c1, c2,...)
document.getElementById("demo3").innerHTML = String.fromCharCode(72,69,76,76,79);
//output: HELLO
//indexOf(char/substring)
var sentence="Hi, Let's start javascript";
if (sentence.indexOf("start")){
document.getElementById("demo4").innerHTML= "Demo of index of";
}
//lastIndexOf(substr, [start])
var myString = "javascript rocks";
document.getElementById("demo5").innerHTML =myString.lastIndexOf("r");
//output: 11
//match(regexp) //select integers only
var intRegex = /[0-9 -()+]+$/;
var myNumber = "999";
document.getElementById("demo6").innerHTML=myNumber.match(intRegex);
//output: 999
var myString = "999 JS Coders";
document.getElementById("demo7").innerHTML =myString.match(intRegex);
//output: null
//replace(substr, replacetext)
var myString = "Java Coders";
document.getElementById("demo8").innerHTML =myString.replace(/Java/i, "JavaScript");
//output: JavaScript Coders
//toLowerCase()
var myString = "JAVASCRIPT ROCKS";
myString = myString.toLowerCase();
document.getElementById("demo9").innerHTML =myString;
//output: javascript rocks
//toUpperCase()
var myString = "javascript rocks";
myString = myString.toUpperCase();
document.getElementById("demo10").innerHTML =myString;
//output: JAVASCRIPT ROCKS
}
</script>
</body>
</html>

Output:

JavaScript String Format eg2.1

After clicking on the button,

Example 2.2