I have the following code:
Function TruncateString(str, n)
' Returns an array with strings no more than n char long, truncated at spaces
Dim truncatedArr() As String
If str <> "" Then
str = remove_spaces_left(str)
For i = 0 To (CLng(Len(str) / n))
Index = InStrRev(Left(str, n), " ")
ReDim Preserve truncatedArr(i)
truncatedArr(i) = Left(str, Index)
If Right(truncatedArr(i), 1) = " " Then truncatedArr(i) = Left(truncatedArr(i), Len(truncatedArr(i)) - 1)
str = Right(str, Len(str) - Index)
Next i
End If
TruncateString = truncatedArr
End Function
My question is what is the value returned by the function when str is empty? I have a type compatibility issue when I do
arr = TruncateString (text,15)
arr is defined like this:
dim arr() as string
please let me know if more info is needed for an answer. Thanks
I thought this was an interesting coding task, below are two attempts of mine to write a more efficient function that “chops up” a large string by
then with any residual length carried over
rexexpto break the string up immediatelySplitto separate the string into smaller text chunks using a space character as a delimiterMid$Modtest), if so appends these partial strings to the final outcomeBoth functions return an array by splitting the final text, which I then have returned as a single string using
Joinin my master sub.Code
1 – Regexp
2-String