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

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

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 -

  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 字符串的某个地方:

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( &quot;\n&quot; );
int pos = a.indexOf( arr[0] + &#39;\n&#39; );
if( pos &gt; -1 &amp;&amp; a.indexOf( arr[1] + &#39;\n&#39;, pos ) &gt; -1 )
  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

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

the solution without checking is identical to the following

 if( a.contains( arr[0] ) &amp;&amp; a.contains( arr[1] ) )
   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:

确定