Skip to content

Instantly share code, notes, and snippets.

@iezekiel
Created July 12, 2018 07:17
Show Gist options
  • Save iezekiel/edfdc56b8f8b5a356152de88ad41eec3 to your computer and use it in GitHub Desktop.
Save iezekiel/edfdc56b8f8b5a356152de88ad41eec3 to your computer and use it in GitHub Desktop.
Flatten Array C#
static string Flatten(string array)
{
string result; //return string
try
{
bool previousWasNumber = false; //chech for numbers greater than 9
result = "["; //starting the array
foreach (char c in array.ToCharArray())
{
int check = (int)c; //check character
if (!(check == 91 || check == 44 || check == 93)) // check if not [ , ]
{
result += c; // add the int to new array
previousWasNumber = true;
}
else
{
if (previousWasNumber) result += ",";
previousWasNumber = false;
}
}
result = result.Remove(result.Length - 1); //Remove last comma ,
result += "]"; // end the array
return result;
}
catch (Exception)
{
return "Not well formated array"; //Can include a lot of checks
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment