#region CPL License /* Nuclex Framework Copyright (C) 2002-2010 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Generic; namespace Nuclex.Support { /// Helper routines for working with strings public static class StringHelper { /// /// Searches for the first occurence of a character other than the characters /// listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// public static int IndexNotOfAny(string haystack, char[] anyNotOf) { return IndexNotOfAny(haystack, anyNotOf, 0, haystack.Length); } /// /// Searches for the first occurence of a character other than the characters /// listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// Index of the character in the haystack at which to start scanning /// /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// public static int IndexNotOfAny(string haystack, char[] anyNotOf, int startIndex) { return IndexNotOfAny(haystack, anyNotOf, startIndex, haystack.Length - startIndex); } /// /// Searches for the first occurence of a character other than the characters /// listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// Index of the character in the haystack at which to start scanning /// /// Number of characters in the haystack to scan /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// public static int IndexNotOfAny( string haystack, char[] anyNotOf, int startIndex, int count ) { int anyLength = anyNotOf.Length; count += startIndex; while(startIndex < count) { char character = haystack[startIndex]; int index = Array.IndexOf(anyNotOf, character, 0, anyLength); if(index == -1) { return startIndex; } ++startIndex; } return -1; } /// /// Searches backwards for the first occurence of a character other than the /// characters listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// public static int LastIndexNotOfAny(string haystack, char[] anyNotOf) { return LastIndexNotOfAny(haystack, anyNotOf, haystack.Length - 1, haystack.Length); } /// /// Searches backwards for the first occurence of a character other than the /// characters listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// Index of the character in the haystack at which to start scanning /// /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// public static int LastIndexNotOfAny(string haystack, char[] anyNotOf, int startIndex) { return LastIndexNotOfAny(haystack, anyNotOf, startIndex, startIndex + 1); } /// /// Searches backwards for the first occurence of a character other than the /// characters listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// Index of the character in the haystack at which to start scanning /// /// Number of characters in the haystack to scan /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// public static int LastIndexNotOfAny( string haystack, char[] anyNotOf, int startIndex, int count ) { int anyLength = anyNotOf.Length; count = startIndex - count; while(startIndex > count) { char character = haystack[startIndex]; int index = Array.IndexOf(anyNotOf, character, 0, anyLength); if(index == -1) { return startIndex; } --startIndex; } return -1; } } } // namespace Nuclex.Support