Extracting filenames from a URL by splitting a string

The name of the pictureThe name of the pictureThe name of the pictureClash 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))






share|improve this question





















  • @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
















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))






share|improve this question





















  • @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












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))






share|improve this question













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))








share|improve this question












share|improve this question




share|improve this question








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
















  • @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










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





share|improve this answer























  • 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


















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.






share|improve this answer





















  • 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










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer























  • 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















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





share|improve this answer























  • 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













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





share|improve this answer















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






share|improve this answer















share|improve this answer



share|improve this answer








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

















  • 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













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.






share|improve this answer





















  • 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














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.






share|improve this answer





















  • 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












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.






share|improve this answer













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.







share|improve this answer













share|improve this answer



share|improve this answer











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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?