#region Apache License 2.0 /* Nuclex .NET Framework Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #endregion // Apache License 2.0 using System; 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(this 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(this 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( this 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(this 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(this 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( this 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