|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Function TrimSArray(SArray)
'Function
' Removes all blank elements from the beginning and end of a string array.
' Blank elements in the middle are not affected.
'Parameters
' SArray (String Array) = The string array to work with.
'Return (String Array)
' Returns the specified string array with all blank elements from the beginning And
' end of the array removed.
' If the specified array is empty, function returns an empty string array.
'Notes
' If an element contains any number of the following values and nothing else,
' it is considered blank:
' Space - chr(32)
' Tab - chr(9)
' Linefeed - chr(13)
' Carriage Return - chr(10)
'---
'Initiate variables
Dim FirstUsedElement 'The index of the first non-blank element in the array
Dim LastUsedElement 'The index of the last non-blank element in the Array
Dim ElmCount 'The number of elements in the specified array
Dim C 'General counter
Dim D 'Counter to track the current element in the return buffer
Dim R() 'Return buffer
'Default return value
TrimSArray = R
'Get the number of elements in the specified array
ElmCount = ElementCount(SArray)
'Trap: The specified string array is empty, exit now
If ElmCount = 0 Then Exit Function
'Locate the index of the first non-blank element in the Array
FirstUsedElement = -1 'Cock the value; If it's -1 after the loop, no non-blank elements exist
For C = 0 To ElmCount - 1
If Not IsWhitespace(SArray(C)) Then 'We found a non-blank line
FirstUsedElement = C 'Flag this as the first non-blank element
Exit For
End If
Next
'Trap: We emerged from the loop with -1 (no non-blank lines exist), exit now
If FirstUsedElement = -1 Then Exit Function
'Locate the index of the last non-blank element in the Array
LastUsedElement = -1 'Cock the value; If it's -1 after the loop, no non-blank elements exist
For C = ElmCount - 1 To 0 Step -1
If Not IsWhitespace(SArray(C)) Then 'We found a non-blank line
LastUsedElement = C 'Flag this as the last non-blank element
Exit For
End If
Next
'Now, transfer all elements between FirstUsedElement and LastUsedElement to the return buffer
D = 0 'Set this to the first element we're loading in the return buffer
For C = FirstUsedElement To LastUsedElement
ReDim Preserve R(D) 'Make room for the new line
R(D) = SArray(C) 'Transfer the line from the source array to the return buffer
D = D + 1 'Increment the return-buffer index
Next
'Return the return buffer
TrimSArray = R
End Function
|
|
|
|
|