Compare a short and a long string in JTextPane and write characters of the long string in an other color

74 Views Asked by At

I hope you can help me to solve my Problem.

I have two Strings and both are nearly equal.

String A is like: "Hello my friend"

String B is like: "Hello! my! friend"

Now I want to compare both Strings and write the result into another String. The result must be the String B with all diffent characters from String A in blue Color.

The "!" is for example. "!" could be any Kind of text including whitespaces.

I have to realize this with a JTextPane/StyledDocument.

Can you give me some examples or ideas to get this?

Thank you.

1

There are 1 best solutions below

2
V. Courtois On

Have you tried writing chars one by one, applying a new style to every single char? That might be dirty but it's easy and that kinda solves your problem.

If it's an algorithmic problem - I don't think it is, but still - you should use a third string in which you add every character of String A and when you encounter a difference, you add the character from B and decrease your iterator on A.

Hope this helps.