合并字符串以特定条件结尾的文本行

Merging Lines of text where string ends in a specific condition

我最近获得了一个新的定制构建工具,用于将文本数据操作到数据库中,但有一个功能让我感到困惑,因为我没有使用 C# 的经验,而我的同事也没有使用过由于时间限制,有一个解决方案。

我得到的工具有一个表达式生成器,以便应用规则来清理纯文本。这是我得到的指导范围:

Use C# code to write your expressions. Use the helper 'Text' string variable to refer to the whole text or the helper 'Lines' string[] variable to refer to the individual text lines. You can also use the 'Builder' (StringBuilder) helper variable to build your output. The expression should either return a string value or a string array.

我正在创建规则来清理在行尾找到的特定关键字的数据,并且需要编写一个 expression/rule 以允许我将具有特定关键字的行与下一行合并.如果行以特定关键字开头,我有一个向上移动行的功能规则,但我需要创建一个规则以在行以关键字结尾的地方向下合并。

示例输入数据

Mr. John and
Mrs. Mary Smith
The Foundation for
the Lord's Children
Widgets Incorporated
Loyal Order of
Bullwinkle the Moose

预期输出

Mr. John and Mrs. Mary Smith
The Foundation for the Lord's Children
Widgets Incorporated
Loyal Order of Bullwinkle the Moose

为了进一步了解背景,这里有一个工作表达式,它将以关键字 up 开头的行(第 [i-1] 行和第 [i] 行)与前一行合并:

for (
var i = 0; i < Lines.Length; i++) {
    if (!Lines[i].StartsWith(" "))
    if (!Lines[i].StartsWith("and "))
    if (!Lines[i].StartsWith("of "))
    if (!Lines[i].StartsWith("for "))
    if (!Lines[i].StartsWith("at "))
    if (!Lines[i].StartsWith("the "))
    if (i > 0) Builder.AppendLine();
    Builder.Append(" ").Append(Lines[i]);
}
return Builder.ToString();

使用以下示例数据并期望输出

Sample Input:
John
and Mary
and Andy Smith
Loyal Order
of Moose
Cineplex Movie Theater
Center
for the Blind

Expected Output:
John and Mary and Andy Smith
Loyal Order of Moose
Cineplex Movie Theater
Center for the Blind

我希望这是一个简单的问题,但请注意,这是一个简单的表达式生成器,我不知道完整的限制或功能,它是为我们公司定制的,所以我没有太多细节可以提供。我将尽我所能提供任何说明,但无法提供 'sample' 解决方案,因为我无法在修改工作查询方面取得任何进展,从根本上说,另一个方向。

澄清问题: 我如何编写一个循环来检查所有行,然后 merges/concatenates 行 [i] 和行 [i+1] 当行 [i] 以特定字符串结尾时(出于示例目的,示例为“和”, “的”、“的”、“在”)

非常感谢任何帮助!

编辑: 该问题因不够清楚而被关闭,但最终提供了解决方案。如果其他人遇到类似问题,这里有一个可行的解决方案。

for (var i = 0; i < Lines.Length; i++) {
    Builder.Append(Lines[i]);
    
    if (Lines[i].EndsWith(" and") || Lines[i].EndsWith(" of") ||
        Lines[i].EndsWith(" for") || Lines[i].EndsWith(" at") ||
        Lines[i].EndsWith(" the")) {
        
        if (i < (Lines.Length - 1)) {
            Builder.Append(" ").Append(Lines[i + 1]);
            i++;
        }
    }
        
    Builder.AppendLine("");
}
return Builder.ToString();

像这样应该可以解决问题:

{
...
if (!Lines[i].TrimEnd().EndsWith(" and"))
if (!Lines[i].TrimEnd().EndsWith(" of"))
if (!Lines[i].TrimEnd().EndsWith(" for"))
if (!Lines[i].TrimEnd().EndsWith(" at"))
if (!Lines[i].TrimEnd().EndsWith(" the"))
if (i > 0) Builder.AppendLine();
Builder.Append(" ").Append(Lines[i + 1]);
}
return Builder.ToString();

这应该有效...

for (var i = 0; i < Lines.Count; i++)
        {
            var keywords = new List<string>{" ", "and", "of", "for", "at", "the"};
            if (i > 0)
            {
                if (keywords.Any(x => Lines[i].StartsWith(x + " ") || Lines[i - 1].EndsWith(" " + x)))
                {
                    Builder.Append(" ");
                }
                else
                {
                    Builder.AppendLine();
                }
            }
            
            Builder.Append(Lines[i]);
        }

这是一个已开发的工作解决方案

for (var i = 0; i < Lines.Length; i++) {
    Builder.Append(Lines[i]);
    
    if (Lines[i].EndsWith(" and") || Lines[i].EndsWith(" of") ||
        Lines[i].EndsWith(" for") || Lines[i].EndsWith(" at") ||
        Lines[i].EndsWith(" the")) {
        
        if (i < (Lines.Length - 1)) {
            Builder.Append(" ").Append(Lines[i + 1]);
            i++;
        }
    }
        
    Builder.AppendLine("");
}
return Builder.ToString();