英文:
How to check If a multiline String contains another multiline String in Java?
问题
1.我尝试使用contains函数,但它不起作用,只有当b = "30-10-1960\n"或b = "Posephine Esmerelda Bloggs\n"时才起作用。如何检查a是否包含b?
2.以下是我编写的代码。
String a = "name Posephine Esmerelda Bloggs\n" +
"birthday 30-10-1960\n" +
"address 102 Smith St, Summer hill, NSW 2130\n" +
"";
String b = "Posephine Esmerelda Bloggs\n" +
"30-10-1960\n";
System.out.println("a.contains(b)");
英文:
1.I tried to use the contains function BUT it did not work, it only works when b = "30-10-1960\n" or b = "Posephine Esmerelda Bloggs\n". How to check if a contains b?
2.Here is the code I wrote.
String a = "name Posephine Esmerelda Bloggs\n" +
"birthday 30-10-1960\n" +
"address 102 Smith St, Summer hill, NSW 2130\n" +
"";
String b = "Posephine Esmerelda Bloggs\n" +
"30-10-1960\n";
System.out.println("a.contains(b)");
答案1
得分: 0
这段代码的翻译如下:
它可以尝试这样做 -
String a = "姓名 Posephine Esmerelda Bloggs\n" +
"生日 30-10-1960\n" +
"地址 102 Smith St, Summer hill, NSW 2130\n" +
"";
String b = "Posephine Esmerelda Bloggs\n" +
"生日 30-10-1960\n";
System.out.println(a.contains(b));
在你的代码中有一些问题 -
1. 你在println方法中使用了字符串字面值,它必须是一个引用。
2. 你正在比较的内容不同。在'a'中有一个额外的单词'生日',在'b'中缺少它。
正确的字符串序列我在示例代码中粘贴了出来。
英文:
it works try this -
String a = "name Posephine Esmerelda Bloggs\n" +
"birthday 30-10-1960\n" +
"address 102 Smith St, Summer hill, NSW 2130\n" +
"";
String b = "Posephine Esmerelda Bloggs\n" +
"birthday 30-10-1960\n";
System.out.println(a.contains(b));
in your code there is some issue -
- you used as a string literals in println method, it must be a reference.
- what you are comparing is not same. In 'a' there is extra word 'birthday' that is missing in 'b'.
The correct string sequence i pasted in sample code.
答案2
得分: -1
我认为你在这里想要的逻辑是断言 b
字符串的每一行都出现在 a
字符串的某个地方:
String a = "name Posephine Esmerelda Bloggs\n" +
"birthday 30-10-1960\n" +
"address 102 Smith St, Summer hill, NSW 2130\n" +
"";
String b = "Posephine Esmerelda Bloggs\n" +
"30-10-1960\n";
String[] lines = b.split("\n");
// 现在检查每一行
boolean all = true;
for (String line : lines) {
if (!a.contains(line)) {
all = false;
break;
}
}
if (all) {
System.out.println("MATCH");
} else {
System.out.println("NO MATCH");
}
这会打印:
MATCH
英文:
I think the logic you want here is to assert that every line of the b
string appears somewhere in the a
string:
String a = "name Posephine Esmerelda Bloggs\n" +
"birthday 30-10-1960\n" +
"address 102 Smith St, Summer hill, NSW 2130\n" +
"";
String b = "Posephine Esmerelda Bloggs\n" +
"30-10-1960\n";
String[] lines = b.split("\n");
// now check each line
boolean all = true;
for (String line : lines) {
if (!a.contains(line)) {
all = false;
break;
}
}
if (all) {
System.out.println("MATCH");
}
else {
System.out.println("NO MATCH");
}
This prints:
MATCH
答案3
得分: -2
*寻找在b中找到两个部分的问题的答案*
String[] arr = b.split("\n");
int pos = a.indexOf(arr[0] + '\n');
if (pos > -1 && a.indexOf(arr[1] + '\n', pos) > -1)
System.err.println("找到了");
`+ '\n'`:仅当部分以`\n`结尾时才找到这些部分<br />
如果不需要检查结尾的`'\n'`,请移除`+ '\n'`<br />
如果部分可以无序,则可以省略`pos`参数<br />
*不检查结尾的`\n`或所选解决方案中的顺序*
String[] arr = b.split("\n");
if (a.indexOf(arr[0]) > -1 && a.indexOf(arr[1]) > -1)
System.err.println("找到了");
*不检查的解决方案与以下内容相同*
if (a.contains(arr[0]) && a.contains(arr[1]))
System.err.println("找到了");
*...因为* `contains(s)` *返回* `indexOf(s.toString()) > -1`
英文:
for the answer of the question how to find the two parts in b<br />
String[] arr = b.split( "\n" );
int pos = a.indexOf( arr[0] + '\n' );
if( pos > -1 && a.indexOf( arr[1] + '\n', pos ) > -1 )
System.err.println( "found" );
+ '\n'
: finds the parts only if they end with a \n
<br />
if the check for the ending '\n'
is unnecessary remove both + '\n'
<br />
if the parts may be unordered the pos
argument can be omitted<br />
without checking for the \n
at the end or the order as in the chosen solution
String[] arr = b.split( "\n" );
if( a.indexOf( arr[0] ) > -1 && a.indexOf( arr[1] ) > -1 )
System.err.println( "found" );
the solution without checking is identical to the following
if( a.contains( arr[0] ) && a.contains( arr[1] ) )
System.err.println( "found" );
…because contains(s)
returns indexOf(s.toString()) > -1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论