Question: Two strings, such as "People's Republic of China", "Chinese name and Country", highlight the different parts of the two strings.
public static string[] Gethighlightdifferent (String a,string b) {string[] Temp=getdiff (A, b);
String[] Result={gethighlight (A, temp[0]), Gethighlight (b, temp[1])};
return result;
private static string Gethighlight (String source,string temp) {stringbuffer sb=new stringbuffer ();
Char[] Sourcechars=source.tochararray ();
Char[] Tempchars=temp.tochararray ();
Boolean flag=false; for (int i=0;i<sourcechars.length;i++) {if (tempchars[i]!= ') {if (i==0) sb.append ("<span style= ' Color:blue '" ;").
Append (Sourcechars[i]);
else if (flag) Sb.append (Sourcechars[i]);
else Sb.append ("<span style= ' Color:blue ' >"). Append (sourcechars[i));
Flag=true;
if (i==sourcechars.length-1) sb.append ("</span>");
else if (flag==true) {sb.append ("</span>"). Append (sourcechars[i));
Flag=false;
}else sb.append (Sourcechars[i]);
return sb.tostring ();
public static string[] Getdiff (String A, string b) {string[] result = NULL; Select a smaller length string to use to raise the substring if (a.length () < B.length ()) {result = Getdiff (A, B, 0, a.length ());
else {result = Getdiff (b, A, 0, b.length ());
result = new String[]{result[1],result[0]};
return result; ///Compare the specified portion of a with B to generate a comparison to the result private static string[] Getdiff (String A, string b, int start, int end) {string[]
result = new string[]{a, b};
int len = Result[0].length (); while (Len > 0) {for (int i = start; I < End-len + 1; i++) {String sub = Result[0].s
Ubstring (i, i + len);
int idx =-1;
if ((idx = Result[1].indexof (sub))!=-1) {result[0] = Setempty (result[0), I, i + len);
RESULT[1] = Setempty (result[1], idx, IDX + len); if (i > 0) {//recursive get blank area left variance result = Getdiff (Result[0], result[1], 0, I
); } if (i + Len < end) {//recursive get blank area right difference res
Ult = Getdiff (Result[0], result[1], i + len, end);
len=0;//exit while loop break;
len = LEN/2;
return result; ///Set the region specified by string s as a space public static string Setempty (string s, int start, int end) {char[] array = S.tochar
Array ();
for (int i = start; i < end; i++) {array[i] = ';
Return to New String (array); }
The recursive algorithm is used to compare the same parts of two strings, to place the same part in space, and then to compare the characters before and after the change to highlight the non space.
Another edit distance algorithm can be implemented, has not been studied.