The parameters called: includeFrom and includeUntil allow you to append the from and until fields to the result.
Usage:
Code Snippet
- // Example for one occurrences
- // This extracts 'Message Here' from this example chat log
- string message = "[12th July 2014] Message Here (Sent by Sean)".Substring(from: "]", until: "(", includeFrom: false, includeUntil: false);
- // Example for multiple occurances
- // This extracts all of the names which are enclosed between single quotes
- List<string> listOfNames = "'sean' random text 'dave' 355353 'jane' some text here 'tom'".SubstringAll(from: "'", until: "'");
End of Code Snippet
Code Snippet
- /// <summary>
- /// Returns a substring between two anchor strings
- /// If from is null - Start of the string until the Second anchor
- /// If until is null - First anchor to the end of the string is returned
- /// </summary>
- /// <param name="this">Input string</param>
- /// <param name="from">An optional string to search after</param>
- /// <param name="until">An optional string to search before</param>
- /// <param name="includeFrom">Include from in the result</param>
- /// <param name="includeUntil">Include until in the result</param>
- /// <param name="comparison">An optional comparison for the search</param>
- /// <returns>A substring based on the search</returns>
- public static string Substring(this string @this, string from = null, string until = null, bool includeFrom = false, bool includeUntil = false, StringComparison comparison = StringComparison.InvariantCulture)
- {
- var fromLength = (from ?? string.Empty).Length;
- var startIndex = !string.IsNullOrEmpty(from)
- ? @this.IndexOf(from, comparison) + fromLength
- : 0;
- if (startIndex < fromLength) { return string.Empty; }
- var endIndex = !string.IsNullOrEmpty(until)
- ? @this.IndexOf(until, startIndex, comparison)
- : @this.Length;
- if (endIndex < 0) { return string.Empty; }
- if (includeFrom) // Do these AFTER start and end have been calculated
- startIndex -= from.Length;
- if (includeUntil)
- endIndex += until.Length;
- return @this.Substring(startIndex, endIndex - startIndex);
- }
- /// <summary>
- /// Returns a List of substrings between two anchor strings
- /// If from is null - Start of the string until the Second anchor
- /// If until is null - First anchor to the end of the string is returned
- /// </summary>
- /// <param name="this">Input string</param>
- /// <param name="from">An optional string to search after</param>
- /// <param name="until">An optional string to search before</param>
- /// <param name="includeFrom">Include from in the result</param>
- /// <param name="includeUntil">Include until in the result</param>
- /// <param name="skipEmptyResults">If empty results are found, then they are not added to the result set.</param>
- /// <param name="comparison">An optional comparison for the search</param>
- /// <returns>A List of substrings based on the search</returns>
- public static List<string> SubstringAll(this string @this, string from = null, string until = null, bool includeFrom = false, bool includeUntil = false, bool skipEmptyResults = true, StringComparison comparison = StringComparison.InvariantCulture)
- {
- try
- {
- while (true)
- {
- string res = @this.SubstringEx(from, until, includeFrom, includeUntil, comparison);
- if (!skipEmptyResults || !string.IsNullOrEmpty(res)) // Skip empty result sets
- result.Add(res);
- res = (!includeFrom) ? from + res : res;
- // If from and until are the same, then we can end up with problems and only returning half the result set. This fixes that problem.
- if (from != until)
- res = (!includeUntil) ? res + until : res;
- @this = @this.Replace(res, string.Empty);
- }
- }
- catch { }
- return result;
- }
End of Code Snippet
No comments:
Post a Comment