JavaScript - Strings
- Introduction to JavaScript strings
- Creating JavaScript strings
- Adding special characters to JavaScript strings
Introduction to JavaScript Strings
A string is an object in JavaScript. The
sequence of characters is stored in a string object such as My Blog Help is a
string. Strings are defined in double or single quotes.
As you know, JavaScript is used to
generate dynamic and interactive web pages. Because of this reason strings have
been defined as an object in JavaScript. Having the objects of strings, you can
control the presentation of the strings at page loading time according to you.
JavaScript provides you
many methods to control string representation on the webpage. With the help of these methods,
you can easily perform operations on strings. You will learn further about
these.
Creating JavaScript Strings
In JavaScript, you create strings
like any normal variable. Its simple syntax has been given below.
var stringName = "text";
As I told you before, you define
strings in single or double-quotes. So you can define even strings in single
quotes.
var stringName = 'text';
Creating strings in JavaScript is
being explained by the example below.
var name = "My Blog Help
Tutorials";
Adding Special Characters to JavaScript String
Suppose you want to type a word in
double-quotes in the string, for that you will write that text in the double quote
as below.
var name = "My"Blog"Tutorials";
But this method is not correct
according to JavaScript and in this way, if you add double quotes, you will get an error. Because the string is already in double-quotes so an error will be
generated. Similarly, if you try to add a backslash, the error will also appear.
JavaScript provides escape sequence
characters to add such type of characters. With the help of these characters, you
can create strings without generating an error.
In JavaScript and other languages,
escape sequence characters are defined with the backslash (\). With Escape
characters, you can add other special characters (@, $, etc) to strings too.
Common JavaScript escape characters
are being given below.
Escape Characters Description
' To
add a single quote inside String.
" To
add double quotes inside the string.
b To add
backspace inside the string.
r To
add carriage return inside String.
f To
add form feed inside String.
Common String Methods
JavaScript provides you many useful methods to work with the strings on the advance level. All methods are called by the dot operator (.) with the string object. It is important to note here that all strings start with index zero.
Some common JavaScript string methods are being given below.
Javascript Length Property
JavaScript provides you a length
property so that you can know the length of any string. You use this property
by using a dot (.) Operator with a string name. Its syntax is being given below.
stringName.length;
An example of this is being given
below.
<! - javascriptLengthProperty.html
->
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var MBH="My Blog Help";
// Printing length of string
document.write(MBH.length);
</script>
</body>
</html>
The above script generates the below output.
12
JavaScript string charAt ()
This method returns the available
character to the passed position. An example of this is being given below.
<! - charatMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var name="Abdussamad";
// Printing character at position 1
var result=name.charAt (1);
document.write(result);
</script>
</body>
</html>
The above script generates the given
output below.
b
JavaScript string Concat()
This method returns to combine two
strings. It is called on a single string and the second string is passed in it.
An example of this is being given below.
<! - concatMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var fname="Md"
var lname=" Abdussamad";
// concatenating first name and last name
var result=fname.concat(lname);
document.write(result);
</script>
</body>
</html>
The above script generates the given
output below.
Md Abdussamad
JavaScript string endsWith()
This method checks whether the string
ends with a passed character or string. An example of this is being given
below.
<! - endWithMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var MBH="My Blog Help";
// Checking what a string ends with
var result=MBH.endsWith("Help");
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
true
JavaScript string include ()
This method checks whether a passed string
or character is in a string or not. An example of this has been given below.
<! - includesMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var name="My Blog Help";
// Checking if a string contains a word
var result=name.includes("Blog");
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
true
JavaScript string indexOf ()
This method returns the position of
the passed string. An example of this is being given below.
<! - indexOfMethodExample.html ->
<!DOCTYPE html>
<html>
<script type ="text/javascript">
var mbh="My Blog Help";
// Getting started index of a word
var result=mbh.indexOf("Blog");
document.write(result);
</script>
</body>
</html>
The above script generates the given
output below.
3
JavaScript string search ()
This method searches for a passed value to a string. If the passed value is found, then this function returns its
position. An example of this is being given below.
<! - searchMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var str="My Blog Help";
// Searching
var result=str.search("Help");
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
9
JavaScript string replace()
This method replaces the passed
string is a string and places a string with the second argument and returns
the complete string. An example of this is being given below.
<! - replaceMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var mbh="My Tutorials Help";
// Replacing a word
var result=mbh.replace("Tutorials", "Blog");
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
My Blog Help
JavaScript string substr()
This method returns the substring which extracts from a string. You pass the starting index number of the substring you want to extract as the first index and in the second argument, you pass the number of characters that you want to extract after the starting index.
An example of this is being given
below.
<! - substrMethodExample.html ->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var bht="My Blog Help";
// Getting substring from a string
var result=bht.substr(3,4);
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
Blog
JavaScript string toLowerCase ()
This method converts all the
characters of a string into lowercase. An example of this is being given below.
<! - toLowerCaseMethodExample.html
->
<!DOCTYPE html>
<html>
<script type ="text/javascript">
var mbh="My Blog Help";
// Converting string to lowercase
var result=mbh.toLowerCase();
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
my blog help
JavaScript string toUpperCase ()
This method converts all the
characters of the string into uppercase. An example of this is being given below.
<! - toUpperCaseMethodExample.html
->
<!DOCTYPE html>
<html>
<script type="text/javascript">
var str="my blog help";
// Converting string to upper case
var result=str.toUpperCase();
document.write(result);
</script>
</body>
</html>
The above script generates the below output.
MY BLOG HELP
xz
ReplyDelete