How to remove lowercase on a textbox?

风格不统一 提交于 2019-11-30 08:30:45

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.

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

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

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

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

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

Without Regex:

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

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

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
string Code = Regex.Replace(txtDesc.text, "[a-z]", "");

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());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!