0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

40 代おっさん GASのNumber,Stringオブジェクトについて学ぶ

Posted at

本記事ついて

本記事は プログラミング初学者の私が学習していく中でわからない単語や概要をなるべくわかりやすい様にまとめたものです。
もし誤りなどありましたらコメントにてお知らせいただけるとありがたいです。

Numberオブジェクト

Numberオブジェクトは、数値を取り扱うラッパーオブジェクト

分類 メンバー 戻り値 説明
メソッド toExponential(n) String 数値を小数点桁数nの指数表記文字列に変更する
メソッド toFixed(n) String 数値を小数点桁数nの少数点表記文字列に変更する
メソッド toPrecision(n) String 指定した行数nの文字列に変換する
メソッド toString(n) String 数値をn進数表記の文字列に変換する
静的メソッド Number.isFinite(num) Boolean 数値numが有限値であるかを判定する
静的メソッド Number.isInteger(num) Boolean 数値numが整数値であるかを判定する
静的メソッド Number.isNan(num) Boolean 数値numがNaNであるかを判定する
静的プロパティ Number.MAX_VALUE 表現可能な正の数の最大値
静的プロパティ Number.MIN_VALUE 表現可能な0より大きい最小値
静的プロパティ Number.NaN 数値でないを表す特別な値
静的プロパティ Number.POSITIVE_INFINITY 正の無限大を表す特別な値
静的プロパティ Number.NEGATIVE_INFINITY 負表すの無限大を特別な値

お試し

function object() {
  const x = 1000 / 3;

  console.log(x.toString());
  console.log(x.toExponential(4));
  console.log(x.toFixed(4));
  console.log(x.toFixed());
  console.log(x.toPrecision(4));
  console.log(x.toPrecision(2));

  console.log(Number.isFinite(x));
  console.log(Number.isInteger(x));
  console.log(Number.isNaN(x));

  console.log(Number.MAX_VALUE);
  console.log(Number.MIN_VALUE);
  console.log(Number.NaN);
  console.log(Number.POSITIVE_INFINITY);
  console.log(Number.NEGATIVE_INFINITY);
}

Stringオブジェクト

Stringオブジェクトは、文字列を取り扱うラッパーオブジェクト

分類 メンバー 戻り値 説明
メソッド charAt(n) String インデックスnの文字を返す
メソッド charCodeAt(n) Integer インデックスnの文字のUnicodeの値を返す
メソッド toLowerCase() String 小文字に変換した文字列を返す
メソッド toUpperCase() String 大文字に変換した文字列を返す
メソッド slice(start[,end]) String 文字列のインデックスstartからendの手前までの文字列を抽出する
メソッド substring(start[,length]) String 文字列のインデックスstartから長さのlengthまでの文字列を抽出する
メソッド split(str) String[] 文字列をstrで分割し配列をして返す
メソッド startsWith(str[,start]) Boolean 文字列のインデックスstart以降が文字列Strではじまっているかを判定する
メソッド endsWith(str[,length]) Boolean 文字列の長さlengthの部分文字列が文字列Strで終わるかどうかを判定する
メソッド concat(str) String 文字列の末尾に文字列strを連結したものを返す
メソッド includs(str[,start]) Boolean 文字列のインデックスstart以降に文字列strが含まれているかを判定する
メソッド indexOf(str[,start]) Integer 文字列をインデックスstartから後方に向かって検索し部分文字列strとマッチしたインデックスを返す
メソッド lastindexOf(str[,start]) Integer 文字列をインデックスstartから前方に向かって検索し部分文字列strとマッチしたインデックスを返す
メソッド padStart((length[,str]) String 文字列を長さlengthになるまで文字列strで先頭方向に延長したものを返す
メソッド padEnd((length[,str]) String 文字列を長さlengthになるまで文字列strで末尾方向に延長したものを返す
メソッド localeCompare(str) lnteger 参照文字列が、並べ替え順においてstrの前であれば1、後ろであれば‐1、同一であれば0を返す
メソッド repeat(num) Strig 文字列をnum回繰り返した文字列を返す
メソッド trim() String 文字列の前後の空白を削除したものを返す
メソッド match(reg) Array 文字列を正規表現regで検索してマッチした文字列を含む配列を返す
メソッド search(reg) Integer 文字列を正規表現regで検索してマッチしたインデックスを返すと文字列Strを置換したものを返す
プロパティ length 文字列の長さを表す

お試し

function object() {
  let str = "利樹は、かわいいです。";

  console.log(str.length);
  console.log(str.charAt(0));
  console.log(str.charCodeAt(0));
  
  console.log(str.toLowerCase());
  console.log(str.toUpperCase());

  console.log(str.slice(3,7));
  console.log(str.substring(3,7));

  console.log(str.split(''));  
  console.log(str.startsWith('利樹は'));
  console.log(str.startsWith('かわいいです。', 4));
  console.log(str.endsWith('かわいいです。'));
  console.log(str.endsWith('利樹は', 3));

  str = str.concat(" 利樹君はみかんが大好きです。")
  console.log(str);

  console.log(str.indexOf('かわいいです。'));
  console.log(str.lastIndexOf('かわいいです。'));
  console.log(str.includes('利樹は'));

  console.log('1234'.padStart(7, '0'));
  console.log('1234'.padEnd(7, '0'));

  console.log('かわいいです'.localeCompare('あいう'));
  console.log('かわいいです'.localeCompare('かわいいです'));
  console.log('かわいいです'.localeCompare(''));

  console.log('利樹'.repeat(5));
  console.log('     利樹      '.trim());
}

参考資料

https://www.amazon.co.jp/s?k=google+apps+script+%E5%AE%8C%E5%85%A8%E5%85%A5%E9%96%80&adgrpid=110264232688&gclid=CjwKCAiA9aKQBhBREiwAyGP5lSl7AJJLCvOEHb4wQgMlyqW1fll5X8GDTT_Rkd1_soUAyIPMXQr26hoClHEQAvD_BwE&hvadid=553833563682&hvdev=c&hvlocphy=1009076&hvnetw=g&hvqmt=b&hvrand=4378489642044417389&hvtargid=kwd-594191211348&hydadcr=4106_13159878&jp-ad-ap=0&tag=googhydr-22&ref=pd_sl_2x1owglv0s_b_p52

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?