Extracting filenames from a URL by splitting a string
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am used to accessing items from an array, returned by the Split
function, directly. This is lazy, I know. Usually, I know the element I want and I say something like:
Debug.Print Split(href, "/")(0)
Questions:
1) Can I do something similar to access the UBound of the array returned?
2) What is the "best practice" way of doing things and why?
Code:
I wrote the following but it looks messy.
Sub Testing()
Dim href As String
Dim fileName As String
href = "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/20180111-AmbSYS-post-ARP-month-of-December-2017v2.xlsx"
fileName = Trim$(Split(href, "/")(UBound(Split(href, "/"))))
End Sub
I saw from here that I can also do:
Debug.Print Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))
Again, messy.
I know that I can assign to an array variable and then access the UBound that way. It looks tidier but is essentially the same thing; i.e.
Dim myArr() As String
myArr() = Split(href, "/")
fileName = myArr(UBound(myArr))
array vba
add a comment |Â
up vote
1
down vote
favorite
I am used to accessing items from an array, returned by the Split
function, directly. This is lazy, I know. Usually, I know the element I want and I say something like:
Debug.Print Split(href, "/")(0)
Questions:
1) Can I do something similar to access the UBound of the array returned?
2) What is the "best practice" way of doing things and why?
Code:
I wrote the following but it looks messy.
Sub Testing()
Dim href As String
Dim fileName As String
href = "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/20180111-AmbSYS-post-ARP-month-of-December-2017v2.xlsx"
fileName = Trim$(Split(href, "/")(UBound(Split(href, "/"))))
End Sub
I saw from here that I can also do:
Debug.Print Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))
Again, messy.
I know that I can assign to an array variable and then access the UBound that way. It looks tidier but is essentially the same thing; i.e.
Dim myArr() As String
myArr() = Split(href, "/")
fileName = myArr(UBound(myArr))
array vba
@Donald.McLean Neither myself or IvenBach bothered to point out the inconsistency in the variable name. So I agree with the OP that the variable name should fixed.
â user109261
Jan 25 at 19:42
@ThomasInzina We have a bot scanning for possible answer invalidations, sometimes a question is rolled back when it's not necessary, I agree that no rollback was needed this time and therefore rolled it back again.
â Simon Forsbergâ¦
Jan 25 at 21:33
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am used to accessing items from an array, returned by the Split
function, directly. This is lazy, I know. Usually, I know the element I want and I say something like:
Debug.Print Split(href, "/")(0)
Questions:
1) Can I do something similar to access the UBound of the array returned?
2) What is the "best practice" way of doing things and why?
Code:
I wrote the following but it looks messy.
Sub Testing()
Dim href As String
Dim fileName As String
href = "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/20180111-AmbSYS-post-ARP-month-of-December-2017v2.xlsx"
fileName = Trim$(Split(href, "/")(UBound(Split(href, "/"))))
End Sub
I saw from here that I can also do:
Debug.Print Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))
Again, messy.
I know that I can assign to an array variable and then access the UBound that way. It looks tidier but is essentially the same thing; i.e.
Dim myArr() As String
myArr() = Split(href, "/")
fileName = myArr(UBound(myArr))
array vba
I am used to accessing items from an array, returned by the Split
function, directly. This is lazy, I know. Usually, I know the element I want and I say something like:
Debug.Print Split(href, "/")(0)
Questions:
1) Can I do something similar to access the UBound of the array returned?
2) What is the "best practice" way of doing things and why?
Code:
I wrote the following but it looks messy.
Sub Testing()
Dim href As String
Dim fileName As String
href = "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/20180111-AmbSYS-post-ARP-month-of-December-2017v2.xlsx"
fileName = Trim$(Split(href, "/")(UBound(Split(href, "/"))))
End Sub
I saw from here that I can also do:
Debug.Print Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))
Again, messy.
I know that I can assign to an array variable and then access the UBound that way. It looks tidier but is essentially the same thing; i.e.
Dim myArr() As String
myArr() = Split(href, "/")
fileName = myArr(UBound(myArr))
array vba
edited Jan 25 at 21:31
Simon Forsbergâ¦
48.2k7124283
48.2k7124283
asked Jan 25 at 15:25
QHarr
1749
1749
@Donald.McLean Neither myself or IvenBach bothered to point out the inconsistency in the variable name. So I agree with the OP that the variable name should fixed.
â user109261
Jan 25 at 19:42
@ThomasInzina We have a bot scanning for possible answer invalidations, sometimes a question is rolled back when it's not necessary, I agree that no rollback was needed this time and therefore rolled it back again.
â Simon Forsbergâ¦
Jan 25 at 21:33
add a comment |Â
@Donald.McLean Neither myself or IvenBach bothered to point out the inconsistency in the variable name. So I agree with the OP that the variable name should fixed.
â user109261
Jan 25 at 19:42
@ThomasInzina We have a bot scanning for possible answer invalidations, sometimes a question is rolled back when it's not necessary, I agree that no rollback was needed this time and therefore rolled it back again.
â Simon Forsbergâ¦
Jan 25 at 21:33
@Donald.McLean Neither myself or IvenBach bothered to point out the inconsistency in the variable name. So I agree with the OP that the variable name should fixed.
â user109261
Jan 25 at 19:42
@Donald.McLean Neither myself or IvenBach bothered to point out the inconsistency in the variable name. So I agree with the OP that the variable name should fixed.
â user109261
Jan 25 at 19:42
@ThomasInzina We have a bot scanning for possible answer invalidations, sometimes a question is rolled back when it's not necessary, I agree that no rollback was needed this time and therefore rolled it back again.
â Simon Forsbergâ¦
Jan 25 at 21:33
@ThomasInzina We have a bot scanning for possible answer invalidations, sometimes a question is rolled back when it's not necessary, I agree that no rollback was needed this time and therefore rolled it back again.
â Simon Forsbergâ¦
Jan 25 at 21:33
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Abstract the implementation into a function. This makes it easy to catch mistakes if you want to use this capability in more than one location. GetUpperBoundElemenentFromDelimitedString
takes the required arguments you give it and returns the UBound
result. The name may be cumbersome but typing getup
and then pressing Ctrl+J will 'List Properties/Methods', the same going to Edit and choosing that option.
Public Function GetUpperBoundElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
Dim temp As Variant
temp = Split(inputValue, delimiter, compare:=compare)
GetUpperBoundElemenentFromDelimitedString = temp(UBound(temp))
End Function
Now when you are using it the descriptive name lets you immediately know what's happening.
Public Sub Foo()
Dim bar As String
bar = "This,is,going,to,return,an,element,at,a,specific,position."
Debug.Print GetUpperBoundElemenentFromDelimitedString(bar, ",")
End Sub
The same could be done for a specific position
Public Function GetArrayElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, ByVal zeroBasedPosition As Long, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
GetArrayElemenentFromDelimitedString = Split(inputValue, delimiter, compare:=compare)(zeroBasedPosition)
End Function
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
1
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
1
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
 |Â
