(1) C#で文字列をタブやスペースで区切り配列に格納する方法
(1-1) 基本構文
(1-2) 基本構文サンプルプログラム
(1-3) 応用構文
(1-4) 応用構文サンプルプログラム
(1) C#で文字列をタブやスペースで区切り配列に格納する方法
(1-1) 基本構文
[文字列名].Split('[区切り文字]');
string[] [配列名] = [文字列].Split(',');
foreach(string [ループ変数名] in [配列名])
{
Console.WriteLine([ループ変数名]);
}
[文字列名].Split('[区切り文字①]','[区切り文字②]');
(1-2) サンプルプログラム
(構文①:サンプルプログラム)
logファイルを1行ずつ読み込んで、Splitで単語に区切っていくサンプルプログラムです。
(※プログラム中の「★」印の箇所が今回の構文に関連する記述になります。それ以外のファイル読み込みに関する部分は別記事(下記URL)で詳細にご紹介しているので、今回は割愛します)
https://rainbow-engine.com/csharp-read-file/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadFileSample
{
class Program
{
static void Main(string[] args)
{
string filepath = "C:\\Temp2\\IT0208\\Sample.log";
try
{
using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))
{
while (sr.EndOfStream == false)
{
string row = sr.ReadLine();
Console.WriteLine("###" + row);
//★Splitクラスを用いてカンマ「,」で文字列を区切る
string[] columns = row.Split(',');
//★foreachで配列の各要素を取り出し
foreach(string col in columns)
{
Console.WriteLine("-----------" + col);
}
}
}
}
catch (IOException e)
{
Console.WriteLine("Exception"+e.Message);
}
}
}
}
(図121)実行結果
カンマのみで区切っているため、パイプ部分は繋がったままです。

(構文②:サンプルプログラム)
こちらも先程と同じで、logファイルを1行ずつ読み込んでSplitで単語に区切っていきますが、1点異なるのは区切り文字を2つ与えて両方で区切るようにしている点です。
(※プログラム中の「★」印の箇所が今回の構文に関連する記述になります。それ以外のファイル読み込みに関する部分は別記事(下記URL)で詳細にご紹介しているので、今回は割愛します)
https://rainbow-engine.com/csharp-read-file/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadFileSample2
{
class Program2
{
static void Main(string[] args)
{
string filepath = "C:\\Temp2\\IT0208\\Sample.log";
try
{
using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))
{
while (sr.EndOfStream == false)
{
string row = sr.ReadLine();
Console.WriteLine("###" + row);
//★Splitクラスを用いてカンマ「,」とパイプ「|」で文字列を区切る
string[] columns = row.Split(',', '|');
//★foreachで配列の各要素を取り出し
foreach (string col in columns)
{
Console.WriteLine("-----------" + col);
}
}
}
}
catch (IOException e)
{
Console.WriteLine("Exception" + e.Message);
}
}
}
}
(図122)実行結果
2つ引数を与えてパイプ部分も区切れましたが、パイプが2つ連続していたため、そこで区切られて空白の単語が認識されてしまいました。これを解消するためには、次でご紹介する応用構文を使います。

(1-3) 応用構文
上記の方法で複数区切り文字を与えると、「区切り文字が2個連続してしまったケース」で不必要なスペースが生じてしまいます。それを防ぐために、区切り文字の引数をchar[]型の配列で与えます。
(応用構文)
char[] [配列名] = new char[] { ',' , '|' }
[文字列名].Split([配列名], StringSplitOptions.RemoveEmptyEntries);
(1-4) 応用構文サンプルプログラム
(応用構文:サンプルプログラム)
こちらも先程と同じ処理ですが、1点異なるのは区切り文字の引数をchar[]型で与えている点です。
(※プログラム中の「★」印の箇所が今回の構文に関連する記述になります。それ以外のファイル読み込みに関する部分は別記事(下記URL)で詳細にご紹介しているので、今回は割愛します)
https://rainbow-engine.com/csharp-read-file/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadFileSample3
{
class Program3
{
static void Main(string[] args)
{
string filepath = "C:\\Temp2\\IT0208\\Sample.log";
try
{
using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))
{
while (sr.EndOfStream == false)
{
string row = sr.ReadLine();
Console.WriteLine("###" + row);
//★Splitクラスを用いてカンマ「,」とパイプ「|」で文字列を区切る
char[] sep = new char[] { ',', '|' };
string[] columns = row.Split(sep, StringSplitOptions.RemoveEmptyEntries);
//★foreachで配列の各要素を取り出し
foreach (string col in columns)
{
Console.WriteLine("-----------" + col);
}
}
}
}
catch (IOException e)
{
Console.WriteLine("Exception" + e.Message);
}
}
}
}
(図141)実行結果
