activities
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

工作流活动

上次更新日期 2026年3月26日

比较文本

使用多个比较选项(包括以下比较类型)验证两个文本是否相等:

  • 比较字词。
  • 比较行。
  • 将字符作为char变量进行比较。

CompareText(string, string, ComparisonType, ComparisonOptions)

CompareText(
    string baselineText,
    string targetText,
    [ComparisonType comparisonType],
    [ComparisonOptions opts]
)
CompareText(
    string baselineText,
    string targetText,
    [ComparisonType comparisonType],
    [ComparisonOptions opts]
)

baselineText String :用作比较参考的基本文本。

targetText String :与基本文本进行比较的文本。

comparisonType ComparisonType (可选):比较文本时使用的比较类型。可选择以下选项:

  • ComparisonType.Char :比较文本中的每个字符char
  • ComparisonType.Line :比较文本中的每一行。
  • ComparisonType.Word :比较文本中的每个单词。

opts “比较文本” :应用于此操作的特定比较选项。要创建类型为CompareDocumentsOptions的对象,请使用TestingOptions.CompareText 类

返回值

ComparisonResult

比较操作的结果,存储在ComparisonResult变量中。 您可以调用 的AreEquivalentDifferences ComparisonResult属性以显示它们是否相同,如果不同,则显示差异。

示例

查看以下示例,以了解如何使用CompareText编码自动化 API:

1. 按字符比较文本:

在此示例中,我们将比较两个string变量,即initialTextmodifiedText 。 比较在字符级别完成。 与WithIgnoreWildcardRuleWithIgnoreRegexRule方法一起提供的通配符规则和正则表达式规则用于忽略比较中的特定模式。 例如,通配符规则R?d和正则表达式规则(f|F)ox允许我们忽略变量中存在的某些词语,例如RedFox 。 在这种情况下,我们将usePlaceholder参数设置为false ,因为我们不想为被忽略的模式使用占位符名称。

WithGenerateHtml方法输出 HTML 报告,突出显示差异。

var initialText = "Red fox jumps over the lazy fox";
var modifiedText = "Blue fox leaps over the lazy dog";

var charCompareResult = testing.CompareText(
    initialText, modifiedText, 
    ComparisonType.Character, 
    TestingOptions.CompareText()
        .WithGenerateHtml(@".\HtmlCompareResults\CharacterTextCompare.html")
        .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
        .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", false)
);

// Log any differences
foreach (var diff in charCompareResult.Differences)
{
    Log(diff.Operation.ToString());
    Console.WriteLine(diff.Text);
    Console.WriteLine("------------------------------------------------------------------------------");
}
var initialText = "Red fox jumps over the lazy fox";
var modifiedText = "Blue fox leaps over the lazy dog";

var charCompareResult = testing.CompareText(
    initialText, modifiedText, 
    ComparisonType.Character, 
    TestingOptions.CompareText()
        .WithGenerateHtml(@".\HtmlCompareResults\CharacterTextCompare.html")
        .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
        .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", false)
);

// Log any differences
foreach (var diff in charCompareResult.Differences)
{
    Log(diff.Operation.ToString());
    Console.WriteLine(diff.Text);
    Console.WriteLine("------------------------------------------------------------------------------");
}

2. 按行比较文本:

在此示例中,我们将比较两个string变量,即initialTextmodifiedText 。 比较是在行级别完成的。 与WithIgnoreWildcardRuleWithIgnoreRegexRule方法一起提供的通配符规则和正则表达式规则用于忽略比较中的特定模式。 例如,通配符规则R?d和正则表达式规则(f|F)ox允许我们忽略变量中存在的某些词语,例如RedFox 。 在这种情况下,将usePlaceholder参数设置为true ,因为我们希望为忽略的模式使用占位符名称。

WithGenerateHtml方法输出 HTML 报告,突出显示差异。

var initialText = @"Red fox jumps over the lazy fox";
var modifiedText = @"Blue fox leaps over the lazy dog";

var lineCompareResult = testing.CompareText(
    initialText, modifiedText, 
    ComparisonType.Line, 
    TestingOptions.CompareText()
        .WithGenerateHtml(@".\HtmlCompareResults\LineTextCompare.html")
        .WithIgnoreWildcardRule("WildcardRule", "R?d", true)
        .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
);

// Log any differences
foreach (var diff in lineCompareResult.Differences)
{
    Log(diff.Operation.ToString());
    Console.WriteLine(diff.Text);
    Console.WriteLine("------------------------------------------------------------------------------");
}
var initialText = @"Red fox jumps over the lazy fox";
var modifiedText = @"Blue fox leaps over the lazy dog";

var lineCompareResult = testing.CompareText(
    initialText, modifiedText, 
    ComparisonType.Line, 
    TestingOptions.CompareText()
        .WithGenerateHtml(@".\HtmlCompareResults\LineTextCompare.html")
        .WithIgnoreWildcardRule("WildcardRule", "R?d", true)
        .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
);

// Log any differences
foreach (var diff in lineCompareResult.Differences)
{
    Log(diff.Operation.ToString());
    Console.WriteLine(diff.Text);
    Console.WriteLine("------------------------------------------------------------------------------");
}

3. 按单词比较文本

在此示例中,与上一个示例类似,我们将比较两个string变量: initialTextmodifiedText 。 比较在词级别完成。 与WithIgnoreWildcardRuleWithIgnoreRegexRule方法一起提供的通配符规则和正则表达式规则用于忽略比较中的特定模式。 例如,通配符规则R?d和正则表达式规则(f|F)ox允许我们忽略变量中存在的某些词语,例如RedFox

WithGenerateHtml方法输出 HTML 报告,突出显示差异。

var initialText = "Red fox jumps over the lazy fox";
var modifiedText = "Blue fox leaps over the lazy dog";

var wordCompareResult = testing.ComapareText(
    initialText, modifiedText, 
    ComparisonType.Word, 
    TestingOptions.CompareText()
        .WithGenerateHtml(@".\HtmlCompareResults\LineWordCompare.html")
        .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
        .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
);

// Log any differences
foreach (var diff in wordCompareResult.Differences)
{
    Log(diff.Operation.ToString());
    Console.WriteLine(diff.Text);
    Console.WriteLine("------------------------------------------------------------------------------");
}
var initialText = "Red fox jumps over the lazy fox";
var modifiedText = "Blue fox leaps over the lazy dog";

var wordCompareResult = testing.ComapareText(
    initialText, modifiedText, 
    ComparisonType.Word, 
    TestingOptions.CompareText()
        .WithGenerateHtml(@".\HtmlCompareResults\LineWordCompare.html")
        .WithIgnoreWildcardRule("WildcardRule", "R?d", false)
        .WithIgnoreRegexRule("RegexRule", @"(f|F)ox", true)
);

// Log any differences
foreach (var diff in wordCompareResult.Differences)
{
    Log(diff.Operation.ToString());
    Console.WriteLine(diff.Text);
    Console.WriteLine("------------------------------------------------------------------------------");
}

CompareText返回的结果包含一个Differences属性,您可以使用该属性来打印出文本之间的差异。

  • CompareText(string, string, ComparisonType, ComparisonOptions)
  • 返回值
  • 示例

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新