Function String Manipulations Class

StringEveryNthCharacter

Steps forward through vString creating a string from all of the sub-strings of length vLength. Starts at character position vStep. If vTruncate is True, then the final sub-string will be truncated unless it is of length vLength.

Signature (Visual Basic / VBA)

Public Function StringEveryNthCharacter( _
      ByVal vString As Variant _
    , Optional ByVal vStart As Variant _
    , Optional ByVal vLength As Variant _
    , Optional ByVal vStep As Variant _
    , Optional ByVal vTruncate As Variant _
    ) As Variant

Code Example

    StringEveryNthCharacter("123456789", 2, 4, 5, False) = "2345789"
    StringEveryNthCharacter("123456789", 2, 4, 5, True) =  "2345"

Parameters

Parameter Description
vString The string whose sub-strings are to be sampled. Function returns Null if vString is Null or cannot be fixed up to a String.
vStep The number of character positions to move after extracting each sub-string. vStep defaults to 1 (one) if it is missing or Null or cannot be fixed up to a number. Function returns an empty string if the step is 0 (zero).
vLength The length of each sub-string that is to be extracted. vLength defaults to 1 (one) if it is missing or Null or cannot be fixed up to a number. Function returns an empty string if length is <= 0 (less than or equal to zero).
vStart The character position at which the extraction process is to begin. When vStart is missing or Null or non-numeric, it assumes a default value based upon the step direction. vStart defaults to 1 (one) if the step (vStep) is positive. vStart defaults to the position of the Nth from last (where N = vLength) character if the step (vStep) is negative.
vTruncate Determines whether the last partial sub-string is included in the result. vTruncate defaults to False if it is missing or Null or cannot be fixed up to a number. With vTruncate False, the last partial sub-string will be truncated/removed unless it is of length vLength. With vTruncate True, the last partial sub-string is always included within the result, no matter what its length.

Notes & Version History

Future possible problem with this function when the step is negative--should possibly copy from current position (lngCurPos) backwards, instead of lngCurPos forwards.