C#如何在句子结束前删除空格

时间:2018-05-14 16:12:47

标签: c#

我试图在句子结束前删除空格但没有成功。我正在考虑使用Split功能,但它并不顺利。我唯一成功的就是在句子结束后添加空格。这是我的代码:

static void Main(string[] args)
        {
            System.Windows.Forms.OpenFileDialog dlgOpen = new System.Windows.Forms.OpenFileDialog();
            if (dlgOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StreamReader sr = new StreamReader(dlgOpen.FileName);
                string dat1 = sr.ReadToEnd();
                string dat2 = Path.GetDirectoryName(dlgOpen.FileName);
                string dat3 = Path.GetFileNameWithoutExtension(dlgOpen.FileName);
                string dat4 = Path.GetExtension(dlgOpen.FileName);

                dat2 = dat2 + "/" + dat3 + "_norm" + dat4;
                sz1(ref dat1);
                Console.Write(dat1);
                StreamWriter sw = new StreamWriter(dat2, false);
                sw.WriteLine(dat1);
                sw.Flush();
                sw.Close();

                Console.ReadLine();
            }
        }
        static void sz1(ref string dat1)
        {
            char[] ArrayCharacters = { '.', ':', ',', ';', '!', '?' };
            int i = -1;
            dat1 = dat1.Trim();

            for (int k = 0; k < dat1.Length; k++)
            {
                dat1 = dat1.Replace("  ", " ");
            }

            do
            {
                i = dat1.IndexOfAny(ArrayCharacters, i + 1);

                if (i != -1)
                {
                    dat1 = dat1.Insert((i + 1), " ");
                    dat1 = dat1.Replace("  ", " ");
                }
            } while (i != -1);

            do
            {
                i = dat1.IndexOfAny(ArrayCharacters, i + 1);

                if (i != -1)
                {
                    dat1 = dat1.Insert((i - 1), "  ");
                    dat1 = dat1.Replace("  ", " ");
                    dat1 = dat1.Remove(i - 1, 1);
                }
            } while (i != -1);
        }

3 个答案:

答案 0 :(得分:2)

一种选择是使用正则表达式:

string pattern = "\\s+$";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(dat1, replacement);

答案 1 :(得分:0)

如果您只是在学习编程,那么您应该熟悉的一个解决方案是使用循环将字符串一次一个字符处理,并且因为我们正在检查字符串的结尾,所以它是有意义的向后走。

我假设您的代码(尽管如果您在问题中澄清它会很好)您在句子末尾有一组允许的字符,并且您希望单独保留这些字符,但删除任何额外的空格。

然后,逻辑将从字符串的结尾开始,如果字符是有效的结束字符,则不管它。否则,如果是空格,请将其删除。最后,如果不是,那么我们就完成了。

下面是一个使用此逻辑的方法,以及用于存储结果的StringBuilder变量。从字符串的末尾开始,我们捕获最后的字符,如果它们有效则将它们添加到结果中,如果它们是空格则跳过它们,直到我们达到“常规”字符,此时我们保留其余的字符。字符串:

static string TrimEndSpaces(string input)
{
    // If the input is null, there's nothing to do - just return null
    if (input == null) return input;

    // Our array of valid ending punctuation
    char[] validEndingPunctuation = { '.', ':', ',', ';', '!', '?' };

    // This will contain our final result
    var result = new StringBuilder();

    // Walk backwards through the input string
    for (int i = input.Length - 1; i >= 0; i--)
    {
        if (validEndingPunctuation.Contains(input[i]))
        {
            // Valid character, so add it and keep going backwards
            result.Insert(0, input[i]);
            continue;
        }

        if (input[i] == ' ')
        {
            // Space character at end - skip it
            continue;
        }

        // Regular character found - we're done. Add the rest of the string
        result.Insert(0, input.Substring(0, i + 1));
        break;
    }

    return result.ToString();
}

以下是一个示例用法,其中一些测试句子具有不同的空格结尾,有效字符,空字符串,空字符串等:

private static void Main()
{
    var testInput = new List<string>
    {
        null,
        "",
        "       ",
        "Normal sentence test.",
        "Test with spaces   .",
        "Test with multiple ending chars  !?!?!",
        "Test with only spaces at end   ",
        "Test with spaces after punctuation.   ",
        "Test with mixed punctuation and spaces ! ? ! ? ! "
    };

    foreach (var test in testInput)
    {
        // Format output so we can "see" null and empty strings
        var original = test ?? "<null>";
        if (original.Length == 0) original = "<empty>";

        // Show original and the result. Wrap result in <> so we know where it ends.
        Console.WriteLine($"{original.PadRight(50, '-')} = <{TrimEndSpaces(test)}>");
    }

    GetKeyFromUser("\nDone! Press any key to exit...");
}

<强>输出

enter image description here

答案 2 :(得分:-1)

如果您只是想从最后删除它们,可以使用:

if(myString.EndsWith(" ") == true)
{
    myString = myString.TrimEnd();
}

当然,您需要考虑结束符号“。”,“!”或者“?”,如果空格就在它之前,你可能想要排除那个字符。

另一种方法是:

var keepTrimming = true;

while(keepTrimming == true)
{
    if(myString.EndsWith(" ") == true)
    {
        myString= myString.Remove(myString.Length - 1);
    }
    else
    {
        keepTrimming = false
    }
}