Re: Can this be written better?
- From: "Mike Williams" <mikea@xxxxxxxxxxxxxxxxx>
- Date: Sat, 31 Mar 2007 11:13:55 +0100
"Ron" <Ron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:3BA97A3D-12F7-495C-A7E6-86AEF18B220A@xxxxxxxxxxxxxxxx
A string from my program includes a file ext which I want
to exclude, so I have written this code, can this be done better?
Do Until xCount = xLen1 + 1 ' Run until fullstop is found or end of string
Running through the string character by character is very slow. VB has various functions which will search for a substring inside a string and return its position for you. There are two main such functions, one of which searches from the start of the string looking forwards and the other searches from the end looking backwards. Check out the Instr and the IntrRev functions (type one of those names into some code and hit the F1 key). There are also functions that will return a specified number of characters of a string for you, from either the left side, right side or somewhere in the middle. Have a look at the Left$, Right$ and Mid$ functions.
Also, you haven't actually made it quite clear what you are trying to do. Your description implies that you want to remove a file's extension whatever it is, but your code is specifically looking for the .jpg extension and will leave other extensions alone. Which do you want to do? If you only want to remove the specific extension ".jpg" and leave other extensions alone then you could use:
If Right$(xPart2, 4) = ".jpg" Then
xPart2 = Left$(xPart2, Len(xPart2) - 4)
End If
However, if you want to remove the file extension no matter what it is then you need to do it differently and you also need to take into account that on modern versions of Windows a file extension can have more (or less) than three characters. Here is some code that will remove a file extension for you, no matter what it is (subject to the limitations mentioned in the next few lines). The code is something I've just knocked up very quickly and it takes into account only what "I have in my own head at the moment" about file extensions, so you will need to work on it. For example, I know that a space is not permitted in a file extension, so my code takes that into account, but there are almost certainly other characters that are not permitted in a file extension so you will need to deal with them as well. It'll be a good exercise for you. By the way, there will almost certainly be an API routine that will do this job for you, but looking at your sample code I imagine that you will be far more interested in seeing how it is done using standard VB functions so I have not bothered with the API stuff.
p1 = InStrRev(xPart2, ".")
p2 = InStrRev(xPart2, " ")
If p1 > 0 And p2 < p1 Then
xPart2 = Left$(xPart2, p1 - 1)
End If
Mike
.
- Follow-Ups:
- Re: Can this be written better?
- From: Mike Williams
- Re: Can this be written better?
- Prev by Date: Re: Can this be written better?
- Next by Date: Re: International MsgBox button captions
- Previous by thread: Re: Can this be written better?
- Next by thread: Re: Can this be written better?
- Index(es):
Relevant Pages
|