简体   繁体   中英

Removing Unwanted Characters

I am trying to scrub a list of items using another list and it's working fine except for its not ignoring case. When I try to add ordinal or regex casing checks, I get a syntax error. Can someone tell me what I am doing wrong? Here is my code:

List<string> removeChars = new List<string>(textBox_ScrubList.Text.Split(','));
            for (int i = 0; i < sortBox1.Count; i++)
                foreach (string repl in removeChars)
                    sortBox1[i] = sortBox1[i].Replace(repl, "", RegexOptions.IgnoreCase);

And here is the syntax error I am getting:

正则表达式套接字语法错误

Assuming sortBox1 is a List<string> or similar, the problem is that String.Replace doesn't have any overload which takes a RegexOptions .

You can use Regex.Replace , but in that case you should probably be able to construct a single regular expression to remove all the characters in one go. If you do want to remove them one at a time, you may want to use Regex.Escape to avoid regular expression patterns from being a problem. (For example, if it tried to replace "." with "", you'd end up getting rid of everything.)

So I figured it out. The last line:

 sortBox1[i] = sortBox1[i].Replace(repl, "", RegexOptions.IgnoreCase);

had to be changed to:

 sortBox1[i] = Regex.Replace(sortBox1[i], repl, "", RegexOptions.IgnoreCase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM