检查差异

本文关键字:检查 | 更新日期: 2023-09-27 18:13:06

嗨,我正在构建一个程序,该程序将查看已放置到SVN中的文件,并显示每次提交时已更改的文件

因为我只想显示文件路径。如果路径相同,我只想显示差异

的例子:

第一个文件路径是:

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

第二个文件路径为:

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

我想做的是在差点处的子字符串

在这个例子中:

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

将是子字符串

检查差异

希望对您有所帮助:

     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;
    }

使用循环可以很容易地做到这一点。基本上:

 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;
    }

像这样:

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);
}

请记住,这段代码是区分大小写的(而windows中的路径通常不是)。