Tuesday, February 18, 2020

Strings in JavaScript

Strings In JavaScript


String


The string is one of the data types in JavaScript described as a sequence of characters. A string can be used in JavaScript within a single('text') or double quotes ("text").


Declaring strings in JavaScript


var str1='Welcome to JavaScript'
var str2="Welcome to javascript"

We can also create the strings using String Constructors like String('text') or almost anything that can be converted into a string using String(something).

str=new String('JavaScript');


Difference between directly declared string literals and String literals created using Constructor

<script>
  str1="Hello World!!!";
  str2=new String("Hello World");
  document.write(typeof str1);
  document.write('<br>');
  document.write(typeof str2);
  </script>

Output:
string
object


The length of the string variable can be virtually infinite. For managing large string literals we can put the literal in multiple lines and concatenate them using concatenation (+) operator.


var long_message="HTML, CSS and JavaScript"+
                                   "are the main  technologies for front end development."+
                                    "Web page's UI is controlled and managed by HTML and CSS";



OR (\) backslash


var long_message="HTML, CSS and JavaScript\
                                   "are the main technologies for front end development\
                                    "Web page's UI is controlled and managed by HTML and CSS";

(+)  and  (+=) is the most commonly used operator on strings.


Length of string literal


We can calculate the length of string literal using the length property.

length='web technologies'.length;


Escape characters in JavaScript


Special characters can be represented using the escape characters, like


Escape characters
escape coderesult
\'single-quote
\"double-quote
\\backslash
\nnew line
\rcarriage return
\vvertical tab
\ttab
\bbackspace
\fform feed


for example the output of 

document.write('\'JavaScript\'is awesome?');

is 'JavaScript'is awesome?

indexOf() method

The indexOf() method is used to find the location of a substring starting from position 0 in a given string.

<script>
  var str='Johny Johny yes Papa<br> Eating sugar no Papa';
    document.write(str.indexOf('sugar'));
</script> 

Output:
32

We can also find the substring count from the last index of string using the lastIndexOf() method.

<script>
    var str='Johny Johny yes Papa<br> Eating sugar no Papa';
    document.write(str.lastIndexOf('Papa'));
</script>    

Output
41


charAt() and charCodeAt() method



The charAt(index) method can be used to get the character at any given index. However, characters can also be accessed by index directly like array elements. The charCodeAt() will return 
UTF-16 value for the given index.



<script>
  var str='JavaScript';
    document.write(str.charAt(5));
    document.write('<br>');
    document.write(str[4]);
    document.write('<br>');
    document.write(str.charCodeAt(6));
</script>     

Output:
c
S
114


Comparing strings in JavaScript


We can compare string literal using ==,<,> operators.

<script>
    var str1='JavaScript';
    str2='JavaScript';
    if(str1==str2)
    {
      document.write('true')
    }
    else
    {
      document.write('false');
    }
</script>

Output:
true


for comparing using ignoring the cases.

<script>
  var str1='JavaScript';
    str2='javascript';
    if(str1.toUpperCase()==str2.toUpperCase())
    {//Convert to uppercase and check with == operator
      document.write('true')
    }
    else
    {
      document.write('false');
    }
</script>    

Output:
true

The replace() method

For replacing the substring from a string we can use replace() method.

<script>
  str1="Hello World!!!";
  str2=str1.replace("World","people");
  document.write(str2);
  </script>    

Output:
Hello people!!!