How to compare 2 strings & print difference?

I’ve 2 strings:
String A = “This is a test”
String B = “This is”

Taking A as reference, I want to compare it with B and print whatever is not present in B.

Expected output after compare: “a test”

@abhi_25690,

May be this will help

use stringA.Replace(StringB,“”) , this will replace the text of stringA and returns the remaining :slight_smile:

Hi @abhi_25690,
Welcome to the Community!
Here you have similar topic from yesterday:

1 Like

@abhi_25690
If you want to capture everything after the first difference, use
var intDiffIndex = String.Compare(string1, string2).
The output of this will be the index of the first difference. Then use Substring method to get the additional text,
var difference = string1.substring(intDiffIndex)

But if you want all the differences, then check the below post.

Hope this helps!

2 Likes

Thanks @Madhavi - This is exactly what I was looking for!! :grinning:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.