How to keep only numbers and some special characters in a string?
<p>I have a string containing regular characters, special characters and numbers. I'm trying to remove the regular characters, just keeping the numbers and the special characters. I use a loop to check if a character is a special character or a number. Then, I replace it with an empty string. However, this doesn't seem to work because I get an error "can't apply != to string or char". My code is below. If possible, please give me some ideas to fix this. Thanks.</p>

<pre><code>public string convert_string_to_no(string val)
{
    string str_val = "";
    int val_len = val.Length;
    for (int i = 0; i &lt; val_len; i++)
    {
        char myChar = Convert.ToChar(val.Substring(i, 1));
        if ((char.IsDigit(myChar) == false) &amp;&amp; (myChar != "-"))
        {
             str_val = str_val.replace(str_val.substring(i,1),"");
        }
    }
    return str_val;
} 
</code></pre>