文字列(関数、プロパティ、テンプレートリテラル)についてのすべて!


おそらく文字列について聞いたことがあるでしょう.文字列は文字列の配列です.例えば、"This is string"または'This is string'.
それはプログラミングで学ぶ非常に重要なトピックです.Wはどこでも文字通り文字列を使用しています.そのため、多くのプログラミング言語で文字列関数のトンがあります.

さて、問題は文字列関数ですか?
文字列関数はすべてのプログラミング言語で定義済みの関数に過ぎません.ああ!!独自のカスタム関数を作成する場合、これらの関数を使用する必要はありません.

それぞれの機能に応じて異なる文字列関数をグループ化したlinkです.
//String properties and Functions

//special trick for special characters




//let text = "This is an "important" line to remember"; this will give error
let text = "This is an \"important\" line to remember";
console.log(text);

let text1 = "This is \\ line to remember";
console.log(text1);

//Function for strings
const name = "Himanshu Pal  ";
const greeting = "Greetings";
const phrase = "A quick brown fox jumps over the lazy dog";
console.log(greeting + ' ' + name);
console.log(greeting.concat(' ', name));//also use this function to concatinate
console.log(name.toLowerCase()); //change all character to uppercase
console.log(name.toUpperCase()); //change all character to owercase
console.log(phrase.length); //return the total length of the string within '' or ""
console.log(name.indexOf('a')); // return the index number of the character
console.log(phrase.lastIndexOf('dog'));
console.log(phrase.charCodeAt(5)); //return unicode index value of that particular character
console.log(phrase.endsWith('g')); //check last letter or word of string 
console.log(String.fromCharCode(65)); //convert unicode digit to character
console.log(phrase.includes('fox'));
console.log(phrase.localeCompare(name)); //return -1 if first variable character appears before the second variable character--> ex: ab compare cd return -1
                                        //return 1 if first variable character appears after the second variable character--> ef compare cd return 1
                                       //return 0 if first variable character appears equally the second variable character ab compare ab return 0

console.log(phrase.match(/ox/g)); //match regular expression within a string
console.log(name.repeat(2)); //repeat the string given number of times
console.log(phrase.replace("fox", "Ox"));// replace given string with desired string
console.log(phrase.search('fox'));
console.log(phrase.slice(0,8));//extract a part of string within givin index value
console.log(phrase.split(" ")); //convert string into array of string
console.log(phrase.startsWith('A')); 
console.log(phrase.substring(2,7)); //select the substring from a sting  Output => quick
//The main diffrenct between substring and substr is 
//substring() pick value of the first given index and and end before n-1. means if we given index 2-7 it will treverse 2-6
//substr(0 pick value fron first given index value to last till n. Means grom 2-7)
console.log(phrase.substr(2,7)); //Output => quick b
console.log(phrase.toString());//return value of string Object
console.log(phrase.trim()); //remove whitespace from both ends of the string
console.log(phrase.valueOf()); //return primitve value of string object



console.log("Concept of \" = \" , \" == \" and \"===\" in String");
//Ways we can use string
let var1 = "100"; //litral value passed in primitive string
let var2 = 100; //another example of litreal passed to primitve string
let var3 = "100";

let varobj = new String("100"); // we defined an object type string with "new" keyword

//How they impact 
console.log(var1==var2); //RETURN TRUE regardless of datatype
console.log(var1==varobj); // RETURN TRUE even ignoring the object type
console.log(var1===varobj);//RETURN FALSE  strictly checking both value nd datatype
console.log(var3===var1);// RETURN TRUE BOTH VALUE AND DATA TYPE MATCHING
//Diffrence between "=" , "==" and "==="

//Properties of String
console.log(phrase.constructor);
console.log(phrase.length);

//Protoype  allow toadd methods and properties in  an object
function employee(name, job, tittle)
{
    this.name = name;
    this.job = job; 
    this.tittle = tittle;
}

employee.prototype.salary = 2000;

const fred = new employee('Alex', 'IT', 'Analyst', 4000);

console.log(fred);
console.log(fred.salary);


let html;

html = "<h1> this is heading</h1>"+
       "<p> this is my para</p>"; //using  "+" will be complicated for long html scripts

    //use template lirtals to avoid "+" and optimize code

html = html.concat('this');
console.log(html);
console.log(html.includes('is'));
console.log(html.split(' '));
console.log(html.split('>'));


// Starting with template littrals
let namee = 'Himanshu';
let fruit1 = 'Orangr';
let fruit2 = 'Apple';
let myHtml = `Hello ${namee}
              <h1> This is heading </h1>
              <p> You like ${fruit1} and ${fruit2}
             `; //using backtick button just upper key of tab left of 1 key

document.body.innerHTML = myHtml;
上記のコードを介しても、物事の理解を与えるコメントを読んでください.また、私はほとんどの機能の定義を与えている.このコードを実行して出力を見ることができます.

テンプレートリテラルは何ですか?

私たちが尋ねるべきであるこの質問の前に、テンプレートリテラルは2つの理由のために存在します.Why template literals?連結冗長性と2番目のスクリプトで変数を使用できる.しかし最初に重要な点について知っています.数字1の左のキーのすぐ上のキー.これらのbackticksはより効率的です.マルチライン文字列に対して""あるいは"を使用できないので、"\&"と"""'を使うのはちょっと複雑です.backticksこれらの異常を削除します.First:はJSでHTMLをタイプするために使用されます.これらを使用することによって、我々は直接JSでHTMLを書くことができて、異なる目的のためにビルトインJS Funtionを使うことができます.