split string (1 Viewer)

cpampas

Registered User.
Local time
Today, 09:42
Joined
Jul 23, 2012
Messages
218
Hello,
I am trying to split a string into words, with the condition that each word has at least 3 characters, something like this :

str= "AAAAA BB CCC D"
sWords = Split(str, " ")

I am looking for this result :

sWords(0)="AAAAA"
sWords(1)="CCC"

Any thoughts ?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:42
Joined
Oct 29, 2018
Messages
21,454
Hi. You will probably have to create a custom function to do that. One possible approach is to split the input, examine each item and then build a new string or array without the items with less than 3 chars.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:42
Joined
May 21, 2018
Messages
8,525
Code:
Public Sub Test()
  Debug.Print Split2("aaa bb cccc")(1)
End Sub
Public Function Split2(TheString As String, Optional TheLength As Integer = 3, Optional delimiter As String = " ") As String()
  'Returns an array of strings that meet the length requirement
  Dim aStr() As String
  Dim aTemp() As String
  Dim I As Integer
  Dim counter As Integer
  aStr() = Split(TheString, delimiter)
  ReDim aTemp(UBound(aStr))
  For I = 0 To UBound(aStr)
    If Len(aStr(I)) >= TheLength Then
      aTemp(counter) = aStr(I)
      counter = counter + 1
    End If
  Next I
  ReDim Preserve aTemp(counter - 1)
  Split2 = aTemp
End Function
 

cpampas

Registered User.
Local time
Today, 09:42
Joined
Jul 23, 2012
Messages
218
It works great, thanks a lot for your help:):)
 

Users who are viewing this thread

Top Bottom