Created
July 12, 2018 07:17
-
-
Save iezekiel/edfdc56b8f8b5a356152de88ad41eec3 to your computer and use it in GitHub Desktop.
Flatten Array C#
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
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