ES6-新しい文字列メソッド
以下は、メソッドとその説明のリストです。
| シニア番号 | 方法と説明 | 
|---|---|
| 1 |  String.prototype.startsWith(searchString、position = 0)  受信者がsearchStringで始まる場合はtrueを返します。位置を使用すると、チェックする文字列の開始位置を指定できます。  |  
      
| 2 |  String.prototype.endsWith(searchString、endPosition = searchString.length)  受信者がsearchStringで始まる場合はtrueを返します。位置を使用すると、チェックする文字列の開始位置を指定できます。  |  
      
| 3 |  String.prototype.includes(searchString、position = 0)  レシーバーにsearchStringが含まれている場合はtrueを返します。positionを使用すると、検索する文字列の開始位置を指定できます。  |  
      
| 4 |  String.prototype.repeat(count)  受信者を連結したカウント回数を返します。  |  
      
テンプレートリテラル
Template literals 埋め込み式を許可する文字列リテラルです。 Templatestrings一重引用符または二重引用符ではなく、バックティック( ``)を使用します。したがって、テンプレート文字列は次のように記述できます。
var greeting = `Hello World!`; 
    文字列補間とテンプレートリテラル
示されているように、テンプレート文字列は、$ {}構文を使用した文字列置換にプレースホルダーを使用できます。
Example 1
var name = "Brendan"; 
console.log('Hello, ${name}!'); 
    上記のコードが正常に実行されると、次の出力が表示されます。
Hello, Brendan! 
    Example 2: Template literals and expressions
var a = 10; 
var b = 10; 
console.log(`The sum of ${a} and ${b} is  ${a+b} `); 
    上記のコードが正常に実行されると、次の出力が表示されます。
The sum of 10 and 10 is 20 
    Example 3: Template literals and function expression
function fn() { return "Hello World"; } 
console.log(`Message: ${fn()} !!`); 
    上記のコードが正常に実行されると、次の出力が表示されます。
Message: Hello World !! 
    複数行の文字列とテンプレートリテラル
テンプレート文字列には複数の行を含めることができます。
Example
var multiLine = `
   This is 
   a string 
   with multiple 
   lines`; 
console.log(multiLine) 
    上記のコードが正常に実行されると、次の出力が表示されます。
This is 
a string 
with multiple 
line 
    String.raw()
ES6には、バックスラッシュに特別な意味がない生の文字列用のタグ関数String.rawが含まれています。 String.raw正規表現リテラルの場合と同じようにバックスラッシュを記述できます。次の例を考えてみましょう。
var text =`Hello \n World` 
console.log(text)  
var raw_text = String.raw`Hello \n World ` 
console.log(raw_text) 
    上記のコードが正常に実行されると、次の出力が表示されます。
Hello 
World 
Hello \n World 
    タグ付きテンプレート
A tagテンプレートリテラルを解釈して処理できる関数です。テンプレートリテラルの前にタグが表示されます。構文を以下に示します。
構文
let output_fromTag = tagFunction `Template literal with ${variable1} , ${variable2}` 
    タグ関数の実装構文は次のとおりです。
function tagFunction(literals,...variable_values){
   //process
   return "some result"
} 
    例
次の例では、タグ関数を定義しています myTagFn()。渡されたパラメータが表示されます。それを表示した後、Done 発信者に。
<script>
   function myTagFn(literals,...values){
      console.log("literal values are");
      for(let c of literals){
         console.log(c)
      }
      console.log("variable values are ");
      for(let c of values){
         console.log(c)
      }
      return "Done"
   }
   let company = `TutorialsPoint`
   let company_location = `Mumbai`
   let result = myTagFn `Hello this is ${company} from ${company_location}`
   console.log(result)
</script> 
    上記のコードの出力は以下のようになります-
//literal
literal values are
Hello this is
from
//values
variable values are
TutorialsPoint
Mumbai
Done 
    例
以下 tag function かかります template literal 以下に示すように大文字に変換します-
<script>
   function convertToUpperTagFn(literals, ...values) {
      let result = "";
      for (let i = 0; i < literals.length; i++) {
         result += literals[i];
         if (i < values.length) {
            result += values[i];
         }
      }
      return result.toUpperCase();
   }
   let company = `TutorialsPoint`
   let company_location = `Mumbai`
   let result = convertToUpperTagFn `Hello this is ${company} from ${company_location}`
   console.log(result)
</script> 
    上記のコードの出力は以下のようになります-
HELLO THIS IS TUTORIALSPOINT FROM MUMBAI 
    String.fromCodePoint()
静的文字列。fromCodePoint()メソッドは、指定されたUnicodeコードポイントのシーケンスを使用して作成された文字列を返します。無効なコードポイントが渡されると、関数はRangeErrorをスローします。
console.log(String.fromCodePoint(42))        
console.log(String.fromCodePoint(65, 90)) 
    上記のコードが正常に実行されると、次の出力が表示されます。
* 
AZ