「Google Apps Script」sliceとtoUpperCaseメソッドで最初の文字を大文字にして他の文字を小文字にする

構文
1.文字列.slice(0,1).toUpperCase()
sliceの引数が2つの場合は、1つめの引数は開始位置で2つめの引数は終了位置です。
先頭の文字のみ大文字にします。

2.文字列.slice(1).toLowerCase()
sliceの引数が1つの場合は、指定した位置の文字から最後の文字まで取得します。
後ろ2つの文字から最後の文字を小文字にします。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function myFunction() {
const strA = "study SKILL";
console.log(strA.slice(0,1).toUpperCase() + strA.slice(1).toLowerCase());
const strB = "arkgAME";
console.log(strB.slice(0,1).toUpperCase() + strB.slice(1).toLowerCase());
}
function myFunction() { const strA = "study SKILL"; console.log(strA.slice(0,1).toUpperCase() + strA.slice(1).toLowerCase()); const strB = "arkgAME"; console.log(strB.slice(0,1).toUpperCase() + strB.slice(1).toLowerCase()); }
function myFunction() {
  const strA = "study SKILL";
  console.log(strA.slice(0,1).toUpperCase() + strA.slice(1).toLowerCase()); 

  const strB = "arkgAME";
  console.log(strB.slice(0,1).toUpperCase() + strB.slice(1).toLowerCase()); 
}

実行結果
Study skill
Arkgame

Google Apps Script

Posted by arkgame