I am programming a language interpreter in c# recently and i have created a set of functions that receive either a string or a string[] and splits it by a received string.
For example: with a string Input-
"Hey:123:hello:456"
":"
Will return this array
{"hey","123","hello","456"}
And with a string[] Input-
{"a:b","c","d:e:f"}
":"
Will return this array
{"a","b","c","d","e","f"}
So this is my code
public string[] SplitRows(object thevar, string delimiter) {
if (delimiter == "<newline>") delimiter = "\n";
if (thevar.GetType() == typeof(string)) {
string temp = (string) thevar;
return temp.Split(new string[] {
delimiter
}, System.StringSplitOptions.None);
}
if (thevar.GetType() == typeof(string[])) {
return stringarraysplitter((string[]) thevar, delimiter);
}
return null;
}
public string[] stringarraysplitter(string[] arr, string delimiter) {
string[][] tempr = new string[arr.Length][];
for (int i = 0; i < arr.Length; i++) {
tempr[i] = arr[i].Split(new string[] {
delimiter
}, System.StringSplitOptions.None);
}
System.Collections.Generic.List < string > templist = new System.Collections.Generic.List < string > ();
for (int i = 0; i < tempr.Length; i++) {
for (int j = 0; j < tempr[i].Length; j++) {
templist.Add(tempr[i][j]);
}
}
return templist.ToArray();
}
How can i improve efficiency and maybe make it tidier.
Side note:SplitRows() is the only main function allowed to receive either a string array or string,even tho i use 2 functions,i can not create 2 for the 2 variable types.
Edit:
I'm sorry for <newline>
its a replacement string for \n in my language. Can be ignored