show 6 more comments
up vote
2
down vote
If you need to use the Array then then this is the best practice.
Dim FileName As String, myArr() As String
myArr() = Split(href, "/")
FileName = myArr(UBound(myArr))
Here are several different ways of returning the substring after the last delimiter and the time that it takes to run each 1 million times:
FileName = Right(href, Len(href) - InStrRev(href, "/")) ' 1.25 seconds
FileName = Mid(href, InStrRev(href, "/") + 1) ' 1.29 seconds
FileName = Split(href, "/")(UBound(Split(href, "/"))) '8.93 seconds
myArr() = Split(href, "/"):FileName = myArr(UBound(myArr)) ' 8.71 seconds
FileName = Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))' 9.1 seconds
Notice that InstrRev is almost 7 times faster than using the Split methods.
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Abstract the implementation into a function. This makes it easy to catch mistakes if you want to use this capability in more than one location. GetUpperBoundElemenentFromDelimitedString
takes the required arguments you give it and returns the UBound
result. The name may be cumbersome but typing getup
and then pressing Ctrl+J will 'List Properties/Methods', the same going to Edit and choosing that option.
Public Function GetUpperBoundElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
Dim temp As Variant
temp = Split(inputValue, delimiter, compare:=compare)
GetUpperBoundElemenentFromDelimitedString = temp(UBound(temp))
End Function
Now when you are using it the descriptive name lets you immediately know what's happening.
Public Sub Foo()
Dim bar As String
bar = "This,is,going,to,return,an,element,at,a,specific,position."
Debug.Print GetUpperBoundElemenentFromDelimitedString(bar, ",")
End Sub
The same could be done for a specific position
Public Function GetArrayElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, ByVal zeroBasedPosition As Long, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
GetArrayElemenentFromDelimitedString = Split(inputValue, delimiter, compare:=compare)(zeroBasedPosition)
End Function
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
1
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
1
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
 |Â
