JavaScript strings
A string is a sequence of character, letters and numbers or the combination of all. In JavaScript, a string can be enclosed in a single quote
(') or double quotes
(").
Here is an example
<html>
<head>
<title>String </title>
</head>
<body>
<script>
var sample="Hey guys, welcome to Zhullyblog";
</script>
</body>
</html>
Screenshot
Example 2
<html>
<head>
<title>String </title>
</head>
<body>
<script>
var sample="Hey guys, welcome to Zhullyblog";
document.write(sample);
</script>
</body>
</html>
Screenshot
Manipulating strings
Get the Length of a string
The length of the string specify the number of characters such as \n , letters or number of a string.
Here is an example
<html>
<head>
<title>Count the number of string</title>
</head>
<body>
<p>The number of string is :</p>
<script>
var sample = "This is a example of a string.";
document.write(sample.length);
</script>
</body>
</html>
Screenshot
Output
Finding a string inside another String
To find a string inside another string , you use the
indexOf ( ) method.
<html>
<head>
<title>Find a string in another</title>
</head>
<body>
<script>
var x = "Hello world, how is life. Do you find JavaScript interesting";
var y = x.indexOf("JavaScript");
alert(y);
</script>
</body>
</html>
Screenshot
Concatenate two strings
Just like any other programming language, JavaScript gives room for string concatenation. This means that you can join two or more strings together using the
"+" operator and
"+=" operator.
Let's take an example:
<html>
<head>
<title>Find a string in another</title>
</head>
<body>
<script>
var x = "Hello geeks,";
var y ="welcome to Zhullyblog";
var z = x + y ;
document.write(z);
</script>
</body>
</html>
Screenshot
Output
Please share
JavaScript Syntax
Where do you place JavaScript in HTML document
JavaScript Variables
JavaScript If, If else, If else if statement
JavaScript Function: How to define and call a function
How to generate output in JS
JavaScript Switch case
Comments
Post a Comment