java起始句子_比较Java中的句子

我已经提出了下面的代码和这个问题.

条件未考虑

>从任何列表中删除了项目

>第一个字符差异

>重复差异项目

如果您有疑问,请检查并告诉我.

public static void main(String[] args) {

String str1 = "It's a lovely day today.";

String str2 = "It's a very lovely day today, isnt it?";

StringBuilder builder = new StringBuilder();

StringBuilder added = new StringBuilder();

StringBuilder changed = new StringBuilder();

for (int i = 0; i < str1.length(); i++)

for (int j = 0; j < str2.length(); j++) {

if (str1.charAt(i) == str2.charAt(j)) {

if (added.length() > 0) {

builder.append("[I:" + added.toString() + "]");

added = new StringBuilder();

}

if (changed.length() > 0) {

changed.append("[C:" + changed.toString() + "]");

changed = new StringBuilder();

}

// skip as there is no difference.

builder.append(str1.charAt(i));

i++;

// check if index -1 chars are equal then there is

// difference start

} else if (str1.charAt(i - 1) == str2.charAt(j - 1)) {

// check if end of line

if ((i + 1 == str1.length())

|| (str1.charAt(i + 1) == str2.charAt(j + 1))) {

changed.append(str1.charAt(i));

changed.append("/");

changed.append(str2.charAt(j));

j++;

// everything is added

if (i + 1 == str1.length()) {

while (j < str2.length() - 1)

added.append(str2.charAt(j++));

}

continue;

}

// Go until next equal found

while (!(str1.charAt(i) == str2.charAt(j))

&& j < str2.length() - 1) {

added.append(str2.charAt(j++));

}

j--;

}

}

if (changed.length() > 0) {

builder.append("[C:" + changed.toString() + "]");

}

if (added.length() > 0) {

builder.append("[I:" + added.toString() + "]");

}

System.out.println(builder.toString());

}

产量

It's a [I:very ]lovely day today[C:./,][I: isnt it]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值