zaagan
7/1/2019 - 2:55 PM

JavaScript Strings

JavaScript Strings

var me = "I am Batman";

var len = me.length; // Length of the string
// Output : 11 

me.indexOf('Bat'); // First occurance on text,
// Output : 5        -1 if not found

me.lastIndexOf("a"); // Last occurance of text
// Output : 9

me.slice(5,11); // Extract a section of the text
// Output : Batman

me.replace("Batman", "Robin"); // Find and replace 
// Output : I am Robin            specifed text

me.toUpperCase(); // Convert to upper case
// Output : I AM BATMAN

me.toLowerCase(); // Convert to lower case
// Output : i am batman

// Join two or more strings into one
me.concat(", I like ","apples"); 
// Output : I am Batman, I like apples

me.charAt(5); // Character at the index
// Output : B

me.charCodeAt(5); // Unicode of character
// Output : 66       at position

me.search('am'); // Search for string and 
// Output : 2       return its position

me[3];   // unsafe, abc[2] = "C" doesn't work

me.split(','); // Splits into an array of strings by commas
// Output : ["I am Batman"]

me.split(''); // Split by character
// Output : [ "I", " ", "a", "m", " ", "B", "a", "t", … ]

me.substr(5);  // extracts a substring depending 
// Output : Batman  on a specified number of characters

me.substring(5);  // Similar, can’t accept negative indices
// Output : Batman   

var value = 12345
value.toString(); // Convert the number to string

var letsBreakThis ="You can't \n break me";
// Output :"You can't 
//          break me"