show 6 more comments
up vote
4
down vote
accepted
Abstract the implementation into a function. This makes it easy to catch mistakes if you want to use this capability in more than one location. GetUpperBoundElemenentFromDelimitedString
takes the required arguments you give it and returns the UBound
result. The name may be cumbersome but typing getup
and then pressing Ctrl+J will 'List Properties/Methods', the same going to Edit and choosing that option.
Public Function GetUpperBoundElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
Dim temp As Variant
temp = Split(inputValue, delimiter, compare:=compare)
GetUpperBoundElemenentFromDelimitedString = temp(UBound(temp))
End Function
Now when you are using it the descriptive name lets you immediately know what's happening.
Public Sub Foo()
Dim bar As String
bar = "This,is,going,to,return,an,element,at,a,specific,position."
Debug.Print GetUpperBoundElemenentFromDelimitedString(bar, ",")
End Sub
The same could be done for a specific position
Public Function GetArrayElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, ByVal zeroBasedPosition As Long, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
GetArrayElemenentFromDelimitedString = Split(inputValue, delimiter, compare:=compare)(zeroBasedPosition)
End Function
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
1
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
1
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
 |Â
show 6 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Abstract the implementation into a function. This makes it easy to catch mistakes if you want to use this capability in more than one location. GetUpperBoundElemenentFromDelimitedString
takes the required arguments you give it and returns the UBound
result. The name may be cumbersome but typing getup
and then pressing Ctrl+J will 'List Properties/Methods', the same going to Edit and choosing that option.
Public Function GetUpperBoundElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
Dim temp As Variant
temp = Split(inputValue, delimiter, compare:=compare)
GetUpperBoundElemenentFromDelimitedString = temp(UBound(temp))
End Function
Now when you are using it the descriptive name lets you immediately know what's happening.
Public Sub Foo()
Dim bar As String
bar = "This,is,going,to,return,an,element,at,a,specific,position."
Debug.Print GetUpperBoundElemenentFromDelimitedString(bar, ",")
End Sub
The same could be done for a specific position
Public Function GetArrayElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, ByVal zeroBasedPosition As Long, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
GetArrayElemenentFromDelimitedString = Split(inputValue, delimiter, compare:=compare)(zeroBasedPosition)
End Function
Abstract the implementation into a function. This makes it easy to catch mistakes if you want to use this capability in more than one location. GetUpperBoundElemenentFromDelimitedString
takes the required arguments you give it and returns the UBound
result. The name may be cumbersome but typing getup
and then pressing Ctrl+J will 'List Properties/Methods', the same going to Edit and choosing that option.
Public Function GetUpperBoundElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
Dim temp As Variant
temp = Split(inputValue, delimiter, compare:=compare)
GetUpperBoundElemenentFromDelimitedString = temp(UBound(temp))
End Function
Now when you are using it the descriptive name lets you immediately know what's happening.
Public Sub Foo()
Dim bar As String
bar = "This,is,going,to,return,an,element,at,a,specific,position."
Debug.Print GetUpperBoundElemenentFromDelimitedString(bar, ",")
End Sub
The same could be done for a specific position
Public Function GetArrayElemenentFromDelimitedString(ByVal inputValue As String, ByVal delimiter As String, ByVal zeroBasedPosition As Long, Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbTextCompare) As String
GetArrayElemenentFromDelimitedString = Split(inputValue, delimiter, compare:=compare)(zeroBasedPosition)
End Function
edited Jan 25 at 17:15
answered Jan 25 at 17:09
IvenBach
907314
907314
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
1
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
1
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
 |Â
show 6 more comments
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
1
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
1
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
I have accepted this as I think it will be useful over the longer term.
â QHarr
Jan 26 at 10:25
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Creating methods from raw code will help in abstracting your code and make it clearer what's going on. A good rule of thumb for me is if a comment is needed to tell you what is going on it's time to refactor that usage into a respective Sub/Function.
â IvenBach
Jan 26 at 20:22
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
Thank you. I am currently getting ready to post my next item for review. I have tried, with limited success IMHO, to do some abstraction. I have thirty subs/functions in the code I hope to have reviewed. Admittedly quite a few are very short. Are there guidelines on how much code you can post on this site? Largest around 44 lines, average under 10 maybe.
â QHarr
Jan 26 at 20:31
1
1
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
@QHarr please disregard IvenBach's recommendation to post a MCVE on this site - MCVE's are great for Stack Overflow, but the devil is in the details, and reviewing examplified code tends to, well, end up closed as off-topic "hypothetical/example code".
â Mathieu Guindon
Jan 26 at 21:03
1
1
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
@QHarr CR has double the post size capacity of any other SE site, including Stack Overflow ;-) rule of thumb, if it fits within ~60K characters and it's reasonably described, then it's fine. Basically, the paradigm is entirely different on this site, vs. on SO; see this meta for more information.
â Mathieu Guindon
Jan 26 at 21:37
 |Â
