Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract 4 characters of each word in a multiple word field

Status
Not open for further replies.

blurworld

Programmer
Sep 19, 2001
46
0
0
GB
Hi

How do i ....
Extract the first 4 characters of each word in a multiple word field i.e.

If i had a name DAVID JONES
I want it to become.... DAVIJONE
or FOX TELEVISION would become FOXTELE

Thanx for your help!

-Martin
 
This may work - I haven't tested it though. The function takes your original string and returns a modified version:

Code:
Function fctnTakeFour(strText As String) As String
'First make sure that all mulitple spaces are reduced to one space
Do Until InStr(1, strText, "  ", vbTextCompare) = 0
    strText = Replace(strText, "  ", " ", , , vbTextCompare)
Loop
'Then take the first four chars of each word.
Dim lngSpace As Long
Do Until InStr(1, strText, " ", vbTextCompare) = 0
    lngSpace = InStr(1, strText, " ", vbTextCompare)
    If lngSpace > 4 Then
        fctnTakeFour = fctnTakeFour & Left$(strText, 4)
    Else
        fctnTakeFour = fctnTakeFour & Left$(strText, lngSpace - 1)
    End If
    strText = Mid$(strText, lngSpace + 1)
Loop
'Finally allow for leftover text after the last space.
If Len(strText) > 0 Then fctnTakeFour = fctnTakeFour & Left$(strText, 4)
End Function

[pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top