Skip to content

Instantly share code, notes, and snippets.

@sapphire-al2o3
Created January 29, 2021 07:16
Show Gist options
  • Save sapphire-al2o3/722369bc14efe80f89269f980a2da814 to your computer and use it in GitHub Desktop.
Save sapphire-al2o3/722369bc14efe80f89269f980a2da814 to your computer and use it in GitHub Desktop.
カンマ区切りの数字をパースして配列を返す
public static int[] ParseCSV(string t)
{
if (t.IndexOf(',') == -1)
{
return new int[] { int.Parse(t) };
}
int count = 0;
for (int i = 0; i < t.Length; i++)
{
if (t[i] == ',')
{
count++;
}
}
int[] v = new int[count + 1];
int n = 0;
int k = 0;
for (int i = 0; i < t.Length; i++)
{
if (t[i] == ',')
{
v[k] = n;
k++;
n = 0;
}
else if (t[i] >= '0' && t[i] <= '9')
{
n = n * 10 + t[i] - '0';
}
}
v[k] = n;
return v;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment