Help us understand the problem. What is going on with this article?

【ColdFusion】文字列を区切り文字で分解してひとつだけ使いたい

More than 1 year has passed since last update.

知らなかったのでメモ。今まで回りくどいことをしていた…。

例えば123-456-789をハイフン区切りで3番目789だけ取得したいなんてとき。配列に変換して目的の文字列を取得していましたよ。

<CFSET sTest = "123-456-789">
<CFSET aTest = listToArray(sTest, "-")>
<CFDUMP var="#aTest[3]#">

もしくは、以下のように1行にしたり。でもなんかかっこ悪いですね。

<CFSET sTest = "123-456-789">
<CFDUMP var="#listToArray(sTest, "-")[3]#">

しかし!getToken関数を使えば一発でn番目の文字列だけ取得できます。

<CFSET sTest = "123-456-789">
<CFDUMP var="#getToken(sTest, 3, "-")#">

しかも配列方式だと存在しない位置の文字列を取得しようとした場合にエラーになるのでArrayLenで長さチェックが必須なんですが、getToken関数なら[empty string]が返るだけなので便利です。

ただし、分解後の文字列を全て使いたいという場合には普通にlistToArrayで配列にしといたほうが便利ですね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした