How to remove lowercase on a textbox?

2019-02-06 04:59发布

问题:

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 Blue Shield'):

txtDesc.text = "Blue Cross Blue Shield";

string Code = //This must be BCBS.. 

Is it possible? Please help me. Thanks!

回答1:

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.



回答2:

string Code = new String(txtDesc.text.Where(c => IsUpper(c)).ToArray());


回答3:

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 ;-)



回答4:

string Code = Regex.Replace(txtDesc.text, "[a-z]", "");


回答5:

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());


回答6:

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);


回答7:

Without Regex:

string input = "Blue Cross Blue Shield";
string output = new string(input.Where(Char.IsUpper).ToArray());
Response.Write(output);


回答8:

string caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string.Join("",
   "Blue Cross Blue Shield".Select(c => caps.IndexOf(c) > -1 ? c.ToString() : "")
                           .ToArray());


回答9:

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


回答10:

string Code = Regex.Replace(txtDesc.text, "[a-z]", "");


回答11:

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());
}