Issue
I have two strings :
String s1 = "D3516025204 StackOverflow is good.";
String s2 = "D3516025204 StackOverflow Nice to see you.";
First compare both the string character by character and print the indexes or column no. if the characters are different. I need to include the spaces in the string.
Like in above string :
Difference of indexes from s1 and s2: 34 to 48.
I tried below :
char[] first = text1.toLowerCase().toCharArray();
char[] second = text2.toLowerCase().toCharArray();
int minimum = Math.min(first.length, second.length);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < minimum; i++) {
if (first[i] != second[i]) {
do{
//System.out.print(first[i]+" "+second[i]);
System.out.println(text1.offsetByCodePoints(first[i],text1.indexOf(second[i])));
}while(first[i] == second[i]);
}
}
Solution
String text1 = "D8516025209 StackOverflow is good.";
String text2 = "D3516025204 StackOverflow Nice to see you.";
int minimum = Math.min(text1.length(), text2.length());
int maximum = Math.max(text1.length(), text2.length());
StringBuilder stringBuilder = new StringBuilder("Difference of indices from s1 and s2: ");
StringBuilder text1Diff = new StringBuilder();
StringBuilder text2Diff = new StringBuilder();
int index = 0;
while (index < minimum)
{
if (text1.charAt(index) != text2.charAt(index))
{
stringBuilder.append(stringBuilder.length() > 0 ? ", " : "").append(index + 1).append(" - ");
text1Diff.append(text1Diff.length() > 0 ? ", " : "");
text2Diff.append(text2Diff.length() > 0 ? ", " : "");
while ((index < minimum) && (text1.charAt(index) != text2.charAt(index)))
{
text1Diff.append(text1.charAt(index));
text2Diff.append(text2.charAt(index));
index++;
}
if (index == minimum)
{
stringBuilder.append(maximum);
index = maximum;
if (maximum == text1.length())
text1Diff.append(text1.substring(minimum));
else
text2Diff.append(text2.substring(minimum));
}
else
stringBuilder.append(index);
}
index++;
}
if (minimum != maximum && index < maximum)
{
stringBuilder.append(stringBuilder.length() > 0 ? ", " : "").append(minimum + 1).append(" - ").append(maximum);
if (maximum == text1.length())
text1Diff.append(text1.substring(minimum));
else
text2Diff.append(text2.substring(minimum));
}
System.out.println(stringBuilder.toString());
System.out.println("Differences: ");
System.out.println(text1Diff.toString());
System.out.println(text2Diff.toString());
Answered By – aviad
Answer Checked By – Clifford M. (BugsFixing Volunteer)