String in Java Script

String : 

>Anything written in quotes is String.

>You can use single or double quotes

>String is a primitive data type

Define String :

1st way - 

let myName = "vinod thapa"

2nd way - 

let ytName  = new String("vinod thapa")

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

length:

let myName = "vinod thapa";

console.log(myName.length);

Escape Character :

let myName = "vinod \"elli\" thapa";

console.log(myName);


Find a string in a string :

indexOf() and lastIndexOf()  - if matched , return index position , else return -1

let myName = "vintodthapa";
console.log(myName.indexOf("t"));
console.log(myName.indexOf("t",2));

console.log(myName.lastIndexOf("t"));


Searching for a string in a string :

search() - 

>if matched , return index position , else return -1

let myName = "vintodthapa";
console.log(myName.search("t"));

>cannot take second start position

let myName = "vintodthapa";

console.log(myName.indexOf("t",5));
console.log(myName.search("t",5)); //cannot take second start position


Extraction String parts :

slice() - 

>extract a part of a string and return the extracted part in a new string

>It takes 2 parameter (start position , end position(optional))

>Select starting element but doesn't add the last element

>Orignal string will not be muted

let myName = "vintodthapa";
console.log(myName.slice(0,4));
console.log(myName.slice(4));
console.log(myName.slice(4,-2));


substring() - 

>similar to slice

>cannot use negative indexes

let myName = "vintodthapa";
console.log(myName.substring(0,4));
console.log(myName.substring(4));
console.log(myName.substring(4,-2));;


substr() :

let myName = "vintodthapa";
console.log(myName.substring(4,2));

console.log(myName.substr(4,2));

replace() :

> does not change the old string

> it only replaces the first match


let myName = "vinod thapa vinod";
console.log(myName.replace("vinod",'Vinod'))


Extracting string character :

charAt() :

>returns the character of the specified index number 

let myName = "vainod thapa vinod";
console.log(myName.charAt(1))

charCodeAt():

always returns an UTF-16 integer i.e (0 to 65535)

UTF - unique number is provide to every character

let myName = "vainod thapa vinod";
console.log(myName.charAt(1))
console.log(myName.charCodeAt(1))


Q)  Find unicode of last character ?

let myName = "vainod thapa vinod";

console.log(myName.charCodeAt(myName.length -1))


Property Access :

let myName = "vainod thapa vinod";
console.log(myName[1]);


Other useful methods : 

toUpperCase() , toLowerCase() , concat() :

let myName1 = "himanshu";
let myName2= "Himanshu";
console.log(myName1.toUpperCase());
console.log(myName2.toLowerCase());
console.log(myName1.concat(myName2));
console.log(myName1.concat(" ",myName2)); //for space

 trim() :

//remove white space from start and end
let myName1 = "     himanshu        ";
console.log(myName1.trim());

split() :

convert string to array : 

let myName1 = "hi,ma,ns,hu";
console.log(myName1.split(","));



 



0 Comments