Posts by OscarGunther

    Here are the performance data, for the sake of completeness. First, the character-counting code:

    In the code above, Case 1 iterates through each character in the string, incrementing a counter if the character matches strFind. Case 2 replaces each instance of strFind in strText and compares the length of the original strText with the altered version. Case 3 splits the string into an array, using strFind as the boundary character.

    Here is the test harness (TimerStart, TimerStop, and ElapsedTime are my high-resolution timer functions and not included here for the sake of brevity; feel free to use your own):

    An average of test results (in seconds) for 100,000 iterations for each counting method are:

    Method 1: 0.585

    Method 2: 0.4672

    Method 3: 0.432

    Method 3 is 7.5% faster than Method 2 and 26.2% faster than Method 1. Your individual test results will vary, but the relationships between each method should hold.

    It appears that the fastest way to count the instances of a given character in a string is to use the SPLIT and UBOUND functions (Method 3).