× back

Strings

What we will learn ↓

            
let myName = "vinod Thapa";
let myChannelName = 'Vinode Thapa';

//let ytName = new String("Thapa Techinical");
let ytName = 'Thapa Technical';
console.log(myName);
console.log(ytName);
            
        

String.prototype.length

How to find the length of a string.

                
let myName = "Vinod Thapa";
console.log(myName = length); // 11
                
            

Escape Character

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

Finding a String in a String

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

                
const myBioData = 'I am the Thapa Technical';
console.log(myBioData.indexOf("t", 6));
                
            

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

  • Returns the index within the calling string object of the last occurence of searchValue, or -1 if not found.
                    
const myBioData = 'I am the Thapa Technical';
console.log(myBioData.lastIndexOf("t", 6)); // starts searching from 6th index
                    
                

Searching for a String in a String

String.prototype.search(regexp)

                
const myBioData = 'I am the Thapa Technical';
let sData = myBioData.search("Technical");
console.log(sData); // 15
                
            

Extracting String Parts

The slice( ) method

  • slice( ) extracts a parrt of a string and returns the extracted part in a new string.
  • The method takes 2 parameters: the start position, and the end position (end not included).
                    
let str = "Apple, Banana, Kiwi, mango";
// let res = str.slice(0, 4); // "Appl"
let res = str.slice(7); // "Banana, Kiwi, mango"
// starts from 7th index to last.
                    
                
  • The slice( ) method selects the elements 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.

Challenge Time ⌣

Display only 280 characters of a string like the one used in Twitter.

                    
let myTweets = "Lorem Impsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? ";
let myActualTweet = myTweets.slice(0, 280); // Lorem Impsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five cen
console.log(myActualTweet);
console.log(myActualTweet.length); // 280
                    
                

The substring( ) method

  • substring( ) is similar to slice( ).
  • The difference is that substring( ) cannot accept negative indexes.
                    
let str = "Apple, Banana, Kiwi";
let res = str.substring(8, -2); 
console.log(res); // Apple, B
                    
                
  • If we give negative value then the characters are counted from the 0th position.

The substr( ) method

  • It is similar to slice( ).
  • The difference is that the second parameter specifies the length of the extracted part.
                    
let str = "Apple, Banana, Kiwi";
// let res = str.substr(0, 4); // "Appl"
// let res = str.substr(7, -2); // ""
let res = str.substr(-4); // "Kiwi" // extracting data from back
                    
                

Replacing String Content

String.prototype.replace(searchFor, replaceWith)

                
let myBioData = 'I am Vinod Bahadur Thapa vinod';
let replacedString = myBioData.replace('vinod', 'Vinod');
console.log(replacedString); // I am Vinod Bahadur Thapa Vinod
console.log(myBioData); // I am Vinod Bahadur Thapa vinod
                
            

Points to remember ↓

  1. The replace() method does not change the string it is called on, it returns a new string.
  2. By default, the replace() method replaces only the first match.
  3. By default, the replace() method is case sensitive. Writing "VINOD" (with upper case) will not work.

Extracting String Characters

The charAt( ) method

  • This method returns the character at a specified index (position) in a string
                    
let str = "HELLO WORLD";
console.log(str.charAt(9)); // L
                    
                

The charCodeAt( ) Method

  • The charCodeAt() method returns the unicode of the character at a specified index in a string.
  • This method returns a UTF-16 code (an interger 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 bode units.
                    
let str = "HELLO WORLD";
console.log(str.charCodeAt(0));
                    
                

Challenge Time ⌣

Return the Unicode of the last character in a string.

                    
let str = "HELLO WORLD";
console.log(str.charCodeAt(str.length - 1));
                    
                

Property Access

  • ECMAScript 5 (2009) allows property access [ ] of strings.
                    
let str = "HELLO WORLD";
console.log(str[1]); // E
                    
                

Other useful methos

                
let myName = "vinod thapa";
console.log(myName.toUpperCase());
myName = "VINOD THAPA";
console.log(myName.toLowerCase());
                
            

The concat() method

                
let fName = "Vinod";
let lName = "Thapa";
console.log(fName + lName);
console.log(`${fName} ${lName}`);
console.log(fName.concat(lName));
console.log(fName.concat(" ", lName));
                
            

String.trim()

                
                    let str = "         Hello       WORLD       ";
                    console.log(str.trim()); // "Hello      World"
                
            

Converting a String to an Array

                
let txt = "a, b,c d,e"; // string
console.log(txt.split(",")); // Split on commas // [ "a", " b", "c d", "e" ]
console.log(txt.split(" ")); // Split on spaces // [ "a,", "b,c", "d,e" ]
console.log(txt.split("|")); // Split on pipe // [ "a, b,c d,e" ]