public IEnumerable<int> FindIndex(string stringToSearchIn, enIndexFinderOccurrence occurrence, string charsToSearchFor, enIndexFinderDirection direction, bool matchCase, int startIndex) { IEnumerable<int> result = new[] { -1 }; if(!string.IsNullOrEmpty(stringToSearchIn) && !string.IsNullOrEmpty(charsToSearchFor)) { #region Calculate the index according to what the user enterd var comparisonType = matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; int firstIndex = stringToSearchIn.IndexOf(charsToSearchFor, startIndex, comparisonType); int lastIndex = stringToSearchIn.LastIndexOf(charsToSearchFor, stringToSearchIn.Length - 1, comparisonType); if(direction == enIndexFinderDirection.RightToLeft) { result = RightToLeftIndexSearch(occurrence, firstIndex, lastIndex, stringToSearchIn, charsToSearchFor, comparisonType); } else { result = LeftToRightIndexSearch(occurrence, firstIndex, lastIndex, stringToSearchIn, charsToSearchFor, comparisonType); } #endregion } return result; }
public IEnumerable <int> FindIndex(string stringToSearchIn, string firstOccurrence, string charsToSearchFor, string direction, bool matchCase, string startIndex) { enIndexFinderOccurrence occurrence = enIndexFinderOccurrence.FirstOccurrence; enIndexFinderDirection dir = enIndexFinderDirection.LeftToRight; int startIdx; #region Set the enums according to the strings switch (firstOccurrence) { case "First Occurrence": occurrence = enIndexFinderOccurrence.FirstOccurrence; break; case "Last Occurrence": occurrence = enIndexFinderOccurrence.LastOccurrence; break; case "All Occurrences": occurrence = enIndexFinderOccurrence.AllOccurrences; break; } switch (direction) { case "Left to Right": dir = enIndexFinderDirection.LeftToRight; break; case "Right to Left": dir = enIndexFinderDirection.RightToLeft; break; } startIndex = !string.IsNullOrWhiteSpace(startIndex) ? startIndex : "0"; if (!int.TryParse(startIndex, out startIdx)) { throw new Exception("The start index specified was not a number."); } #endregion return(FindIndex(stringToSearchIn, occurrence, charsToSearchFor, dir, matchCase, startIdx)); }
public IEnumerable <int> FindIndex(string stringToSearchIn, enIndexFinderOccurrence occurrence, string charsToSearchFor, enIndexFinderDirection direction, bool matchCase, int startIndex) { IEnumerable <int> result = new[] { -1 }; if (!string.IsNullOrEmpty(stringToSearchIn) && !string.IsNullOrEmpty(charsToSearchFor)) { #region Calculate the index according to what the user enterd var comparisonType = matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; int firstIndex = stringToSearchIn.IndexOf(charsToSearchFor, startIndex, comparisonType); int lastIndex = stringToSearchIn.LastIndexOf(charsToSearchFor, stringToSearchIn.Length - 1, comparisonType); if (direction == enIndexFinderDirection.RightToLeft) { result = RightToLeftIndexSearch(occurrence, firstIndex, lastIndex, stringToSearchIn, charsToSearchFor, comparisonType); } else { result = LeftToRightIndexSearch(occurrence, firstIndex, lastIndex, stringToSearchIn, charsToSearchFor, comparisonType); } #endregion } return(result); }