How to remove lowercase on a textbox?

后端 未结 11 1818
难免孤独
难免孤独 2020-12-31 02:47

I\'m trying to remove the lower case letters on a TextBox..

For example, short alpha code representing the insurance (e.g., \'BCBS\' for \'Blue Cross B

相关标签:
11条回答
  • 2020-12-31 03:36

    Rather than matching on all capitals, I think the specification would require matching the first character from all the words. This would allow for inconsitent input but still be reliable in the long run. For this reason, I suggest using the following code. It uses an aggregate on each Match from the Regex object and appends the value to a string object called output.

    string input = "Blue Cross BLUE shield 12356";
    Regex regex = new Regex("\\b\\w");
    string output = regex.Matches(input).Cast<Match>().Aggregate("", (current, match) => current + match.Value);
    Console.WriteLine(output.ToUpper()); // outputs BCBS1
    
    0 讨论(0)
  • 2020-12-31 03:38
    string Code = Regex.Replace(txtDesc.text, "[a-z]", "");
    
    0 讨论(0)
  • 2020-12-31 03:39

    I´d map the value to your abbreviation in a dictionary like:

    Dictionary<string, string> valueMap = new Dictionary<string, string>();
    valueMap.Add("Blue Cross Blue Shield", "BCBS");
    
    string Code = "";
    if(valueMap.ContainsKey(txtDesc.Text))
      Code = valueMap[txtDesc.Text];
    else
      // Handle
    

    But if you still want the functionality you mention use linq:

    string newString = new string(txtDesc.Text.Where(c => char.IsUpper(c).ToArray());
    
    0 讨论(0)
  • 2020-12-31 03:46

    Without Regex:

    string input = "Blue Cross Blue Shield";
    string output = new string(input.Where(Char.IsUpper).ToArray());
    Response.Write(output);
    
    0 讨论(0)
  • 2020-12-31 03:49
    string caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    string.Join("",
       "Blue Cross Blue Shield".Select(c => caps.IndexOf(c) > -1 ? c.ToString() : "")
                               .ToArray());
    
    0 讨论(0)
提交回复
热议问题