, - , - O (m log n) time, m - s n chars:
public static int NotIndexOf( this string s , string chars )
{
char[] orderedChars = chars.ToCharArray() ;
Array.Sort( orderedChars ) ;
int index = -1 ;
for ( int i = 0 ; index < 0 && i < s.Length ; ++i )
{
int p = Array.BinarySearch(orderedChars, s[i] ) ;
if ( p < 0 )
{
index = i ;
}
}
return index ;
}
With regex, you just need to build the desired class of negative characters. On the other hand, with the exception of constructing a negative character set, it is quite simple. Here, I created a set of characters using Unicode escape-code code ( \uXXXX) to not have to deal with characters that are special in the context of the character set ( ], ^, -and \just for starters).
public static int NotIndexOf( this string s , string chars )
{
StringBuilder regexString = new StringBuilder(3+6*chars.Length) ;
regexString.Append("[^") ;
foreach ( char c in chars.Distinct() )
{
regexString.AppendFormat( @"\u{0:X4}" , (ushort)c ) ;
}
regexString.Append("]") ;
Regex rxCharSet = new Regex( regexString , Regex ) ;
Match m = rxCharSet.Match(s) ;
return m.Success ? m.Index : -1 ;
}
source
share