Create a program to read the string with different methods in javascript. In this Java script example, create a simple web page to design and perform string operations that read the string in different methods using HTML, CSS, JS.
Read string in JS with charAt(x) method
Problem statement
In this task, we will learn how to read strings with the charAt(x) methods using the javascript functions. For that create an HTML file and use the js function that displays the output.
Solution
- Create html file
- Enter heading as” Read string with different methods.” in<h2> </h2> tag
- In javascript First Enter string with var str1=’Beatiful!!!’; print given string using document.write(“<b>”+”Str1 -“+str1+”</br>”+”</b>”);
- Use function charAt() this will return character at X position within string document.write(“String character at 3 is – “+ str1.charAt(3)+”</br>”+”</br>”);
JavaScript code to read string with charAt(x) method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Read string</title> </head> <body style="text-align: center;"> <h2 style="color: greenyellow; font-style: italic;font-weight: bold;">Read string with different methods</h2> <b>Using charAt(x) method</b></br> <script type="text/javascript"> //charAt(x) var str1='Beatiful!!!'; document.write("</br>"+"<b>"+"String1 -"+str1+"</br>"+"</b>"); document.write("String character at 3 is - "+ str1.charAt(3)+"</br>"+"</br>"); </script> </body> </html>
Output
Read string in JavaScript with charAt(position) method
Problem statement
In this task, we will learn how to read strings with the charAt(position) methods using the javascript functions. For that create an HTML file and use the js function that displays the output.
Solution
- Create html file
- Enter heading as” Read string with different methods.” in<h2> </h2> tag
- Enter second string var str2=”Javascript”; print given string using document.write(“<b>”+”Str2 -“+str2+”</br>”+”</b>”);
- Use function charAt(position) this will return unicode value of character at position x within string document.write(“String character at position 1 is-“+str2.charAt(1)+”</br>”+”</br>”);
JavaScript code to read string with charAt(position) method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Read string</title> </head> <body style="text-align: center;"> <h2 style="color: greenyellow; font-style: italic;font-weight: bold;">Read string with different methods</h2> <b>Using charAt(position) method</b></br> <script type="text/javascript"> //charAt(position) var str2="Javascript"; document.write("<b>"+"</br>"+"Str2 -"+str2+"</br>"+"</b>"); document.write("String character at position 1 is-"+str2.charAt(1)+"</br>"+"</br>"); </script> </body> </html>
Output
Read string in JavaScript with concat(v1,v2) method
Problem statement
In this task, we will learn how to read strings with the concat(v1,v2) method using the javascript functions. For that create an HTML file and use the js function that displays the output.
Solution
- Create html file
- Enter heading as” Read string with different methods.” in<h2> </h2> tag
- Enter Third string var str3=”String”; concat string using concat() function combines one or more strings var final=str3.concat(“must have”,”characters”); print given string using document.write(“<b>”+”Str3 -“+str3+”</br>”+”</b>”);
- Display result using document.write(“String concat is-“+final+”</br>”+”</br>”);
JavaScript code to read string with concat(v1,v2)method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Read string</title> </head> <body style="text-align: center;"> <h2 style="color: greenyellow; font-style: italic;font-weight: bold;">Read string with different methods</h2> <b>Using concat(v1,v2) method</b></br> <script type="text/javascript"> //concat(v1,v2) var str3="String"; var final=str3.concat("must have","characters"); document.write("<b>"+"Str3 -"+str3+"</br>"+"</b>"); document.write("String concat is-"+final+"</br>"+"</br>"); </script> </body> </html>
Output
Read string in JavaScript with lastindexOf(substr,[start]) method
Problem statement
In this task, we will learn how to read strings with the lastindexOf(substr,[start]) method using the javascript functions. For that create an HTML file and use the js function that displays the output.
Solution
- Create html file
- Enter heading as” Read string with different methods.” in<h2> </h2> tag
- Enter fourth string var str4=”Hi my name is Rekha”;
print this string using document.write(“<b>”+”Str4 -“+str4+”</br>”+”</b>”);
- Use function lastIndexOf() this searches and returns the index number of the searched character within string document.write(” Last Index of string k is – “+str4.lastIndexOf(‘k’)+”</br>”+”</br>”);
JavaScript code to read string with lastindexOf(substr,[start])method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Read string</title> </head> <body style="text-align: center;"> <h2 style="color: greenyellow; font-style: italic;font-weight: bold;">Read string with different methods</h2> <b>Using lastindexOf(substr,[start]) method</b></br> <script type="text/javascript"> //lastindexOf(substr,[start]) var str4="Hi my name is Rekha"; document.write("<b>"+"Str4 -"+str4+"</br>"+"</b>"); document.write(" Last Index of string k is - "+str4.lastIndexOf('k')+"</br>"+"</br>"); </script> </body> </html>
Output
Read string in JavaScript with search(regexp) method
Problem statement
In this task, we will learn how to read strings with the search(regexp)method using the javascript functions. For that create an HTML file and use the js function that displays the output.
Solution
- Create html file
- Enter heading as” Read string with different methods.” in<h2> </h2> tag
- Enter 5 string with regex var intRegex=/[0-9-()+]+$/;
var str5=’0009′; use function search(intRegex) that test for match in string and returns index of match if not found var isInt=str5.search(intRegex); - Print sting with document.write(“<b>”+”Str5 -“+str5+”</br>”+”</b>”);
- Print result document.write(“String index of match is-“+isInt+”</br>”+”</br>”);
JavaScript code to read string with search(regexp) method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Read string</title> </head> <body style="text-align: center;"> <h2 style="color: greenyellow; font-style: italic;font-weight: bold;">Read string with different methods</h2> <b>Using search(regexp) method</b></br> <script type="text/javascript"> //search(regexp) var intRegex=/[0-9-()+]+$/; var str5='0009'; var isInt=str5.search(intRegex); document.write("<b>"+"Str5 -"+str5+"</br>"+"</b>"); document.write("String index of match is-"+isInt+"</br>"+"</br>"); </script> </body> </html>
Output
Read string in JavaScript with slice(start,end) method
Problem statement
In this task, we will learn how to read strings with the slice(start,end) method using the javascript functions. For that create an HTML file and use the js function that displays the output.
Solution
- Create html file
- Enter heading as” Read string with different methods.” in<h2> </h2> tag
- Enter 6 the string var str6=”excellent”; print this string using document.write(“<b>”+”Str6 -“+str6+”</br>”+”</b>”);
- Use function slice() it will return a substring of string based on the start and end index document.write(“String based on start and end is-“+str6.slice(0,4)+”</br>”+str6.slice(2,4)+ “</br>”);
JavaScript code to read string with slice(start,end) method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Read string</title> </head> <body style="text-align: center;"> <h2 style="color: greenyellow; font-style: italic;font-weight: bold;">Read string with different methods</h2> <b>Using slice(start,end) method</b></br> <script type="text/javascript"> //slice(start,end) var str6="excellent"; document.write("<b>"+"Str6 -"+str6+"</br>"+"</b>"); document.write("String based on start and end is-"+str6.slice(0,4)+"</br>"+str6.slice(2,4)+ "</br>"); </script> </body> </html>
Output
In this way, we will learn how to read strings with different methods using JS.