如何在Java中检查多行字符串是否包含另一个多行字符串?

huangapple go评论98阅读模式
英文:

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.以下是我编写的代码。

  1. String a = "name Posephine Esmerelda Bloggs\n" +
  2. "birthday 30-10-1960\n" +
  3. "address 102 Smith St, Summer hill, NSW 2130\n" +
  4. "";
  5. String b = "Posephine Esmerelda Bloggs\n" +
  6. "30-10-1960\n";
  7. 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.

  1. String a = "name Posephine Esmerelda Bloggs\n" +
  2. "birthday 30-10-1960\n" +
  3. "address 102 Smith St, Summer hill, NSW 2130\n" +
  4. "";
  5. String b = "Posephine Esmerelda Bloggs\n" +
  6. "30-10-1960\n";
  7. System.out.println("a.contains(b)");

答案1

得分: 0

这段代码的翻译如下:

  1. 它可以尝试这样做 -
  2. String a = "姓名 Posephine Esmerelda Bloggs\n" +
  3. "生日 30-10-1960\n" +
  4. "地址 102 Smith St, Summer hill, NSW 2130\n" +
  5. "";
  6. String b = "Posephine Esmerelda Bloggs\n" +
  7. "生日 30-10-1960\n";
  8. System.out.println(a.contains(b));
  9. 在你的代码中有一些问题 -
  10. 1. 你在println方法中使用了字符串字面值它必须是一个引用
  11. 2. 你正在比较的内容不同'a'中有一个额外的单词'生日''b'中缺少它
  12. 正确的字符串序列我在示例代码中粘贴了出来
英文:

it works try this -

  1. String a = "name Posephine Esmerelda Bloggs\n" +
  2. "birthday 30-10-1960\n" +
  3. "address 102 Smith St, Summer hill, NSW 2130\n" +
  4. "";
  5. String b = "Posephine Esmerelda Bloggs\n" +
  6. "birthday 30-10-1960\n";
  7. System.out.println(a.contains(b));

in your code there is some issue -

  1. you used as a string literals in println method, it must be a reference.
  2. 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 字符串的某个地方:

  1. String a = "name Posephine Esmerelda Bloggs\n" +
  2. "birthday 30-10-1960\n" +
  3. "address 102 Smith St, Summer hill, NSW 2130\n" +
  4. "";
  5. String b = "Posephine Esmerelda Bloggs\n" +
  6. "30-10-1960\n";
  7. String[] lines = b.split("\n");
  8. // 现在检查每一行
  9. boolean all = true;
  10. for (String line : lines) {
  11. if (!a.contains(line)) {
  12. all = false;
  13. break;
  14. }
  15. }
  16. if (all) {
  17. System.out.println("MATCH");
  18. } else {
  19. System.out.println("NO MATCH");
  20. }

这会打印:

  1. MATCH
英文:

I think the logic you want here is to assert that every line of the b string appears somewhere in the a string:

  1. String a = "name Posephine Esmerelda Bloggs\n" +
  2. "birthday 30-10-1960\n" +
  3. "address 102 Smith St, Summer hill, NSW 2130\n" +
  4. "";
  5. String b = "Posephine Esmerelda Bloggs\n" +
  6. "30-10-1960\n";
  7. String[] lines = b.split("\n");
  8. // now check each line
  9. boolean all = true;
  10. for (String line : lines) {
  11. if (!a.contains(line)) {
  12. all = false;
  13. break;
  14. }
  15. }
  16. if (all) {
  17. System.out.println("MATCH");
  18. }
  19. else {
  20. System.out.println("NO MATCH");
  21. }

This prints:

  1. MATCH

答案3

得分: -2

  1. *寻找在b中找到两个部分的问题的答案*
  2. String[] arr = b.split("\n");
  3. int pos = a.indexOf(arr[0] + '\n');
  4. if (pos > -1 && a.indexOf(arr[1] + '\n', pos) > -1)
  5. System.err.println("找到了");
  6. `+ '\n'`仅当部分以`\n`结尾时才找到这些部分<br />
  7. 如果不需要检查结尾的`'\n'`请移除`+ '\n'`<br />
  8. 如果部分可以无序则可以省略`pos`参数<br />
  9. *不检查结尾的`\n`或所选解决方案中的顺序*
  10. String[] arr = b.split("\n");
  11. if (a.indexOf(arr[0]) > -1 && a.indexOf(arr[1]) > -1)
  12. System.err.println("找到了");
  13. *不检查的解决方案与以下内容相同*
  14. if (a.contains(arr[0]) && a.contains(arr[1]))
  15. System.err.println("找到了");
  16. *...因为* `contains(s)` *返回* `indexOf(s.toString()) > -1`
英文:

for the answer of the question how to find the two parts in b<br />

  1. String[] arr = b.split( &quot;\n&quot; );
  2. int pos = a.indexOf( arr[0] + &#39;\n&#39; );
  3. if( pos &gt; -1 &amp;&amp; a.indexOf( arr[1] + &#39;\n&#39;, pos ) &gt; -1 )
  4. System.err.println( &quot;found&quot; );

+ &#39;\n&#39;: finds the parts only if they end with a \n<br />
if the check for the ending &#39;\n&#39; is unnecessary remove both + &#39;\n&#39;<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

  1. String[] arr = b.split( &quot;\n&quot; );
  2. if( a.indexOf( arr[0] ) &gt; -1 &amp;&amp; a.indexOf( arr[1] ) &gt; -1 )
  3. System.err.println( &quot;found&quot; );

the solution without checking is identical to the following

  1. if( a.contains( arr[0] ) &amp;&amp; a.contains( arr[1] ) )
  2. System.err.println( &quot;found&quot; );

…because contains(s) returns indexOf(s.toString()) &gt; -1

huangapple
  • 本文由 发表于 2020年5月4日 18:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61590122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定