# CompareText

> Verifies if two texts are equivalent, using multiple comparison options, including the following comparison types:

Verifies if two texts are equivalent, using multiple comparison options, including the following comparison types:

* Comparing words.
* Comparing lines.
* Comparing characters as `char` variables.

## `CompareText(string, string, ComparisonType, ComparisonOptions)`

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

`baselineText` [String](https://learn.microsoft.com/en-us/dotnet/api/system.string?view=net-8.0) : The base text that is used as a reference for comparison.

`targetText` [String](https://learn.microsoft.com/en-us/dotnet/api/system.string?view=net-8.0) : The text that is compared against the base text.

`comparisonType` `ComparisonType` (Optional) : The type of comparison to use when comparing texts. The following options are available:

   * `ComparisonType.Char`: Compares every character `char` in the texts.
   * `ComparisonType.Line`: Compares every line in the texts.
   * `ComparisonType.Word`: Compares every word in the texts.

`opts` [CompareText](https://docs.uipath.com/activities/other/latest/workflow/uipath-testing-activities-api-models-testingoptions-comparedocuments#testingoptionscomparedocuments-class) : The specific comparison options that should be used for this operation. To create an object of type `CompareDocumentsOptions`, use the [TestingOptions.CompareText Class](https://docs.uipath.com/activities/other/latest/workflow/uipath-testing-activities-api-models-testingoptions-comparetext#testingoptionscomparetext-class).

## Return value

`ComparisonResult`

The result of the comparison action, stored within a `ComparisonResult` variable. You can call the `AreEquivalent` or `Differences` properties on the `ComparisonResult` to display if they are equivalent, and if not, to display the differences.

## Examples

Check the following examples for using the `CompareText` coded automation API:

**1. Comparing text by character**:

In this example, we are comparing two `string` variables, `initialText` and `modifiedText`. The comparison is done at the character level. The wildcard rule and regex rule provided along with `WithIgnoreWildcardRule` and `WithIgnoreRegexRule` methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule `R?d` and the regex rule `(f|F)ox` allow us to ignore certain words that are present in the variables, such as `Red`, and `Fox`. In this scenario, the `usePlaceholder` parameter was set to `false`, because we do not want to have a placeholder name for the ignored patterns.

The `WithGenerateHtml` method outputs an HTML report highlighting the differences.

   ```
   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. Comparing text by line**:

In this example, we are comparing two `string` variables, `initialText` and `modifiedText`. The comparison is done at the line level. The wildcard rule and regex rule provided along with `WithIgnoreWildcardRule` and `WithIgnoreRegexRule` methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule `R?d` and the regex rule `(f|F)ox` allow us to ignore certain words that are present in the variables, such as `Red`, and `Fox`. In this scenario, the `usePlaceholder` parameter was set to `true`, because we want to have a placeholder name for the ignored patterns.

The `WithGenerateHtml` method outputs an HTML report highlighting the differences.

   ```
   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. Comparing text by word**:

In this example, similar to the previous one, we are comparing two `string` variables: `initialText` and `modifiedText`. The comparison is done at the word level. The wildcard rule and regex rule provided along with `WithIgnoreWildcardRule` and `WithIgnoreRegexRule` methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule `R?d` and the regex rule `(f|F)ox` allow us to ignore certain words that are present in the variables, such as `Red`, and `Fox`.

The `WithGenerateHtml` method outputs an HTML report highlighting the differences.

   ```
   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("------------------------------------------------------------------------------");
   }
   ```

The returned result from `CompareText` includes a `Differences` property, which you can use to print out the differences between texts.
