-
Nov 8th, 2014, 01:35 PM
#1
Thread Starter
PowerPoster
DEVIDE string in 4 parts
PIAZZA LEVI, 11 - 15011 ACQUI TERME (AL)
PIAZZA DELLA LIBERTA', 6 - 15100 ALESSANDRIA (AL)
VIALE INDIPENDENZA, 63 - 63100 ASCOLI PICENO (AP)
thi sis a lines in a txt file.
i need to devide the intire string in 4 parts...
example for first line:
var1="PIAZZA LEVI, 11" (variable lenght)
vat2="15011" (is a fixed lenght 5, always)
var3="ACQUI TERME" (variable lenght)
var4="(AL)" (is a fixed format(XX), always)
Tks.
note
the simbol "-" is always in string used to delimit var1 and the other part of string
Last edited by luca90; Nov 8th, 2014 at 01:39 PM.
-
Nov 8th, 2014, 02:39 PM
#2
Re: DEVIDE string in 4 parts
Brute force?
Code:
Option Explicit
Private Function Splitter(ByVal Text As String) As String()
Dim Results() As String
Dim I As Long
Dim J As Long
ReDim Results(2)
I = 1
J = InStr(I, Text, "-")
If J > 0 Then
Results(0) = Trim$(Left$(Text, J - 1))
I = J + 1
Do While I > 0 And Mid$(Text, I, 1) = " "
I = I + 1
Loop
If I > 0 Then
J = InStr(I, Text, " ")
If J > 0 Then
Results(1) = Trim$(Mid$(Text, I, J - I))
I = J + 1
J = InStr(I, Text, "(")
If J > 0 Then
I = J
J = InStr(I + 1, Text, ")")
If J > 0 Then
Results(2) = Mid$(Text, I, J - I + 1)
End If
End If
End If
End If
End If
Splitter = Results
End Function
Private Sub Form_Load()
Font.Name = "Courier New"
Font.Size = 12
AutoRedraw = True
Print Join$(Splitter("PIAZZA LEVI, 11 - 15011 ACQUI TERME (AL)"), "•")
Print Join$(Splitter("PIAZZA DELLA LIBERTA', 6 - 15100 ALESSANDRIA (AL)"), "•")
Print Join$(Splitter("VIALE INDIPENDENZA, 63 - 63100 ASCOLI PICENO (AP)"), "•")
End Sub
I left out capturing the 3rd part to leave you something to do!
-
Nov 8th, 2014, 03:23 PM
#3
Re: DEVIDE string in 4 parts
Originally Posted by luca90
PIAZZA LEVI, 11 - 15011 ACQUI TERME (AL)
PIAZZA DELLA LIBERTA', 6 - 15100 ALESSANDRIA (AL)
VIALE INDIPENDENZA, 63 - 63100 ASCOLI PICENO (AP)
thi sis a lines in a txt file.
i need to devide the intire string in 4 parts...
example for first line:
var1="PIAZZA LEVI, 11" (variable lenght)
vat2="15011" (is a fixed lenght 5, always)
var3="ACQUI TERME" (variable lenght)
var4="(AL)" (is a fixed format(XX), always)
Tks.
note
the simbol "-" is always in string used to delimit var1 and the other part of string
Example PIAZZA LEVI, 11 - 15011 ACQUI TERME (AL)
Note: I am assuming you have 3 text lines in the file because you use the plural lines
Split on the "-"
Dim s() As String
s = Split(Data, "-") where Data is the example line
now you have "PIAZZA LEVI, 11 " in s(0) and " 15011 ACQUI TERME (AL)" in s(1)
Var(1) = Trim(s(0))
Var(2) = Trim(Left(s(1), 6))
Var(4) = Right(s(1), 4)
q = InStr(1, s(1), "(")
Var(3) = Mid(s(1), 8, q - 9)
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 8th, 2014, 03:55 PM
#4
Thread Starter
PowerPoster
Re: DEVIDE string in 4 parts
tks Dilettante & jmsrickland i canot test now, but tomorrow
-
Nov 9th, 2014, 08:41 AM
#5
Re: DEVIDE string in 4 parts
My approach is pretty close to JMs. Using a listbox instead of pulling from a file.
Code:
Option Explicit
Private Sub Command1_Click()
Dim strInput As String
Dim strValues() As String
strInput = List1.List(List1.ListIndex)
If strInput <> "" Then
strValues = SplitLine(strInput)
Debug.Print Join(strValues, vbCrLf)
Debug.Print
End If
End Sub
Private Function SplitLine(ByVal strInput As String) As String()
Dim strParts(3) As String
Dim strTemp() As String
strTemp = Split(strInput, "-")
'Get the value before the -
strParts(0) = Trim(strTemp(0))
'Clean up the rest of the string
strParts(2) = Trim(strTemp(1))
' Get the 5 character value
strParts(1) = Left(strParts(2), 5)
' Get the last value
strParts(3) = Right(strParts(2), 4)
' Get the variable length part
strParts(2) = Trim(Replace(Replace(strParts(2), strParts(1), ""), strParts(3), ""))
' ' The easier to read version of the previous line
' strParts(2) = Replace(strParts(2), strParts(1), "")
' strParts(2) = Replace(strParts(2), strParts(3), "")
' strParts(2) = Trim(strParts(2))
SplitLine = strParts
End Function
Private Sub Form_Load()
List1.AddItem "PIAZZA LEVI, 11 - 15011 ACQUI TERME (AL)"
List1.AddItem "PIAZZA DELLA LIBERTA', 6 - 15100 ALESSANDRIA (AL)"
List1.AddItem "VIALE INDIPENDENZA, 63 - 63100 ASCOLI PICENO (AP)"
End Sub
-
Nov 9th, 2014, 09:49 AM
#6
Re: DEVIDE string in 4 parts
Personally, I would use a custom designed function that would result in the more readable code at parse time.
The one presented here is part of my own toolbox, but modified slightly to add a special case to handle the last part of your parsing needs where you want to keep the string between parenthesis
Code:
Function SeparateTrim(astring$, delimiter$, Optional nondestructive)
xpos = InStr(astring$, delimiter$)
If xpos Then
SeparateTrim = Trim$(Left$(astring$, xpos - 1))
If IsMissing(nondestructive) Then
astring$ = Trim$(Right$(astring$, Len(astring$) - xpos - Len(delimiter$) + 1))
Else
astring$ = Trim$(Right$(astring$, Len(astring$) - xpos + 1))
End If
Else
SeparateTrim = Trim$(astring$)
astring$ = ""
End If
End Function
Your code can go like this :
Code:
yourstring$ = "PIAZZA DELLA LIBERTA', 6 - 15100 ALESSANDRIA (AL)"
Rem work on a copy
a$ = yourstring$
part1$ = SeparateTrim(a$, "-")
part2$ = SeparateTrim(a$, " ")
part3$ = SeparateTrim(a$, "(", 1)
part4$ = a$
Rem optional : show the result$
tk1$ = tk1$ & part1$ & vbCrLf
tk1$ = tk1$ & part2$ & vbCrLf
tk1$ = tk1$ & part3$ & vbCrLf
tk1$ = tk1$ & part4$ & vbCrLf
MsgBox tk1$
-
Nov 9th, 2014, 12:31 PM
#7
Re: DEVIDE string in 4 parts
Just to throw in a quick hint; you don't need to specify a single character as the the delimiter when using the Split function. So, for posts #3 and #5, you can use
s = Split(Data, " - ")
and
strTemp = Split(strInput, " - ")
respectively. Then you don't need to mess around so much with the Trim function.
HTH...
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Nov 9th, 2014, 01:00 PM
#8
Re: DEVIDE string in 4 parts
Very true if you can guarantee that the delimiter is always going to be space dash space. I opted to split on the dash alone just in case it was possible that a line my not always have a space before and after the dash. It just seemed safer.
-
Nov 9th, 2014, 01:37 PM
#9
Re: DEVIDE string in 4 parts
That was my thinking also. I felt it better to use the "-" only and the Trim() rather than wind up with a possible
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 9th, 2014, 04:14 PM
#10
Re: DEVIDE string in 4 parts
Originally Posted by MarkT
Very true if you can guarantee that the delimiter is always going to be space dash space. I opted to split on the dash alone just in case it was possible that a line my not always have a space before and after the dash. It just seemed safer.
Just pointing it out. Although, saying that, both #3 and #5 seem perfectly happy to assume that a dash can never appear within a field's contents which is no less valid as a blanket assumption!
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Nov 9th, 2014, 04:29 PM
#11
Re: DEVIDE string in 4 parts
You have to make a conclusion (or assumption) somewhere and the best we have is what the OP says and I have to base my solutions based on what OP says and if it turns out to be different then back to the drawing board.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 9th, 2014, 04:43 PM
#12
Re: DEVIDE string in 4 parts
Originally Posted by ColinE66
Just pointing it out. Although, saying that, both #3 and #5 seem perfectly happy to assume that a dash can never appear within a field's contents which is no less valid as a blanket assumption!
Originally Posted by jmsrickland
You have to make a conclusion (or assumption) somewhere and the best we have is what the OP says and I have to base my solutions based on what OP says and if it turns out to be different then back to the drawing board.
Originally Posted by luca90
note
the simbol "-" is always in string used to delimit var1 and the other part of string
OP says a dash will always delimit var1. I don't see any blanket assumptions made except that the delimitor should be " - " in post # 7 instead of "-". The code posted in #3 and #5 would work with either. The code would fail if you were splitting on " - " and the input was any of the following
PIAZZA LEVI, 11- 15011 ACQUI TERME (AL)
PIAZZA LEVI, 11 -15011 ACQUI TERME (AL)
PIAZZA LEVI, 11-15011 ACQUI TERME (AL)
-
Nov 9th, 2014, 06:53 PM
#13
Re: DEVIDE string in 4 parts
Yes, the OP says that "-" delimits field 1 from the rest but that doesn't mean that "-" won't appear in vars 2,3 or 4 does it? In which case #3 and #5 will not work without further code. Personally, if I had customer telling me that the format was as described in the OP, I'd ask more questions before suggesting a solution that could be 'undone' by a sin of omission! The only thing that is categorically stated in the OP is that the first occurrence of a "'-" denotes the end of the first field but I would challenge even that assumption before suggesting a solution...
Like I say, all I was doing was pointing out that the delimiter in the Split function needn't be a single char, since I thought that might be helpful (or even unknown to some)...
Last edited by ColinE66; Nov 9th, 2014 at 07:05 PM.
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Nov 9th, 2014, 08:24 PM
#14
Re: DEVIDE string in 4 parts
Originally Posted by ColinE66
Like I say, all I was doing was pointing out that the delimiter in the Split function needn't be a single char, since I thought that might be helpful (or even unknown to some)...
Point taken. I guess when you have been working with VB6 for as long as some of us have, it's hard sometimes to realize some haven't learned about some of the common vb functions and how to use them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|