拆分字符串后如何 Trim 恰好 1 个空格

How to Trim exactly 1 whitespace after splitting the string

我制作了一个程序,通过在管道中拆分字符串来评估字符串,字符串是随机生成的,有时白色space是需要评估的部分。

HftiVfzRIDBeotsnU uabjvLPC | LstHCfuobtv eVzDUBPn jIRfai

这个字符串两边的长度相同(管道左侧为 2 x 白色 space),但是当我必须 trim 两边的 space 时,我的问题就来了管道的两侧(我在拆分后这样做) 有什么方法可以确保我只有 trim 1 个 space 而不是全部。 到目前为止我的代码:

foreach (string s in str)
    {
        int bugCount = 0;
        string[] info = s.Split('|');
        string testCase = info[0].TrimEnd();
        char[] testArr = testCase.ToCharArray();
        string debugInfo = info[1].TrimStart();
        char[] debugArr = debugInfo.ToCharArray();
        int arrBound = debugArr.Count();
        for (int i = 0; i < arrBound; i++)
            if (testArr[i] != debugArr[i])
                bugCount++;
        if (bugCount <= 2 && bugCount != 0)
            Console.WriteLine("Low");
        if (bugCount <= 4 && bugCount != 0)
            Console.WriteLine("Medium");
        if (bugCount <= 6 && bugCount != 0)
            Console.WriteLine("High");
        if (bugCount > 6)
            Console.WriteLine("Critical");
        else 
            Console.WriteLine("Done");

    }
    Console.ReadLine();

无法告诉 Trim.. 方法系列在删除一些字符后停止。

在一般情况下,您需要通过检查 Split 之后获得的部分并检查它们的 first/last 字符和子字符串来获得正确的部分来手动完成。

但是,在您的情况下,有一种更简单的方法 - Split 也可以将 string 作为参数,甚至更多 - 一组字符串:

string[] info = s.Split(new []{ " | " });
// or even
string[] info = s.Split(new []{ " | ", " |", "| ", "|" });

应该处理管道 | 字符周围的单个空格,只需将它们视为分隔符的一部分。

您有 2 个选择。

  1. 如果管道前后总是 1 space,在{space}|{space}.

    上拆分
    myInput.Split(new[]{" | "},StringSplitOptions.None);
    
  2. 否则,不要使用 TrimStart() & TrimEnd() 使用 SubString.

    var split = myInput.Split('|');
    var s1 = split[0].EndsWith(" ") 
                     ? split[0].SubString(0,split[0].Length-1) 
                     : split[0];
    var s2 = split[1].StartsWith(" ")
                     ? split[1].SubString(1) // to end of line
                     : split[1];
    

注意,这里有些复杂 - 如果管道周围没有 space,但 last/first 字符是合法的(数据)space 字符,上面的内容将被截断它关了。您需要更多逻辑,但希望这能让您入门!

这是 trim space 的字符串扩展,用于计算次数,以防万一。

public static class StringExtension
{
    /// <summary>
    /// Trim space at the end of string for count times
    /// </summary>
    /// <param name="input"></param>
    /// <param name="count">number of space at the end to trim</param>
    /// <returns></returns>
    public static string TrimEnd(this string input, int count = 1)
    {
        string result = input;
        if (count <= 0)
        {
            return result;
        }
        if (result.EndsWith(new string(' ', count)))
        {
            result = result.Substring(0, result.Length - count);
        }
        return result;
    }
    /// <summary>
    /// Trim space at the start of string for count times
    /// </summary>
    /// <param name="input"></param>
    /// <param name="count">number of space at the start to trim</param>
    /// <returns></returns>
    public static string TrimStart(this string input, int count = 1)
    {
        string result = input;
        if (count <= 0)
        {
            return result;
        }
        if (result.StartsWith(new string(' ', count)))
        {
            result = result.Substring(count);
        }
        return result;
    }
}

主要

static void Main(string[] args)
{
   string a = "1234  ";
   string a1 = a.TrimEnd(1); // returns "1234 "
   string a2 = a.TrimEnd(2); // returns "1234"
   string a3 = a.TrimEnd(3); // returns "1234  "

   string b = "  5678";
   string b1 = b.TrimStart(1); // returns " 5678"
   string b2 = b.TrimStart(2); // returns "5678"
   string b3 = b.TrimStart(3); // returns "  5678"
}