show 6 more comments
up vote
2
down vote
If you need to use the Array then then this is the best practice.
Dim FileName As String, myArr() As String
myArr() = Split(href, "/")
FileName = myArr(UBound(myArr))
Here are several different ways of returning the substring after the last delimiter and the time that it takes to run each 1 million times:
FileName = Right(href, Len(href) - InStrRev(href, "/")) ' 1.25 seconds
FileName = Mid(href, InStrRev(href, "/") + 1) ' 1.29 seconds
FileName = Split(href, "/")(UBound(Split(href, "/"))) '8.93 seconds
myArr() = Split(href, "/"):FileName = myArr(UBound(myArr)) ' 8.71 seconds
FileName = Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))' 9.1 seconds
Notice that InstrRev is almost 7 times faster than using the Split methods.
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
add a comment |Â
up vote
2
down vote
If you need to use the Array then then this is the best practice.
Dim FileName As String, myArr() As String
myArr() = Split(href, "/")
FileName = myArr(UBound(myArr))
Here are several different ways of returning the substring after the last delimiter and the time that it takes to run each 1 million times:
FileName = Right(href, Len(href) - InStrRev(href, "/")) ' 1.25 seconds
FileName = Mid(href, InStrRev(href, "/") + 1) ' 1.29 seconds
FileName = Split(href, "/")(UBound(Split(href, "/"))) '8.93 seconds
myArr() = Split(href, "/"):FileName = myArr(UBound(myArr)) ' 8.71 seconds
FileName = Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))' 9.1 seconds
Notice that InstrRev is almost 7 times faster than using the Split methods.
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you need to use the Array then then this is the best practice.
Dim FileName As String, myArr() As String
myArr() = Split(href, "/")
FileName = myArr(UBound(myArr))
Here are several different ways of returning the substring after the last delimiter and the time that it takes to run each 1 million times:
FileName = Right(href, Len(href) - InStrRev(href, "/")) ' 1.25 seconds
FileName = Mid(href, InStrRev(href, "/") + 1) ' 1.29 seconds
FileName = Split(href, "/")(UBound(Split(href, "/"))) '8.93 seconds
myArr() = Split(href, "/"):FileName = myArr(UBound(myArr)) ' 8.71 seconds
FileName = Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))' 9.1 seconds
Notice that InstrRev is almost 7 times faster than using the Split methods.
If you need to use the Array then then this is the best practice.
Dim FileName As String, myArr() As String
myArr() = Split(href, "/")
FileName = myArr(UBound(myArr))
Here are several different ways of returning the substring after the last delimiter and the time that it takes to run each 1 million times:
FileName = Right(href, Len(href) - InStrRev(href, "/")) ' 1.25 seconds
FileName = Mid(href, InStrRev(href, "/") + 1) ' 1.29 seconds
FileName = Split(href, "/")(UBound(Split(href, "/"))) '8.93 seconds
myArr() = Split(href, "/"):FileName = myArr(UBound(myArr)) ' 8.71 seconds
FileName = Split(href, "/")(Len(href) - Len(Replace(href, "/", "")))' 9.1 seconds
Notice that InstrRev is almost 7 times faster than using the Split methods.
answered Jan 25 at 19:10
user109261
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
add a comment |Â
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
Thank you. Really helpful. I will actually use this in my current project due to the speed implications and for the fact that I only care about the last delimited substring.
â QHarr
Jan 26 at 10:25
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185977%2fextracting-filenames-from-a-url-by-splitting-a-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
@Donald.McLean Neither myself or IvenBach bothered to point out the inconsistency in the variable name. So I agree with the OP that the variable name should fixed.
â user109261
Jan 25 at 19:42
@ThomasInzina We have a bot scanning for possible answer invalidations, sometimes a question is rolled back when it's not necessary, I agree that no rollback was needed this time and therefore rolled it back again.
â Simon Forsbergâ¦
Jan 25 at 21:33