Wednesday, December 21, 2016

Javascript-9-datatype String

 String

A string variable can be defined with " ' " or  " "" " as below:

  • var carname = "Volvo XC60";(recommended)
  • var y = new String("John");(not recommended as it takes more processing time in comparison)



A string variable size can be measure as:

  • var carname = "Volvo XC60";(recommended)
  • carname.length;(not recommended as it takes more processing time in comparison)



==  and  === operators on strings in below scenarios:
  • var x = "John";             
  • var y = new String("John");
  • var z = new String("John");
(x==y) //returns true
(x===y) //returns false (since string and object)
(y==z) //returns false
(y===z) //returns false




\ escape character.
\can be used prior to special characters within a string.
  • var x = 'It\'s alright';
  • var y = "We are the so-called \"Vikings\" from the north."


---------------------------------------


Breaking a long code line

1)the best place to break it is after an operator: 
  • document.getElementById("demo").innerHTML =
    "Hello Dolly.";
2)within a text string with a single backslash"\" 
  • document.getElementById("demo").innerHTML ="Hello \
    Dolly.";
  • document.getElementById("demo").innerHTML = \ 
    "Hello Dolly!";
    // WRONG
3)the best way is, use string addition
  • document.getElementById("demo").innerHTML = "Hello" + 
    "Dolly!";


---------------------------------------

Finding a String in a String


Let's say,
var str = "Please locate where 'locate' occurs!";

  •  indexOf() : returns the first index of the first occurrence of the string
    • var pos = str.indexOf("locate")
    • //returns 7
  • lastIndexOf() method returns the index of the last occurrence
    • var pos = str.lastIndexOf("locate");
    • //returns 21

---------------------------------------

Searching a String in a String


Let's say,
var str = "Please locate where 'locate' occurs!";

  •  search() method searches a string for a specified value and returns the position of the match
    • var pos = str.search("locate"); //returns 7. 
    • similar to indexOf()

---------------------------------------

Extracting String Parts


Let's say,
var str = "Apple, Banana, Kiwi";


There are 3 methods for extracting a part of a string:
  • slice(start index, end index): slice() extracts a part of a string and returns the extracted part in a new string.
    • var res = str.slice(713); //returns Banana
    • var res = str.slice(-12, -6);//the position is counted from backward index. returns Banana
    • var res = str.slice(7); //returns Banana, Kiwi
    • var res = str.slice(-12); //returns Banana, Kiwi
  • substring(start index, end): similar to slice(). does not accept -ve index
    • var res = str.substring(713); //returns Banana
  • substr(start index, length): substr() is similar to slice(), but second parameter is the length instead of end-index. Accepts -ve index.
    • var res = str.substr(7, 9);//returns Banana K


Extracting String Characters

Let's say,
var str = "Hello World";
There are 2 safe methods for extracting string characters:
  • charAt(position): 
    • str.charAt(0); // returns H
  • charCodeAt(position). returns the unicode of the character
    • str.charCodeAt(0);// returns 72
---------------------------------------

Replacing String Content(replace(str2replace,replacedWith))

Lets say,
str = "Please Microsoft visit Microsoft!";

  • var n = str.replace("Microsoft""W3Schools");//replace all "Microsoft" occurence
  • var n = str.replace(/Microsoft/g"W3Schools");//replace only the first Microsoft occurence
To replace all matches, use a regular expression with a g flag (for global match):

---------------------------------------

Converting to Upper and Lower Case

Lets say,
var text1 = "Hello World!";       // String

  • var text2 = text1.toUpperCase(); //returns HELLO WORLD
  • var text2 = text1.toLowerCase(); //returns hello world


---------------------------------------

Converting a String to an Array

Lets say, var txt = "a,b,c,d,e";  
  • txt.split(",");          // Split on commas and returns array
  • txt.split(" ");          // Split on spaces and returns array
  • txt.split("|");          // Split on pipe and returns array
  • However,  txt.split(""); // Split in characters. i.e array of characters
---------------------------------------

Other methods:
  • concat() joins two or more strings: 
    • var text1 = "Hello";
      var text2 = "World";
      text3 = text1.concat(" ", text2); //Returns 
      Hello World!
    • var text = "Hello" + " " + "World!";
      var text = "Hello".concat(" ""World!");
      //Returns Hello World!
  • str.match(regex) : returns the string based on regex
    • var text1 = "Hello World new world";
      text3 = text1.match(/world/); //Returns 
      World!
  • str.repeat(NumberOfTimesToRepeat'str') : 
    • str.repeat(3);//Returns Hello World! Hello World! Hello World!

______________________________________________________

String.prototype.replace()



___________________________________________________

      No comments:

      Post a Comment