LoginSignup
3
4

More than 3 years have passed since last update.

GASで特定の文字以降の文字列を削除する方法

Posted at

gasで特定の文字以降が不要になったので、削除してしまいました。

const a = "abcdefghijk";
const b = "de";
const c = a.slice(0,a.indexOf(b))

sliceというメソッドを用いて、deの文字が見つかった場合にはそれ以降の文字は必要ないので削除します。
結果、"abc"を取得することができました。

文字列の、0から indexOfで合致した文字列の番号まで取得し削除する。という方法ですね。

これを使えば反対に、"de"より前の文字だけ削除することも可能かと思いますね。
gasは結構使うので、pythonとかで書かしてくれないかなぁ〜。

参考:
文字列削除する方法
https://teratail.com/questions/200062
indexof
https://tonari-it.com/gas-array-indexof-lastindexof/
slice
https://blog.8basetech.com/google-apps-script/gas-substring/

3
4
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
3
4