代码之家  ›  专栏  ›  技术社区  ›  Entity

C#从字符串末尾移除字符串

  •  34
  • Entity  · 技术社区  · 14 年前

    string[] remove = { "a", "am", "p", "pm" };
    

    我有一个用户输入文本的文本框。如果他们在 remove 数组在文本框中文本的末尾,应将其删除。最简单的方法是什么?

    为了澄清,我正在做一个时间分析器。当给函数一个字符串时,它会尽力将其解析为以下格式: 08:14pm 我有一个文本框来测试它。当焦点离开文本框时,我需要得到不带am/pm/a/p后缀的文本,这样我就可以解析只有数字的段。

    8 回复  |  直到 8 年前
        1
  •  73
  •   ahsteele tungi52    8 年前
    string[] remove = { "a", "am", "p", "pm" };
    string inputText = "blalahpm";
    
    foreach (string item in remove)
        if (inputText.EndsWith(item))
        {
            inputText = inputText.Substring(0, inputText.LastIndexOf(item));
            break; //only allow one match at most
        }
    
        2
  •  19
  •   LukeH    14 年前
    foreach (string suffix in remove)
    {
        if (yourString.EndsWith(suffix))
        {
            yourString = yourString.Remove(yourString.Length - suffix.Length);
            break;
        }
    }
    
        3
  •  7
  •   Shane Yu    12 年前

    我认为BrokenGlass的解决方案是一个很好的解决方案,但就我个人而言,我更喜欢创建三个独立的方法,允许用户只修剪start、end或两者。

    如果这些麻烦将被大量使用,我将在helper类中创建它们和/或作为扩展方法; http://msdn.microsoft.com/en-gb/library/vstudio/bb383977.aspx

    public string TrimStart(string inputText, string value, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
    {
        if (!string.IsNullOrEmpty(value))
        { 
            while (!string.IsNullOrEmpty(inputText) && inputText.StartsWith(value, comparisonType))
            {
                inputText = inputText.Substring(value.Length - 1);
            }
        }
    
        return inputText;
    }
    
    public string TrimEnd(string inputText, string value, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
    {
        if (!string.IsNullOrEmpty(value))
        {
            while (!string.IsNullOrEmpty(inputText) && inputText.EndsWith(value, comparisonType))
            {
                inputText = inputText.Substring(0, (inputText.Length - value.Length));
            }
        }
    
        return inputText;
    }
    
    public string Trim(string inputText, string value, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
    {
        return TrimStart(TrimEnd(inputText, value, comparisonType), value, comparisonType);
    }
    

    使用这些方法,我们可以修改代码,以便在包含要修剪的字符串的数组中循环。

    var content = "08:00 AM";
    var removeList = new [] { "a", "am", "p", "pm" };
    
    for (var i = 0; i < removeList.length; i++)
    {
        content = TrimEnd(content, removeList[i]);
    }
    

    注意:此代码可以进一步优化,但会以良好的速度正常工作。

        4
  •  4
  •   Metro Smurf    14 年前
        5
  •  1
  •   Chris Klepeis    14 年前

    using System.Text.RegularExpressions;
    
    public bool IsValidTime(string thetime)
    {
      Regex checktime = new Regex(@"^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$");
    
      return checktime.IsMatch(thetime);
    } 
    

    假设你想要xx:xx格式的时间

        6
  •  1
  •   Jeff Ogata    14 年前
    var output = (from x in remove
          where input.EndsWith(x)
         select input.Substring(0, input.Length - x.Length)).FirstOrDefault() ?? input;
    
        7
  •  1
  •   C. Dragon 76    14 年前

    你也许可以用 DateTime

    使用 DateTime.TryParse 读取用户输入的时间。使用 DateTime.ToString 用你需要的格式重新格式化。

        8
  •  0
  •   ulty4life    14 年前
    char[] remove = { 'a', 'p', 'm' };
    string s = "8:14pm";
    
    s = s.TrimEnd(remove);