Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:50:12+00:00 2026-05-28T04:50:12+00:00

I have the following code: Function TruncateString(str, n) ‘ Returns an array with strings

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:50:12+00:00Added an answer on May 28, 2026 at 4:50 am

    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

    • Space [CHR(32)]
    • then into fixed lengths
    • then with any residual length carried over

      1. My preferred method uses a rexexp to break the string up immediately
      2. The second method runs 3 string manipulations and to me feels uglier and more complex
        • uses Split to separate the string into smaller text chunks using a space character as a delimiter
        • loops through each element of this array and breaks this up into fixed length chucks using Mid$
        • tests if any strings less than the desired fixed length chunks are left (with a Mod test), if so appends these partial strings to the final outcome

    Both functions return an array by splitting the final text, which I then have returned as a single string using Join in my master sub.

    Code

    Sub Test()
        Dim strIn As String
        Dim lngChks As Long
        strIn = Application.Rept("The quick fox jumped over the lazy dog", 2)
        lngChks = 2
        MsgBox Join(TruncateRegex(strIn, lngChks), vbNewLine)
        MsgBox Join(TruncateMid(strIn, lngChks), vbNewLine)
    End Sub
    

    1 – Regexp

    Function TruncateRegex(ByVal strIn, ByVal lngChks)
        Dim objRegex As Object
        Dim objRegMC As Object
        Dim objRegM As Object
        Dim strOut As String
        Dim lngCnt As Long
        Set objRegex = CreateObject("vbscript.regexp")
        With objRegex
            .Pattern = "[^\s]{1," & lngChks - 1 & "}(\s+|$)|[^\s]{" & lngChks & "}"
            .Global = True
            'test to avoid nulls
            If .Test(strIn) Then
                Set objRegMC = .Execute(strIn)
                For Each objRegM In objRegMC
                    'concatenate long string with (short string & short string)
                    strOut = strOut & (objRegM & vbNewLine)
                Next
            End If
        End With
        TruncateRegex = Split(strOut, vbNewLine)
    End Function
    

    2-String

    Function TruncateMid(ByVal strIn, ByVal lngChks)
        Dim arrVar
        Dim strOut As String
        Dim lngCnt As Long
        Dim lngCnt2 As Long
        'use spaces to delimit string array
        arrVar = Split(strIn, Chr(32))
        For lngCnt = LBound(arrVar) To UBound(arrVar)
            If Len(arrVar(lngCnt)) > 0 Then
                lngCnt2 = 0
                For lngCnt2 = 1 To Int(Len(arrVar(lngCnt)) / lngChks)
                    strOut = strOut & (Mid$(arrVar(lngCnt), (lngCnt2 - 1) * lngChks + 1, lngChks) & vbNewLine)
                Next
                'add remaining data at end of string < lngchks
                 If Len(arrVar(lngCnt)) Mod lngChks <> 0 Then strOut = strOut & (Mid$(arrVar(lngCnt), (lngCnt2 - 1) * lngChks + 1, Len(arrVar(lngCnt)) Mod lngChks) & vbNewLine)
            End If
        Next
        TruncateMid = Split(strOut, vbNewLine)
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In continuing my question from yesterday , I have the following code: function VAL(str)
I have following code function Model(onChanged) { this.array = new Array(); this.onChanged = onChanged;
I have the following code: function doSomething(str){ return str+=a; } function anotherFunction(str){ return str+=b;
Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
I have the following code: protected function safePath($path) { $path = (string) $path; $path
I currently have the following js code function clearMulti(option) { var i; var select
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
given i have the following block of code (function(){ var mb = { abc:function(){
I have the following code in my .Zshrc function google; { $VIEW http://www.google.com/search?q='url-encode ${(j:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.