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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hope someone can help how can i 2

Status
Not open for further replies.

GrimR

IS-IT--Management
Jun 17, 2007
1,149
ZA
Hope someone can help

how can i add ; in cetain places
e.g. numbers (1 to 4 digits)(ADD;) String text can be any length also contain &+ etc characters)(ADD;) (Price in 0:20 to 1450:00 formatt)(ADD;)(more String Txt)(ADD;) another digit

1 Coke 20 Stock 0
1; Coke; 20; Stock; 0​
234 Fanta Grape & Ice Cream 65.20 Room 2
234; Fanta Grape & Ice Cream; 65.20; Room; 2​
etc

MCSE NT to 2012, MCITP:EA/SA, MCSA, MCDBA, MCTS, MCP+I, MCP
 
Hi,
Code:
Function SpecialParse(sText As String, Optional DELIM As String = ";") As String
    Dim a As Variant, ia As Integer
    Const SPACE = " "
'1 Coke 20 Stock 0
    a = Split(sText, SPACE)
    
    SpecialParse = a(LBound(a))
    
    If IsNumeric(a(LBound(a))) Then
        SpecialParse = SpecialParse & DELIM & SPACE
    Else
        SpecialParse = SpecialParse & SPACE
    End If
    
    For ia = LBound(a) + 1 To UBound(a) - 1
        Select Case IsNumeric(a(ia))
            Case True
                SpecialParse = SpecialParse & DELIM & SPACE & a(ia) & DELIM
            Case Else
                SpecialParse = SpecialParse & SPACE & a(ia)
        End Select
    Next
    
    If IsNumeric(a(UBound(a))) Then
        SpecialParse = SpecialParse & DELIM & SPACE & a(ia)
    Else
        SpecialParse = SpecialParse & SPACE & a(ia)
    End If
End Function

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
A moment of boredom had me create a (slightly clunky) regular expressions solution:

Code:
[blue]Public Function SpecialParse(sText As String, Optional DELIM As String = ";") As String
    With CreateObject("vbscript.regexp") 
        .Pattern = "(\d+(?:\.\d+)?)\s+|(\w+)\s+(?=[\d\s])"
        .Global = True
        SpecialParse = .Replace(sText, "$1$2" & DELIM & " ")
    End With
End Function[/blue]
 
Anyone home?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top