Categories
ECMAScript Javascript

Strings

/**
 * to demonstrate String capabilities
 */

//to create String using constructor new String(StringLiteral)

let employeeName  = new String("Sam Josea");
console.log("employeeName : "+employeeName);

//length property 
console.log("employeeName.length : " + employeeName.length);

//charAt(index) method
console.log("charAt 2 : "+employeeName.charAt(2));

//charCodeAt(index) method
console.log("charCodeAt 2 : "+employeeName.charCodeAt(2));

//concat string.concat(string2, string3[, ..., stringN]);
let nameAndCity = employeeName.concat(" Of"," Goa"," India");
console.log("employeeName : " + employeeName);
console.log("nameAndCity : "+nameAndCity);

//indexOf(searchValue,[fromIndex]) to return the index of first occurance characters in string
console.log("index of 'a' : " + employeeName.indexOf("a"));
console.log("index of 'a' from index 2 : " + employeeName.indexOf("a",2));

//lastIndexOf(searchValue,[fromIndex]) to return index of last occurance character in string
console.log("last index of 'a' : "+employeeName.lastIndexOf("a"));

//string1.localeCompare(string2) to compare two Strings string1, string2
console.log("local compare :: " + employeeName.localeCompare("Tam Joseas"));

//string1.replace(regexp/substring, substring) it is used to replace substring which matches passed argument regular expression
var regExpr = /London/g;
var message = "london is historical city, London is modern city";
var newMessage = message.replace(regExpr,"Tokyo");
console.log(newMessage);

//string1.search(regepx/substring)
//returns starting index of substring matching passed regular expression. if not found returns -1
console.log("index of London(case sensitive):"+message.search(regExpr));
console.log("index of London(case insensitive):"+message.search(/London/gi));

//string.slice(startIndex[, endIndex]) to extract substring in given startindex to endIndex(exclusive)
console.log("slice from 0 to 10 message :" + message.slice(0,21));

//string.split(seperatorChar[,noOfTokens]) to split by seperator Character, limit tokens to noOfTokens, returns array
console.log("split by space message : " + message.split(" ",4));

//string.substr(startIndex [,length]) to split by length
console.log("substring of message :" +message.substr(27,6));

//string1.substring(startIndex[,endIndex]) to return substring between startIndex and endIndex. endIndex is exclusive.
console.log("substring of message :" + message.substring(27,34));
console.log("substring of message : " + message.substring(27));

//string.toLocaleLowerCase() to return entire string to lower case.
console.log("message in lower case : " + message.toLocaleLowerCase());

//string.toLocaleUpperCase() to return entire string to upper case.
console.log("message in upper case : " + message.toLocaleUpperCase());

//string.toUpperCase() to convert string to upper case
console.log("message toUpperCase : " + message.toUpperCase());

//string.toString() to return the string representation
console.log("message toString : "+ message.toString());




Output

Leave a comment

Design a site like this with WordPress.com
Get started