カンマ区切りデータを配列に入れる

Key Difinitions

プログラミング別:カンマ区切りデータを配列に入れるやり方

1. PHP

explode関数を利用

「’日付’,’科目’,’出金’,’入金’,’残高’」⇒ arr(‘日付’,’科目’,’出金’,’入金’,’残高’)

<?php
// カンマ区切りの文字列
$str_csv = "'日付','科目','出金','入金','残高'";

// カンマ区切りのデータを配列にする
$arr = explode(",", $str_csv);

// 変換された配列を表示
print_r($arr);
?>

PHPで配列のデータをカンマ区切りデータに変換するには、implode関数を使う。

$arr = array('日付','科目','出金','入金','残高');
$str_csv = implode(',', $arr);
echo $str_csv;

2. Python

区切り文字で分割: split()

str_csv = '1,2,3,4,5,6'
arr = str_csv.split(',')
print(arr)
# ['1', '2', '3', '4', '5', '6']

Pythonで配列のデータをカンマ区切りデータに変換するには、join()を利用する。

s = ["A", "B", "C", "D","E"]
"->".join(s)
'A->B->C->D->E'

3. javascript

「split」で引数に「,」を指定すれば、カンマ区切りのデータを配列にできます。

// カンマ区切りデータ
let str = "111,222,333";

// 配列に変換
let arr = str.split(',');
console.log(arr);

4. VBA

「split」で引数に「,」を指定すれば、カンマ区切りのデータを配列にできます。

Sub Sample1()
    Dim tmp As Variant
    tmp = Split("123,備品費,10000", ",")
    MsgBox tmp(0)
    MsgBox tmp(1)
    MsgBox tmp(2)
End Sub

コメント

タイトルとURLをコピーしました