Related post

Inheritance in Java programming with example | Zhullyblog

JavaScript strings: How to create and manipulate a string - Zhullyblog

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
JavaScript strings

Example 2




<html>
  <head>
    <title>String </title>
  </head>
  <body>
    <script>
     var sample="Hey guys, welcome to Zhullyblog";
     document.write(sample);
    </script>
  </body>
</html>



Screenshot
JavaScript strings example 2


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
Get the length of a string in JavaScript

Output
Calculate a string result in JavaScript

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
Find a string in another string JavaScript

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
Concatenate a string in JavaScript
Output

Concatenate string example









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