gpt4 book ai didi

java - 比较 2 个多行字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:23:28 26 4
gpt4 key购买 nike

我正在开发一个应用程序,我希望(如果可能的话)能够比较 2 个多行字符串。

例如:

string1=
001000
000100
000100

string4=
10001000
00100100
00000100

string2=
10
01
01

string3=
010
010
010

输出:

string1.contains(string2)   > true
string1.contains(string3) > false
string4.contains(string2) > true
string4.contains(string3) > false

我需要一个能够看到 string1 包含 string2 的函数。我已经尝试过,但尚未找到满足我需求的答案。我需要它能够看到它包含行彼此相对的字符串(例如,string1 必须在一行上有 10,在它下面必须有 01,依此类推。)

如果该函数可行,那么它应该为“string1 包含 string2) 返回 true,为”string 1 包含 string3) 返回 false,

如果您能提供任何帮助,我们将不胜感激。 (请尽可能使用 VB、C# 或类似的示例代码)

提前致谢。

最佳答案

根据您对@RufusL 的 answer 的评论:

It looks promising but i'm not sure it would work as string1 will not always be 3 lines long. But your code looks easily adaptable to make it cycle through the lines if they had different lengths :)

我假设要比较的源可能包含比要比较的字符串更多的“行”。因此,要采取的步骤基本上是:

  1. 将源字符串和待比较字符串转换成行。
  2. 虽然源中有足够的候选起始行,但请尝试找到匹配的起始行。
  3. 当在候选起始源行中找到匹配的子字符串时,检查所有后续行是否在该子字符串位置匹配。如果他们匹配 => 匹配

下面的代码片段提供了此算法在 C# 扩展方法中的实现。

public static class LinesMatcher
{
public static int CountMatches(this string source, string toCompare, params string[] lineSeparators)
{
// split into parts.
var srcParts = source.Split(lineSeparators, StringSplitOptions.RemoveEmptyEntries);
var cmpParts = toCompare.Split(lineSeparators, StringSplitOptions.RemoveEmptyEntries);

// check until candidate first matching source lines have been exhausted.
var matchCount = 0;
var startLineNdx = 0;
while (cmpParts.Length <= (srcParts.Length - startLineNdx))
{
// search for a match from the start of the current line.
var matchNdx = srcParts[startLineNdx].IndexOf(cmpParts[0]);
while (matchNdx >= 0)
{
// Line has a match with the first line in cmpParts
// Check if all subsequent lines match from the same position.
var match = true;
for (var i = 1; i < cmpParts.Length; i++)
{
if (srcParts[startLineNdx + i].IndexOf(cmpParts[i], matchNdx) != matchNdx)
{
match = false;
break;
}
}
if (match) // all lines matched
matchCount++;

// try to find a next match in this line.
matchNdx = srcParts[startLineNdx].IndexOf(cmpParts[0], matchNdx + 1);
}

// Try next line in source as matching start.
startLineNdx++;
}
return matchCount;
}
}

用法:

class Program
{
public static void Main(params string[] args)
{
var seps = new[] { "\n" };
var string0 = "00000010\n001000\n000100\n000100";
var string1 = "001000\n000100\n000100";
var string4 = "10001000\n00100100\n00000100";
var string2 = "10\n01\n01";
var string3 = "010\n010\n010";

Console.WriteLine(string1.CountMatches(string2, seps));
Console.WriteLine(string1.CountMatches(string3, seps));
Console.WriteLine(string4.CountMatches(string2, seps));
Console.WriteLine(string4.CountMatches(string3, seps));
Console.WriteLine(string0.CountMatches(string2, seps));
}
}

关于java - 比较 2 个多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29527106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com