Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/StringExtensions.cs @ 3962

Last change on this file since 3962 was 3937, checked in by epitzer, 15 years ago

Estimate or calculate StringBuffer sizes in advance, use iterator over string splits instead of arrays. (#1138)

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Text.RegularExpressions;
6
7namespace HeuristicLab.Persistence.Auxiliary {
8  /// <summary>
9  /// Extension methods for the <see cref="System.String"/> class.
10  /// </summary>
11  public static class StringExtensions {
12
13    /// <summary>
14    /// Enumeration over the substrings when split with a certain delimiter.
15    /// </summary>
16    /// <param name="s">The string.</param>
17    /// <param name="delimiter">The delimiter.</param>
18    /// <returns>An enumeration over the delimited substrings.</returns>
19    public static IEnumerable<string> EnumerateSplit(this string s, char delimiter) {
20      int startIdx = 0;
21      for (int i = 0; i < s.Length; i++) {
22        if (s[i] == delimiter) {
23          if (i > startIdx) {
24            yield return s.Substring(startIdx, i - startIdx);
25          }
26          startIdx = i+1;
27        }
28      }
29      if (startIdx < s.Length)
30        yield return s.Substring(startIdx, s.Length - startIdx);
31    }
32
33    /// <summary>
34    /// Enumeration over the substrings when split with a certain delimiter..
35    /// </summary>
36    /// <param name="s">The string.</param>
37    /// <param name="delimiter">The delimiter.</param>
38    /// <returns>An enumerator over the delimited substrings.</returns>
39    public static IEnumerator<string> GetSplitEnumerator(this string s, char delimiter) {
40      int startIdx = 0;
41      for (int i = 0; i < s.Length; i++) {
42        if (s[i] == delimiter) {
43          if (i > startIdx) {
44            yield return s.Substring(startIdx, i - startIdx);
45          }
46          startIdx = i + 1;
47        }
48      }
49      if (startIdx < s.Length)
50        yield return s.Substring(startIdx, s.Length - startIdx);
51    }
52  }
53}
Note: See TracBrowser for help on using the repository browser.