Created
January 29, 2021 07:16
-
-
Save sapphire-al2o3/722369bc14efe80f89269f980a2da814 to your computer and use it in GitHub Desktop.
カンマ区切りの数字をパースして配列を返す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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