2

I am trying to take a string and replace everything that isn't A-Z with a space. So for example "AB$CD$EF" should output "AB CD EF"

The problem I'm having is the following error:

Instance argument: cannot convert from 'string[]' to 'System.Linq.IQueryable'

Code:

        string[] alpha = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        string inVAR = textBox1.Text.ToUpper();
        string outVAR;

        StringBuilder sb = new StringBuilder(inVAR);

        foreach (int i in inVAR) // inVAR because stringbuilders won't work with foreach
        {
            if (alpha.Contains(sb[i]))
            {
                outVAR += sb[i].ToString();
            }
            else
            {
                sb[i] = ' ';
            }
        }

Also, if you got a different way of doing an array of A-Z, I am open! :P

For the record: Yes, I have included System.Linq

Taurophylax
  • 355
  • 1
  • 4
  • 10
  • have you looked at this? http://stackoverflow.com/questions/17457677/how-can-i-remove-none-alphabet-chars-from-a-string – hubson bropa Dec 18 '13 at 20:51

7 Answers7

6

Maybe you can use RegEx for that:

Regex rgx = new Regex("[^a-zA-Z -]");
str = rgx.Replace(str, " ");                                      

Then you can replace it or do with it whatever you want. I hope I understood your problem ;)

Dominic B.
  • 1,897
  • 1
  • 16
  • 31
3

You have many alternatives. For ex,

var strNew1 = Regex.Replace("AB$CD$EF", @"[^A-Z]", " ");

or

var strNew2 = new string("AB$CD$EF".Select(c => c >= 'A' && c <= 'Z' ? c : ' ')
                                   .ToArray());
L.B
  • 114,136
  • 19
  • 178
  • 224
2

You can do this with a few simple lines of code using regex.

string inVAR = textBox1.Text.ToUpper();
string pattern = "[^A-Z]";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(inVAR, replacement);
Robbert
  • 6,481
  • 5
  • 35
  • 61
1

Your code is confusing. I think what you want is this:

    string[] alpha = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    string inVAR = textBox1.Text.ToUpper();
    string outVAR;

    StringBuilder sb = new StringBuilder(inVAR);

    foreach (char c in inVAR) // inVAR because stringbuilders won't work with foreach
    {
        if (!alpha.Contains(c))
        {
            sb[i] = ' ';
        }
    }

    outVAR = sb.ToString();

You really don't need the alpha array. You can call char.IsUpper:

if (!char.IsUpper(c))

Or, as others have pointed out, you can use regular expressions for this task.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

You might try this:

Regex rxNonAlpha = new Regex( "[^A-Z]" , RegexOptions.IgnoreCase ) ;
string someText = GetSomeText() ;
string tidiedText = rxNonAlpha.Replace( someText , " " ) ;

Or, taking advantage of the fact that ASCII/Unicode A-Z and a-z are contiguous code points:

string Tidy( string s )
{
  StringBuilder sb = new StringBuilder( s.Length ) ;

  foreach ( char c in s )
  {
    bool isLowerCase = ( c >= 'a' && c <= 'z' ) ;
    bool isUpperCase = ( c >= 'A' && c <= 'Z' ) ;
    bool isAlpha     = isLowerCase || isUpperCase ;
    sb.Append( isAlpha ? c : ' ' ) ;
  }

  return sb.ToString() ;
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • Maybe you should add lower characters like "a-z", too! ;) – Dominic B. Dec 18 '13 at 20:57
  • 1
    Err...my code accepts both lower- and upper-case letters. You might note the `RegexOptions.IgnoreCase` in the regular expression constructor, and the comparisons in that initialize `isAlpha`. But, heck, I tweaked the code to makes it a little clearer. – Nicholas Carey Dec 18 '13 at 21:23
  • Sorry, my fault, i did not pay attention to that one! – Dominic B. Dec 18 '13 at 21:24
0

You could use Regex.

This should replace upper and lower case alpha characters with a space (untested):

Regex rgx = new Regex("[^a-zA-Z]");
outVAR = rgx.Replace(textBox1.Text, " ");
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

I would go with regular expression instead of iterating through the whole string. Slightly lighter memory needs and fast.

    string inputString = "[whatever your string is]";
    Regex replacelist = new Regex("[;\\\\/:*?\"<>|&'$]");
    string outputString = replacelist.Replace(inputString," ");

I've not tested that directly but the concept is pretty simple with Regex.