Search

Advertising

Home Downloads Components

Components

Folder Path: \ Components \

File: IndexNotOfAny() for Strings

file.png
Uploaded:
July.01.09
Modified:
July.01.09
File Size:
3 KB
Downloads:
599
Version
1.0

Implementations of the C++ std::string::find_first_not_of() and std::string::find_last_not_of() methods for .NET strings.

Details

It's been several years since I switched from C++ to C#. But one of the things I'm missing whenever I do complex string operations are libc++'s std::string::find_first_not_of() and std::string::find_last_not_of() methods.

These methods find the first (or last) character in a string which is not part of the search mask:

string::size_type pos = string("hello world").find_first_not_of("hel");

Above code would return 4 because the fourth character in the string (an 'o') is the first character that is not part of the search mask.

Here's my fix for the situation in .NET:

string test = "12345abcde";

int index = test.IndexNotOfAny(new char[] { '1', '2', '3', '4', '5' });
Debug.Assert(index == 5);

I chose the method names (IndexNotOfAny() and LastIndexNotOfAny()) to match the existing methods of the .NET string class (IndexOfAny() and LastIndexOfAny()). Of course, these methods need to do multiple scans over the search mask, so they will be a bit slower than their positive counterparts, just like in C++.

As always, everything is backed by unit tests and you get 100% test coverage!

The StringHelper class is part of the Nuclex.Support library from the Nuclex Framework. You can find the most recent release of the code on the framework's CodePlex site: http://nuclexframework.codeplex.com/



Joomla Template by Joomlashack