-
Jun 9th, 2014, 05:36 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] vb Split function
Hi all,
I have met these days a string that contains a vbNullChar as delimiter each time when a user selects more than one file from a folder. Because the selected files are variable I want to put them within an array using for this purpose the well-known split function. The idea is whenever I try to split the string by vbNullChar I don't get the expected result. The replace function is a possibility to split the string properly but I wonder if vbNullChar cannot be handled directly using a certain conversion so that to be recognized as string delimiter within Split function. Thank you.
Code:
Dim nullPos As Byte, fileArr() As String, nFiles As String, sFile As String
nullPos = InStr(1, selfile, vbNullChar)
If nullPos > 0 Then 'multi-selection
nFiles = Trim$(Mid$(selfile, nullPos + 1))
nFiles = Replace(nFiles, vbNullChar, "|")
fileArr = Split(nFiles, "|")
End If
-
Jun 9th, 2014, 06:22 AM
#2
Re: vb Split function
what is "selfile"? And why do you think it contains a null?
-
Jun 9th, 2014, 06:24 AM
#3
Re: vb Split function
also, why are you dimming nullpos as byte, instead of an integer?
Once you answer the questions in post2, I'll rewrite the code you did.
-
Jun 9th, 2014, 06:26 AM
#4
Re: vb Split function
I've just tested the Split-Function here with NullChars - and it seems to work as it should.
Dim S As String
S = "A" & vbNullChar & "B" & vbNullChar & "C"
Dim SArr() As String
SArr = Split(S, vbNullChar)
Debug.Print UBound(SArr), SArr(0), SArr(1), SArr(2)
'gives:--> 2 A B C
So the Split-function can handle NullChar-delimiters apparently -
what problems exactly did you encounter with Split and NullChars -
In your above code I don't see any NullChar-Split-Job for the Split-function.
Olaf
-
Jun 9th, 2014, 08:14 AM
#5
Thread Starter
Hyperactive Member
Re: vb Split function
Hi,
Sam, "selfile" is a string received from an OpenDialog function as result of a user selection but I avoid to bring that class here for size reason. The nullPos variable is declared as byte because I knew it is likely to have lengths smaller than 255. Fortunately, after I saw and tested the Olaf's code lines I understood that is a misunderstanding on my side.
So fileArr = Split(nFiles, vbNullChar) does work as we expected.
Thank you both that you got my feet back on the ground and I am sorry for this confusion.
-
Jun 9th, 2014, 08:36 AM
#6
Re: vb Split function
Multiple files are usually separated by a vbNullChar and terminated with a double vbNullChar.
So one should first strip off the double vbNullChar at the end and then do a Split with vbNullChar.
Sample code to remove double vbNullChar would be something like this:
Code:
Private Function StripDblNull(StrIn As String) As String
Dim nul As Long
nul = InStr(StrIn, vbNullChar & vbNullChar)
If (nul) Then
StripDblNull = Left$(StrIn, nul - 1)
Else
StripDblNull = Trim$(StrIn)
End If
End Function
-
Jun 9th, 2014, 09:16 AM
#7
Thread Starter
Hyperactive Member
Re: vb Split function
Exactly, DrUnicode, my string had a double vbNullChar on the right side and that was the cause of my issue. Initially i thought it is a problem with the Split function when vbNullChar is chose as delimiter but after I saw Olaf's sample I realized that is a problem with my string. My Trim function is similar with yours but right now I am about to replace it with your function because it is possible some guys to prefer to hide the file extension (from Windows settings) ...
Code:
Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStrRev(strItem, ".")
If intPos > 0 Then
TrimNull = Left(strItem, intPos + 3)
Else
TrimNull = strItem
End If
End Function
Other weakness would be what happens when the file extension has more or fewer than 3 characters ...
-
Jun 9th, 2014, 10:15 AM
#8
Re: vb Split function
Why not use the Replace function instead?
You could replace all double nulls with a single null with one line of code and not need to worry about the length, extension or number of double nulls contained.
-
Jun 9th, 2014, 10:16 AM
#9
Re: vb Split function
Other weakness would be what happens when the file extension has more or fewer than 3 characters ...
I believe the file extensions are alway included in the filename(s) string returned from Common Dialog and that is only an Explorer option whereby you can elect to display Filenames w/o extension.
-
Jun 9th, 2014, 10:23 AM
#10
Re: vb Split function
Why not use the Replace function instead?
You could replace all double nulls with a single null with one line of code and not need to worry about the length, extension or number of double nulls contained.
There are 2 problems with this:
1. The string will retain the junk after the DblNull and you will get a junk item as last item after Split.
2. You will get an extra empty item after Split when there is a single vbNull at the end of string.
-
Jun 9th, 2014, 11:08 AM
#11
Thread Starter
Hyperactive Member
Re: vb Split function
Originally Posted by DataMiser
Why not use the Replace function instead?
You could replace all double nulls with a single null with one line of code and not need to worry about the length, extension or number of double nulls contained.
Let's say a user does select 3 files from a folder using an OpenDialog function. The string returned should be something like this: selFile = App.Path & "\File1" & vbNullChar & "File2" & vbNullChar & "File3" & vbNullChar & vbNullChar. It seems those double nulls stay at the end of the string as DrUnicode noted and if we replace them with a single null, as you suggested, we will have the code below
Code:
Private Sub Form_Load()
Dim selFile As String, k As Long, fileArr() As String
selFile = App.Path & "\File1" & vbNullChar & "File2" & vbNullChar & "File3" & vbNullChar & vbNullChar
selFile = Trim$(Replace(selFile, vbNullChar & vbNullChar, vbNullChar))
fileArr = Split(selFile, vbNullChar)
k = UBound(fileArr) 'is 3 but should be 2 ...
End Sub
Maybe a solution would be to use in replacing the vbNullString but a bit risky considering the double null could appear in other positions inside the string (not at the end only.
-
Jun 9th, 2014, 11:22 AM
#12
Re: vb Split function
Maybe a solution would be to use in replacing the vbNullString but a bit risky considering the double null could appear in other positions inside the string (not at the end only.
Bear in mind that you probably dimensioned the string large enough to include all the expected Filenames. So you need to dump not only the first DblNull but everything after it. That is handled in Function StripDblNull at http://www.vbforums.com/showthread.p...=1#post4696371
-
Jun 9th, 2014, 04:12 PM
#13
Thread Starter
Hyperactive Member
Re: vb Split function
Thank you very much DrUnicode for your suggestions, I will consider this thread resolved.
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
|