简体   繁体   中英

C# equivalent of Obj-C's NSCharacterSet? Need help converting iOS code

Here is my code in Objective-C for iOS that I need help converting to C#:

NSString kLegal = @"0123456789(['!";
NSString character = @"(";
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:kLegal] invertedSet];
NSString *filtered = [[character componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

I'm familiar with C# and .NET but really don't have a clue where to begin here. Thanks!

I'm not familiar with Objective-C or iOS, but from Googling around it seems that NSCharacterSet is used to filter characters - either specifying the characters you want or those you don't want.

You can do that in C# in several ways. Here's one:

string filter="0123456789(['!";
string text="My phone number is (121)5551234!";

If you want to see only the characters in filter, you do this:

string filtered = new string(text.Where(c=>filter.IndexOf(c)>=0).ToArray());

If you want to see only the characters not in the filter, you can do this:

string filtered = new string(text.Where(c=>filter.IndexOf(c)<0).ToArray());

Both aren't the most efficient way to do this. If you want efficiency, use a regular expression.

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