locked
C# 2010 working with strings RRS feed

  • Question

  • In a C#.net 2010 application, I need to only select alphabetic characters (A to Z) from a large textbox where users can enter anything. These alphabetic characters can be in upper or lower case. I need to concantenate the results together. No other characters are suppose to be selected. Thus can you tell me how to accomplish this task?
    Saturday, June 2, 2012 4:43 PM

Answers

  • I meant regular expression approach something like, but that also ignores whitespace that's why I asked it.

                // regex
    
                string text = "Cat7534Dog%%Mouse+";
    
                string pattern = "[A-Za-z]";
    
                StringBuilder sb = new StringBuilder();
    
                foreach (Match m in Regex.Matches(text, pattern))
                {
                    sb.Append(m.Value);
                }
    
                Console.WriteLine(sb.ToString()); // produces "CatDogMouse"

    But the looping proposed by AceInfinity is same without reqular expression, both just select those chars that are letters. Difference is that in regex approach when looping through matches I already have all the letters selected by regex. So if LINQ is not familiar AceInfinity's code is similar as normal looping through string that is char collection and selecting only those chars that are letters, also ignoring whitespace:

                // looping
    
                sb = new StringBuilder();
    
                foreach (char c in text)
                {
                    if (char.IsLetter(c))
                    {
                        sb.Append(c);
                    }
                }
    
                Console.WriteLine(sb.ToString());  // produces "CatDogMouse"

    • Proposed as answer by MusicDemon Sunday, June 3, 2012 12:47 PM
    • Marked as answer by midnight_car Monday, June 4, 2012 3:11 AM
    Sunday, June 3, 2012 5:57 AM

All replies

  • Two options spring to mind. First, use KeyUp event to check the key that has been pressed if it is not alphabetic ignore it. Secondly, when the text box looses focus or use validation events, loop through the text, if char is alphabetic append to a temporary string, when last char done make Text = temporary.

    I'd try the first approach so look at KeyUp event (and KeyDown, KeyPress might be worth looking at as well).


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Saturday, June 2, 2012 5:09 PM
  • So no whitespace either?

    Other simple option might be to use reqular expression to get only alphabets when the text box loses the focus. Then append those that contain alphabets to the result strings. Key events will work if you have ASP.NET application, with that reqular expression is in my opinion better, but also with key events writing to the text box may seem slow to the end user if he/she is quick typer.


    • Edited by MasaSam Saturday, June 2, 2012 5:59 PM
    Saturday, June 2, 2012 5:49 PM
  • How would you use a regular expression? I do not know how to use them. Key events would not work since the location I can edit the information, is past when data is in a textbox. It is in a very long string field.
    Sunday, June 3, 2012 2:18 AM
  • How would you suggest the 'looping' logic you are referring to? Can you give me an example?
    Sunday, June 3, 2012 2:19 AM
  • No need for Regex, here's how you can get all the letters from the textbox:

    string textboxLetters = new string(textBox1.Text.Where(c => char.IsLetter(c)).ToArray());


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum


    • Edited by Troy Garner Sunday, June 3, 2012 3:42 AM
    • Proposed as answer by MasaSam Sunday, June 3, 2012 3:22 PM
    Sunday, June 3, 2012 3:41 AM
  • I meant regular expression approach something like, but that also ignores whitespace that's why I asked it.

                // regex
    
                string text = "Cat7534Dog%%Mouse+";
    
                string pattern = "[A-Za-z]";
    
                StringBuilder sb = new StringBuilder();
    
                foreach (Match m in Regex.Matches(text, pattern))
                {
                    sb.Append(m.Value);
                }
    
                Console.WriteLine(sb.ToString()); // produces "CatDogMouse"

    But the looping proposed by AceInfinity is same without reqular expression, both just select those chars that are letters. Difference is that in regex approach when looping through matches I already have all the letters selected by regex. So if LINQ is not familiar AceInfinity's code is similar as normal looping through string that is char collection and selecting only those chars that are letters, also ignoring whitespace:

                // looping
    
                sb = new StringBuilder();
    
                foreach (char c in text)
                {
                    if (char.IsLetter(c))
                    {
                        sb.Append(c);
                    }
                }
    
                Console.WriteLine(sb.ToString());  // produces "CatDogMouse"

    • Proposed as answer by MusicDemon Sunday, June 3, 2012 12:47 PM
    • Marked as answer by midnight_car Monday, June 4, 2012 3:11 AM
    Sunday, June 3, 2012 5:57 AM
  • If you need the space as part of it:

    string textboxLetters = new string(textBox1.Text.Where(c => char.IsLetter(c) || c == ' ').ToArray());

    This should do every letter A-Z upper or lowercase, as well as include spaces.


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum

    Sunday, June 3, 2012 7:37 PM
  • I would use Regex.Replace instead of a loop.

    string text = "Cat7534Dog%%Mouse+";
    string result = Regex.Replace(text, "[^A-Za-z]", "");
    Console.WriteLine(result); // produces "CatDogMouse"

    Monday, June 4, 2012 8:45 AM