We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Search and cCountString

Hello,

Would need a function that determines the frequency of a search string in a document.

Hi György,

Thanks for your inquiry. I think, you can achieve what you need by making use of the article suggested below:

https://docs.aspose.com/words/java/find-and-replace/#customize-find-and-replace-operation

I hope, this will help.

Best Regards,

Oh dear, It could not even something more complicated? :slight_smile:

Is there another way to get the needed information ?

Hi György,

Thanks for your inquiry.

You can also achieve that by using Replace Evaluator, please follow up this link where my colleague described about this workaround.

https://docs.aspose.com/words/java/find-and-replace/#customize-find-and-replace-operation

In case of any ambiguity, please let me know.

Hi there,

I can also suggest a simplier way to find the number of occurances of a phrase

You can use the Range.Replace method to achieve this. In your case you would want to use the method overload that takes simple strings and matches them, for example:

int count = doc.Range.Replace("Hello World", "Hello World", false, false);

The number of occurrences of the string “Hello World” in the document is stored in count.

Thanks,

doc.Range.Replace ("Hello World", "Hello World", 

has the disadvantage that a found “HELLO WORLD” unintentionally in “Hello World” will be changed, which is often not desired.

Hi
György,

Thanks for your inquiry.

In this case, you can use replace method like:

[C#]

public int Replace(
string oldValue,
string newValue,
bool isMatchCase,
bool isMatchWholeWord);

Parameters

oldValue - A string to be replaced.
newValue - A string to replace all occurrences of oldValue.
isMatchCase - True indicates case-sensitive comparison, false indicates case-insensitive comparision.
isMatchWholeWord - True indicates the oldValue must be a standalo

For further details, please follow up the link:

https://reference.aspose.com/words/net/aspose.words/range/methods/replace

Hi there,

Thanks for your inquiry.

Yes, your right that is one downside of that code example. If appropriate, you could get around this by cloning the document first (doc.Clone) and executing replace on that document. This would find the correct count without affecting your original document.

Otherwise you can use the Replacing callback like below to execute find with any modification to the original document. Please see the code below.

DocumentTextFinder textFinder = new DocumentTextFinder();
doc.Range.Replace(new Regex("My Text", RegexOptions.IgnoreCase), textFinder, false);
int count = textFinder.Count;
public class DocumentTextFinder : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        mCount++;
        return ReplaceAction.Skip;
    }

    public int Count
    {
        get { return mCount; }
    }

    private int mCount = 0;
}

If we can help with anything else, please feel free to ask.

Thanks,