0
votes

Hi I'm building a program that will look at files at have been placed into SVN and show what files have been changed in each commit.

As i'm only wanting to show the file path. if the path is that same I only want to show the difference.

example:

First file path is:

/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/DataModifier.cs

Second file path is:

/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/Tax Marker Ripper v1.csproj

What I'd like to do is substring at the point of difference.

So in this case:

/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/

would be substringed

3

3 Answers

1
votes

I hope this helps:

     public string GetString(string Path1, string Path2)
    {

        //Split and Put everything between / in the arrays
        string[] Arr_String1 = Path1.Split('/');
        string[] Arr_String2 = Path2.Split('/');

        string Result = "";

        for (int i = 0; i <= Arr_String1.Length; i++)
        {
            if (Arr_String1[i] == Arr_String2[i])
            {
              //Puts the Content that is the same in an Result string with /
                Result += Arr_String1[i] + '/';
                }

            else
                break;
        }
        // If Path is identical he would add a / which we dont want 
        if (Result.Contains('.'))
        {
            Result = Result.TrimEnd('/');
        }

        return Result;
    }
1
votes

You can do this pretty easily with a loop. Basically:

 public String FindCommonStart(string a, string b)
    {
        int length = Math.Min(a.Length, b.Length);

        var common = String.Empty;
        for (int i = 0; i < length; i++)
        {

            if (a[i] == b[i])
            {
                common += a[i];
            }
            else
            {
                 break;
            }
        }

        return common;
    }
0
votes

Something like this:

public string GetCommonStart(string a, string b)
{
  if ((a == null) || (b == null))
    throw new ArgumentNullException();

  int Delim = 0;
  int I = 0;
  while ((I < a.Length) && (I < b.Length) && (a[I] == b[I]))
  {
    if (a[I++] == Path.AltDirectorySeparatorChar) // or Path.DirectorySeparatorChar
      Delim = I;
  }

  return a.Substring(0, Delim);
}

Keep in mind, that this code is case-sensitive (and paths in windows in general are not).