How can this piece of code be shorten to only one line of code?
string[] result1 = content.Split('|');
string result2 = result1[1].Remove(0, 4);
Seriously sorry, but I'm at a loss right now.
If you need to complete array back with first item modified.
string[] result = content.Split('|').Select((r, i) =>
new { Value = i == 0 ? r.Remove(0, 4) : r, Index = i })
.Select(r => r.Value)
.ToArray();
var result instead of string result and I assumed that you needed a modified array. That is why I posted it. But consider Jon Skeet comment, having it on a single line doesn't improve readabilitya different answer try it
public static class MyExtensions
{
public static string SplitRemove(this String str,int arrayIndex,int charToRemove)
{
return str.Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries) [arrayIndex].Remove(0,charToRemove);
}
}
string result2 = content.Split('|')[1].Remove(0, 4);
string[] result1 = content.Split('|'); string result2 = ""; if(result1.Length > 1) result2 = result1[1].Remove(0, 4);Works always. The summarised version you are asking for crashes if|is not present incontent. Reducing the length of the code can be good in some contexts, but not always.