我需要做的是比较两个字符串,并将差异与开始/结束标记进行更改标记。例子:

this is string number one.
this string is string number two.

输出将是

this [is|string is] string number [one|two].  

我一直在努力解决这个问题。我发现我有些事情会帮助我做到这一点,但是我无法实现这一目标。
http://www.angusj.com/delphi/textdiff.html

我在这里工作了约80%,但是我不知道如何使它准确地做我想做的事情。任何帮助,将不胜感激。


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Diff, StdCtrls;

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    Diff: TDiff;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  s1,s2:string;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  i: Integer;
  lastKind: TChangeKind;

  procedure AddCharToStr(var s: string; c: char; kind, lastkind: TChangeKind);
  begin
    if (kind  lastkind) AND (lastkind = ckNone) and (kind  ckNone) then s:=s+'[';
    if (kind  lastkind) AND (lastkind  ckNone) and (kind = ckNone) then s:=s+']';
    case kind of         
      ckNone: s := s  + c;
      ckAdd: s := s + c;         
      ckDelete: s := s  + c;
      ckModify: s := s + '|' + c;
    end;
  end;

begin
  Diff.Execute(pchar(edit1.text), pchar(edit2.text), length(edit1.text), length(edit2.text));

  //now, display the diffs ...
  lastKind := ckNone;
  s1 := ''; s2 := '';
  form2.caption:= inttostr(diff.Count);
  for i := 0 to Diff.count-1 do
  begin

    with Diff.Compares[i] do
    begin
      //show changes to first string (with spaces for adds to align with second string)
      if Kind = ckAdd then 
      begin 
        AddCharToStr(s1,' ',Kind, lastKind); 
      end
      else 
      AddCharToStr(s1,chr1,Kind,lastKind);
      if Kind = ckDelete then 
      begin 
        AddCharToStr(s2,' ',Kind, lastKind)
      end
      else AddCharToStr(s2,chr2,Kind,lastKind);

      lastKind := Kind;
    end;
  end;
    memo1.Lines.Add(s1);
    memo1.Lines.Add(s2);
end;

end.

我从Angusj.com拿了BasicDemo1,并对其进行了修改以实现这一目标。

有帮助吗?

解决方案

在我的答案中,我将专注于活动我已经实施过,我不会涵盖如何实施自定义活动。您可以在 http://msdn.microsoft.com/en-us/library/jj193517(v= azure.10).aspx

这是我相信你正在寻找的解决方案:

[Designer("System.Activities.Core.Presentation.DoWhileDesigner, System.Activities.Core.Presentation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "System.ComponentModel.Design.IDesigner")]
public sealed class DelegateActivity : NativeActivity
{
    [DependsOn("Condition"), DefaultValue((string)null)]
    public Activity Body
    {
        get;
        set;
    }

    protected override void Execute(NativeActivityContext context)
    {
        context.ScheduleActivity(Body);
    }
}
.

如果这就是您需要的话,请在 Execute 方法中添加预执行和后期执行逻辑。

现在,一个免责声明......我在我的实施中使用了 Designer 属性中的值,因为我没有实现自己的 Designer 。您可以通过从 conctiondedesigner 继承并实现 iComponentControl 来实现。您必须为设计师提供自己的XAML。

只会在Visual Studio中删除删除反应性中的任何活动,并且工作流程将执行它。此实现无视条件,因此您可以将其留空:

其他提示

有一个 物体pascal diff引擎 可以提供帮助。您可能需要将每个“单词”分解为单独的行进行比较,或者修改算法以按单词为基础进行比较。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top