patterncsharpMinor
Splitting a path string
Viewed 0 times
pathstringsplitting
Problem
I have the following C# method to split a path string. The only thing I know is a fixed string in the path and I have to split the full path into two parts, first part should be one level below the fixed string and the rest should be the second part.
For example, if I have the following path:
then my firstPart would be
I want to improve this method in terms of memory and speed.
For example, if I have the following path:
string mainText = @"C:\Abc\Fixed\MyTemp\Japan\Tokyo";then my firstPart would be
"C:\Abc\Fixed\MyTemp" and second part "Japan\Tokyo"I want to improve this method in terms of memory and speed.
private static void SplitPath(string mainText, out string firstPart, out string secondPart)
{
firstPart = string.Empty;
secondPart = string.Empty;
if (!string.IsNullOrEmpty(mainText))
{
string strConstatnt = "Fixed";
List splitted = mainText.Split(new char[] { '\\' }).ToList();
int indexToFixed = splitted.IndexOf(strConstatnt);
StringBuilder sbFirst = new StringBuilder();
StringBuilder sbSecond = new StringBuilder();
if (indexToFixed >= 0)
{
for (int i = 0; i 0)
{
firstPart = sbFirst.Remove(sbFirst.Length - 1, 1).ToString();
}
if (sbSecond.Length > 0)
{
secondPart = sbSecond.Remove(sbSecond.Length - 1, 1).ToString();
}
}
}Solution
I took the liberty to make the exercise as well to see how our approaches differ. I don't think there are many remarks in terms of performance although
All together I think my code provides a more easily readable solution which should help in terms of human-reading efficiency. In the end I pretty much abstracted the things you do under library features.
I have tested this code with inputs
If you'd want to take your approach after all, a few remarks:
StringBuilder.Remove() is a red flag in that area.All together I think my code provides a more easily readable solution which should help in terms of human-reading efficiency. In the end I pretty much abstracted the things you do under library features.
public void SplitPath(string mainText, out string firstPart, out string secondPart)
{
const string pivot = "Fixed";
const char delimiter = '\\';
var entries = mainText.Split(delimiter);
int pivotIndex = entries.Length;
for (var i = 0; i < entries.Length; i++)
{
if (entries[i] == pivot)
{
// Take index + 2 because linq.Take/Skip is 1-based
// Optionally take index + 1 to avoid out-of-bounds when the pivot is the last element
pivotIndex = (i == entries.Length) ? i + 1 : i + 2;
break;
}
}
firstPart = string.Join(delimiter.ToString(), entries.Take(pivotIndex));
secondPart = string.Join(delimiter.ToString(), entries.Skip(pivotIndex));
}I have tested this code with inputs
C:\Abc\Fixed\MyTemp\Japan\Tokyo, C:\Abc\Fixed and C:\Abc.If you'd want to take your approach after all, a few remarks:
- Try to early return by using a negated
if-statement condition.
- Put your delimiter in a variable
- Avoid magic values
- Avoid removing data from the
StringBuilder
Code Snippets
public void SplitPath(string mainText, out string firstPart, out string secondPart)
{
const string pivot = "Fixed";
const char delimiter = '\\';
var entries = mainText.Split(delimiter);
int pivotIndex = entries.Length;
for (var i = 0; i < entries.Length; i++)
{
if (entries[i] == pivot)
{
// Take index + 2 because linq.Take/Skip is 1-based
// Optionally take index + 1 to avoid out-of-bounds when the pivot is the last element
pivotIndex = (i == entries.Length) ? i + 1 : i + 2;
break;
}
}
firstPart = string.Join(delimiter.ToString(), entries.Take(pivotIndex));
secondPart = string.Join(delimiter.ToString(), entries.Skip(pivotIndex));
}Context
StackExchange Code Review Q#61608, answer score: 5
Revisions (0)
No revisions yet.