JavaScript : Strings

 


----: Strings In JavaScript :----


1. Escape Character.
2. Finding a string in a String.
3. Searching for a String in a String.
4. Extracting String Parts.
5. Replace String Content.
6. Extracting String Characters.
7. Other useful methods.

A JavaScript string is zero or more characters written inside quotes.
JavaScript string are used for storing and manipulatiing text.
You can use single or double quotes.
Strings can be created as primitives,
from string literals,or as objects, using the String() constructer


let myName = "Himanshu Tiwari";
let myChannelName = "Himanshu Tiwari";
console.log(myName);


How to find length of a string.

String.prototype.length
Reflects the length of the string

let myName = "Himanshu";
console.log(myName.length); 


🌟Escape Character :-

let anySentence = "We are so-called \"Vikings\" from the north";
console.log(anySentence);

If you don't want to mess, simply use the alternate quotes


let anySentence2 = `We are so-called "Vikings" from the north`;
console.log(anySentence2);


🌟Finding a string in a String :-

String.prototype.indexOf(searchValue[, fromIndex])

he indexOf() method returns the index of (the postion of) the first
occurence of a specified text in a string.

const aboutMe = "Hey myself himanshu kumar tiwari ";
console.log(aboutMe.indexOf("h",13));

Js counts position from zero.
0 is the first position in a string, is is the second .........

*******

String.prototype.lastIndexOf(searchValue[, fromIndex])
Returns the index whithin the calling string object of the
last occurence of searchValue, or -1 if not found.

const aboutMe = "Hey myself himanshu kumar tiwari ";
console.log(aboutMe.lastIndexOf("r"));


🌟Searching for a String in a String :-

String.prototype.search(regexp)

The search() method search a string for a specified
value and return the postion of the match.

const aboutMe = "I am himanshu kumar tiwari";
1.
let searchData = aboutMe.search("kumar");
console.log(searchData);
2.
console.log(aboutMe.search("kumar"));

The search() method cannot take a second start position argument.


🌟Extracting String Parts :-

There are 3 methods for extracting a part of a string.

1. slice(start,end)
2. substring(start,end)
3. substr(start,length)

1. The slice() method 
slice() extrats a part of a string and returns the extracted part
in a new string.

The method takes 2 parameters : the start postiton,
and the end position(end not included).

var str = 'Apple , Banana , Kiwi';
let res = str.slice(7,-4);
console.log(res);

The slice() method selects the element starting at the given
start argument, and ends at, but does not include the given 
end argument.

⚠ Note :- The original array will not be changed.
Remember :- javaScript counts positions from zero. First position is 0

Challenge Time :-

Q. Display only 2 characters of a string like the one 
used in twitter ?

let myTweet = "lorem ipsum"

let myActualTweet = myTweet.slice(0,2);
console.log(myActualTweet);

2. The substring() Method
substring() is simiar to slice()
The difference is that substring() can't accept
negative indexes.

var str = "Apple,Banana,Kiwi";
let res = str.substring(0,4);
console.log(res);

3. The substr() method
substr() is similar to slice().

The difference is that the second parameter specifies the 
length of the extracted part.

var str = 'apple,banana,kiwi';
let res = str.substr(-4);
console.log(res);

🌟 Replace String Content:-

String.prototype.replace(searchForreplaceWith)

The replace() method replaces a specific value with
another value in a string.

let myBioData = "Myself Himanshu tiwari";
let res = myBioData.replace("himanshu","Aditya");
console.log(res);


Points to remember :-
1. The replace() method doesn't change the string
   it is called on. It returns a new string.
2. By default, the replace() method replace only 
   the first match.
3. By default, replace() method is case sensitive.
   writing HIMANSHU (with upper-case) will not work.


🌟 Extracting String Characters :-

There are three methods for extracting string characters.

➡ charAt(position)
➡ charCodeAt(position)
➡ property access [ ]

➡ ➡ The charAt() method
The charAt() method returns the character at a 
specified index (positoin) in a string.

let str = 'HELLO WORLD';
let res = str.charAt(6);
console.log(res);

➡ ➡ The charCodeAt() method
The charCodeAt() method returns the unicode of the 
character at a specified index in a string:
The method return a UTF-16 code
(an integer between 0 and 65535)

The Unicode Standard provides a unique number for every
character, no matter the platform, device, application,
or language. UTF-8 is a popular Unicode encoding which 
has 88-bit code units. 

let str = "HELLO WORLD";
let res = str.charCodeAt(0);
console.log(res);


Challenge Time :-
Return the unicode of the last character in a string

let str = "HELLO WORLD";
let lastChar = str.charCodeAt(10);
console.log(lastChar);

➡ ➡ Property Access
ECMAScript 5 (2009) allows property access [ ] on strings

var str = "Hello world";
console.log(str[0]);


🌟 Other useful Methods :-

UpperCase / LowerCase 

let myName = "Himanshu Tiwari";
console.log(myName.toUpperCase());
console.log(myName.toLowerCase());


CONCAT 

The concat() method
concat() joins two or more strings.

let fName = "Himanshu"
let lName = "Tiwari"

console.log(fName + lName);
console.log(fName.concat(lName));
console.log(fName.concat(" ",lName));


TRIM 

String.trim()
The trim() method removes whitespace from both sides 
of a string

var str = "    Hello world";
console.log(str.trim());


CONVERTING STRING INTO AN ARRAY

A string can be coverted to an array with the 
split() method.

var txt = 'a,b,c,d,e';        // string
console.log(txt.split(","));  // split on commas
console.log(txt.split(" "));  // split on spaces
console.log(txt.split("|"));  // split on pipes