-1

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.

morlog47
  • 7
  • 2
  • So what would be the result of your example: "**Hello**! my! friend!" or "**Hello**! **my**! **friend**"? Aka would an "friend" at a different index be considered a "difference" for you. – OH GOD SPIDERS Jun 23 '17 at 08:46
  • The resut must be "Hello! my! friend" but the "!" are blue. – morlog47 Jun 23 '17 at 08:53
  • 1
    I'm left wondering, is your google broken - I found [this example](https://stackoverflow.com/questions/22184448/surrounding-jtextpane-with-images/22185053#22185053) – MadProgrammer Jun 23 '17 at 09:03
  • Sorry @MadProgrammer I've found nothing to solve my problem. The example is good, but I can't solve my problem with it. – morlog47 Jun 23 '17 at 09:13
  • @morlog47 Well, you're not going to find the "perfect" solution to your problem, you're going to have to solve small parts of it, the example demonstrates how to interact with a `StyledDocument` which would at least be a "starting" point, I suggest having a going and seeing what you can do – MadProgrammer Jun 23 '17 at 10:34
  • (1-) `I've found nothing to solve my problem.` - well of course you will need to write the algorithm to find the "different" characters. Finding the characters to highlight is a non trivial exercise depending on your exact requirement of what a match/difference is. However, highlighting the character once you find them is easily demonstrated in that link, or you can just use the section from the Swing tutorial on [Text Component Features](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html) for more examples. – camickr Jun 23 '17 at 13:44
  • @camickr Writing the algorithm for that is my problem. That is the reason why I'm asking for. – morlog47 Jun 23 '17 at 13:59

1 Answers1

0

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.

Shiro
  • 2,610
  • 2
  • 20
  • 36
V. Courtois
  • 315
  • 7
  • 20
  • This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether. – Bilal Ahmed Jun 23 '17 at 10:16
  • @BilalAhmed sorry but I'm new to stackoverflow, and I don't have the right to comment. Would love to~ – V. Courtois Jun 23 '17 at 10:23