How to remove lowercase on a textbox?

后端 未结 11 1817
难免孤独
难免孤独 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:28
    string Code = new String(txtDesc.text.Where(c => IsUpper(c)).ToArray());
    
    0 讨论(0)
  • 2020-12-31 03:31
    string Code = Regex.Replace(txtDesc.text, "[a-z]", "");
    
    0 讨论(0)
  • 2020-12-31 03:32

    Here is my variant:

    var input = "Blue Cross Blue Shield 12356";
    var sb = new StringBuilder();
    foreach (var ch in input) {
      if (char.IsUpper(ch)) { // only keep uppercase
        sb.Append(ch);
      }
    }
    sb.ToString(); // "BCBS"
    

    I normally like to use regular expressions, but I don't know how to select "only uppercase" in them without [A-Z] which will break badly on characters outside the English alphabet (even other Latin characters! :-/)

    Happy coding.


    But see Mr. Skeet's answer for the regex way ;-)

    0 讨论(0)
  • 2020-12-31 03:33

    Well you could use a regular expression to remove everything that wasn't capital A-Z:

    using System;
    using System.Text.RegularExpressions;
    
    class Program
    {
        static void Main( string[] args )
        {
            string input = "Blue Cross Blue Shield 12356";
            Regex regex = new Regex("[^A-Z]");
            string output = regex.Replace(input, "");
            Console.WriteLine(output);
        }
    }
    

    Note that this would also remove any non-ASCII characters. An alternative regex would be:

    Regex regex = new Regex(@"[^\p{Lu}]");
    

    ... I believe that should cover upper-case letters of all cultures.

    0 讨论(0)
  • 2020-12-31 03:33

    You can try use the 'Replace lowercase characters with star' implementation, but change '*' to '' (blank)

    So the code would look something like this:

    txtDesc.Text = "Blue Cross Blue Shield";
    string TargetString = txt.Desc.Text;
    string MainString = TargetString;
    for (int i = 0; i < TargetString.Length; i++)
    {
        if (char.IsLower(TargetString[i]))
        {
            TargetString = TargetString.Replace( TargetString[ i ].ToString(), string.Empty );
        }
    }
    Console.WriteLine("The string {0} has converted to {1}", MainString, TargetString);
    
    0 讨论(0)
  • 2020-12-31 03:34

    This isn't perfect but should work (and passes your BCBS test):

    private static string AlphaCode(String Input)
    {
        List<String> capLetter = new List<String>();
        foreach (Char c in Input)
        {
            if (char.IsLetter(c))
            {
                String letter = c.ToString();
                if (letter == letter.ToUpper()) { capLetter.Add(letter); }
            }
        }
        return String.Join(String.Empty, capLetter.ToArray());
    }
    

    And this version will handle strange input scenarios (this makes sure the first letter of each word is capitalized).

    private static string AlphaCode(String Input)
    {
        String capCase = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Input.ToString().ToLower());
    
        List<String> capLetter = new List<String>();
        foreach (Char c in capCase)
        {
            if (char.IsLetter(c))
            {
                String letter = c.ToString();
                if (letter == letter.ToUpper()) { capLetter.Add(letter); }
            }
        }
        return String.Join(String.Empty, capLetter.ToArray());
    }
    
    0 讨论(0)
提交回复
热议问题