Control Characters in Word documents, How to workaround

string[] inputs = {
    "Basketball Store Comércio Varejista de Artigos Esportivos Ltda. – EPP\a85% Shareholder / Administrator (June 2022 – Present)",
    "Lírios Comércio de Produtos Naturais Ltda.\aUnspecified Shareholder / Administrator (March 2011 – Present)",
    "Araucária Produtos Naturais Ltda.\aUnspecified Shareholder / Administrator (March 2014 – Present)",
    "Litigation Information\u0002 Records from the Brazilian Supreme Federal Court, Superior Court of Justice, and the federal and state courts are reviewed."
};

Above is an array of strings that have been retrieved from my document. I am looking to Find and Replace these within my document, but for some reason, it is unable to find matches. I understand that you can’t replace with control characters, so is there a way to filter these out so that the replace does work?

var texts = "Litigation Information\u0002 Records from the Brazilian Supreme Federal Court, Superior Court of Justice, and the federal and state courts are reviewed.";

var howMany = clone.Range.Replace(texts, "");

Console.WriteLine($"Replaced {howMany} places."); // Writes: 0

@chrisbluemorgan Could you please attach your input document here for testing? Also, please provide code you use for retrieving string from the document to further replacing. We will check the issue and provide you more information.

Sherlock Holmes (Test).docx (130.3 KB)

In this document, I am breaking down the text into paragraph nodes .GetChildNodes(NodeType.P). I will then iterate over the nodes, get the text node.GetText().Trim(), and some of them will be revised. Then for nodes that have been revised, I would like to .Replace(), but this isn’t always successful. I will work on providing some actual code in the meantime.

@chrisbluemorgan Please try using p.ToString(SaveFormat.Text).Trim() instead of p.GetText().Trim() to get paragraph text. When p.GetText() method is used the output string also contains special symbols and field codes.

Ok great, I will try this. Thanks for the tip.

1 Like