我试图找到一种比较两个字符串并行逐行读取的好方法.我想在不使用equals方法的情况下这样做的原因是因为字符串不完全相同,为了更准确,我将给出一个例子.
String s1 = "aaa\nbbb\nccc\ddd"
String s2 = "aaa\n\rbbb\n\rccc\n\rddd"
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,当我们逐行查看时,两个字符串都具有相同的值(尽管由于s2在其中也是\ r,因此不完全相等).现在,我知道我可以使用一些删除方法来清除"\ r",但由于字符串可能非常大,我更喜欢逐行循环,一旦字符串不等于破坏我的逻辑.换句话说,我更喜欢只迭代所需的行而不是从\ r清除entrie字符串.
编辑:我不是从文件中读取.这些是简单的字符串.
有任何想法吗 :) ?
既然你说字符串可能非常大,我想你是从文件中读取的.当您使用BufferedReader并使用该readLine();方法时,您将逐行获取,而不使用行分隔符.现在,你可以使用equals().
BufferedReader reader1 = ...; // depends on your source
BufferedReader reader2 = ...; // depends on your source
String line1 = null;
String line2 = null;
while ((line1 = reader1.readLine()) != null && (line2 = reader2.readLine()) != null)
{
if (line1.equals(line2))
{
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4439 次 |
| 最近记录: |