Easy VB Question
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Easy VB Question

  1. #1
    AO Security for Non-Geeks tonybradley's Avatar
    Join Date
    Aug 2002
    Posts
    830

    Easy VB Question

    OK. Don't shoot me- this is quite the newbie programming question....

    In VB (I'm using VB .Net) how do I return only part of a string?

    For instance, if provided with the string "C:\thisisafile.txt " I want to return just the "C". But, I need it to return whatever is left of the colon regardless of the number of characters. So, if it was "Tony's Drive:\thisisafile.txt" I would want it to return "Tony's Drive".

    Should be simple for you gurus- any takers on answering?

    Thanks

  2. #2
    Senior Member
    Join Date
    Mar 2003
    Location
    central il
    Posts
    1,779
    I haven't delt with it in .net yet, but I believe it has full Regular Expression support with a RegX calss implmented in the frame work...that would be your best bet.
    Who is more trustworthy then all of the gurus or Buddha’s?

  3. #3
    Im not too sure on the syntax of VB, but from a programmer, run a function that goes through each character in the string and locates the ':'. Once this is located, cut everything off to the right of the string including the ':'. Save the string in a new variable. Bingo.

    Scatman
    If the scatman can do it so can you.

  4. #4
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,914
    Code:
    strTest = "My:\Drive"
    intLocationOfColon = InStr(strTest, ":")
    strResult = Microsoft.VisualBasic.Left(strTest, intLocationOfColon - 1)
    This will return My

    InStr and MS.VB.Left are the only two commands you'll need..

    Hope this helps

    -- HT
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  5. #5
    Senior Member
    Join Date
    Jan 2003
    Posts
    1,499
    O.K Tony.

    How you do this is real easy and I know why your doing it to.

    Dim a variable for number of points to colon e.g "dim intnumpoicoln as integer" at the top of the sub function

    Dim a variable for length of string "dim intlength as integer"

    Dim a variable for the number of points to trim "dim intrighttrimto as integer"

    Dim a variable for the newstring "dim strnewstring as string"

    Use this to work out the number of points to the colon "intnumpoicoln = InStr(1, stryourstring, ":")"

    Work out the length you want to trim off using
    "intrighttrimto = Len(stryourtext) - intnumpointsspace"

    Cut the new string from the old one "strnewstring = Right(stryourtext, intrighttrimto)

  6. #6
    AO Security for Non-Geeks tonybradley's Avatar
    Join Date
    Aug 2002
    Posts
    830
    How you do this is real easy and I know why your doing it to.
    You think so?? :-)

    I would be VERY surprised if you do- but you've piqued my curiosity. Why do you think I needed to get VB code to extract the string to the left of the colon?

  7. #7
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,914
    Originally posted here by mark_boyle2002
    O.K Tony.

    How you do this is real easy and I know why your doing it to.

    Dim a variable for number of points to colon e.g "dim intnumpoicoln as integer" at the top of the sub function

    Dim a variable for length of string "dim intlength as integer"

    Dim a variable for the number of points to trim "dim intrighttrimto as integer"

    Dim a variable for the newstring "dim strnewstring as string"

    Use this to work out the number of points to the colon "intnumpoicoln = InStr(1, stryourstring, ":")"

    Work out the length you want to trim off using
    "intrighttrimto = Len(stryourtext) - intnumpointsspace"

    Cut the new string from the old one "strnewstring = Right(stryourtext, intrighttrimto)
    Well you messed up your variables, but that's no big deal.... he wants everything to the left of the colon, you told him how to take everything to the right. In VB .Net the way the InStr command works is (StringToUse, CharacterStringToFind)... so I'm not really sure why you have a 1 at the start of the code. and Right is no longer acceptable on it's own. It has to be Microsot.VisualBasic.Right.....
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  8. #8
    Senior Member
    Join Date
    Jan 2003
    Posts
    1,499

    The 1 is to represent the actual colon's position.

    Tony,

    I believe your doing this for the same reason I have in the past because VB doesn't give you the purest drive letter e.g c: it can give you unc's or names.

    I have a good control which gets round about this I will e-mail to you if you like.

    P.S HTREGZ, Tony is very clever and I was sure he would work it out.

    The 1 in the code is because VB counts from 0 and hence I needed a 1 to represent the actual position of the colon.

  9. #9
    Senior Member tampabay420's Avatar
    Join Date
    Aug 2002
    Posts
    953
    left$(string_var, length)
    right$(string_var, length)
    mid$ (string_var, offset, length)

    a simple answer is always nice

    Code:
    Dim DRIVE_NAME As String
    Dim FILE_PATH As String
    Dim OFFSET As String
    
    FILE_PATH = Text1.Text 'I'm using a textbox for input...
    
    OFFSET = InStr(FILE_PATH, ":\")
    
    DRIVE_NAME = Mid$(FILE_PATH, 1, OFFSET - 1)
    
    MsgBox DRIVE_NAME     'I'm using the MsgBox for output
    yeah, I\'m gonna need that by friday...

  10. #10
    Senior Member
    Join Date
    Jan 2003
    Posts
    1,499
    Yep,

    Its like in waynes world if you book them they will come.

    If you ask a VB Question TampaBay is sure to appear with the best answer.

    I have posted tonnes of VB questions and in almost every case he has appeared and been my saving grace.

    Some Usefull software Terms for you to use when programming Tony

    Glossary of Software Terms

    All new: The software is not compatible with previous versions.

    Advanced design: Upper management doesn't understand it.

    Breakthrough: It nearly booted on the first try.

    Capability maturity model: A method of determining to what extent the developer can reasonably be blamed for the inevitable failure.

    Cleanroom: A management technique that applies to horizontal interfaces what the mushroom technique applies to vertical interfaces.

    Compiler: A tool for adding an exciting amount of uncertainty to the size, speed and correctness of a program.

    Computer human interface: The means by which the program conditions the user into never trying all the things that don't work.

    Cost modelling: A means of convincing the customer to pay for whomever you need to keep employed this year.

    Customer: A primitive life form at the bottom of the food chain.

    Debugger: A tool that substitutes afterthought for forethought.

    Design: The activity of preparing for a design review.

    Design review: A process for ensuring you know exactly what it is you won't build.

    Design simplicity: It was developed on a shoe-string budget.

    Documentary hypothesis: The discredited notion that software is the outcome of a systematic and rational process of development, rather than the result of divine inspiration.

    Documentation: A process for converting trees into entropy, usually applied to provide busywork for the people whose employment cannot be justified by cost modelling.

    Domain: A class of applications where failure on one project gives you an advantage in bidding on the next.

    Enhancement: Breaking what you did right and getting paid for it. [see also: maintenance]

    Exclusive: We're the only ones who have the documentation.

    Field tested: Manufacturing doesn't have a test system.

    Foolproof operation: All parameters are hard coded.

    Formal verification: The construction of an incorrect proof isomorphic to an incorrect program.

    Function point analysis: Cost modelling a program by what it won't do, rather than by how big it won't be.

    Futuristic: It only runs on the next-generation supercomputer.

    Incremental implementation: Delivering several partial products each for the price of a complete one.

    It's here at last: We've released a 26-week project in 48 weeks.

    Maintenance: Fixing what you did wrong and getting paid for it. [see also: enhancement]

    Maintenance free: It's impossible to fix.

    Meets quality standards: It compiles without errors.

    New: It comes in different colors from the previous version.

    Performance proven: It works through beta test.

    Programs: What software used to be, back when we knew how to write it.

    Project management: The art of always knowing how badly you're doing your work and how late you're doing it.

    Quality assurance: A way to ensure you never deliver shoddy goods accidentally.

    Real time: An attribute applied to software that's even more expensive than can be justified by cost modelling.

    Requirements analysis: Determining what it is you can't do before failing to do it.

    Requirements engineering: Convincing the customer to want what you think you can build.

    Requirements review: Explaining what the customer won't get in language they don't understand.

    Reuse: Using an existing product in a new context; especially as applied to proposals, resumes, disclaimers and excuses.

    Revolutionary: The disk drives go round and round.

    Satisfaction Guaranteed: We'll send you another copy if it fails.

    Software engineer: One who engineers others into writing the code for him/her.

    Spiral model: A development model that allows you to fail in a small way several times over. [see also: waterfall model]

    State-of-the-art: What we could do with enough money.

    State-of-the-practice: What we can do with the money you have.

    Stock item: We shipped it once before, and we can do it again, probably.

    Structured walkthrough: The process whereby the false assumptions of one member become shared by an entire team.

    Technology transition: Helping people replace old useless processes, methods and tools with new useless processes, methods and tools.

    Testing: A process for ensuring that the product will work in all circumstances that anybody other than the user can imagine.

    Total quality management: A way of teaching your managers five words of Japanese, without any risk that they will acquire an equivalent amount of competence.

    Unprecedented performance: Nothing ever ran this slow before.

    User: A harmless drudge.

    Waterfall model: A development model that allows you to fail in a big way just once.

    Years of development: We finally got one to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •