I wrote a extension method for String
in a PCL Project.:
public static ICollection<string[]> SplitAt(this string input, char target, int length, StringSplitOptions opts, bool trin = false()
{
string[] itens = input.Split(new char[] { target }, opts);
return InternalSplitAt(itens, target, length, trim);
}
private static ICollection<string[]> InternalSplitAt(string[] itens, char target, int length, bool trim = false)
{
var collectionToReturn = new List<string[]>();
var targetString = target.ToString();
do
{
string firstPart = string.Join(targetString, itens.Take(length));
collectionToReturn.Add(firstPart.Split(target));
if (trim)
{
itens = itens.Skip(length).Select(x => x.Trim()).ToArray();
}
else
{
itens = itens.Skip(length).ToArray();
}
}
while (itens.Length >= length);
return collectionToReturn;
}
Then I use the SplitAt
method like this:
var arr = str.SplitAt('#', 34, StringSplitOption.None);
str
is a String
with 280474 characters.
When I call the above code in a Xamarin.Android App, it takes almost 40 seconds to complete, and in a Console Application, 1 second.
Can my code be improved in any to make it run faster on Android?
Note: That extension method is based on a code I got some time ago from another StackOverflow question I think, but I could not find it again to give proper credit.
Edit:
Explaining what I'm trying to acomplish: I have a String like that:
var str = "001#Test#002#Test#003#Test";
Doing a normal Split
by #, I would get na array this:
string[] { "001", "Test", "002", "Test", "003", "Test" }
But I need it to be three different arrays, so calling that extension like this:
var arr = str.SplitAt('#', 2, StringSplitOption.None);
I got:
string[] { "001", "Test" }
string[] { "002", "Test" }
string[] { "003", "Test" }
In my real scenario, the 280474 string have 53074 of the # character, and since I'm calling the extension with 34 as the length parameter, my final output would be a ICollection with 1561 itens (53074 / 34), each being a string[34]
.