Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

VB.NET string split alternative

  • 08-12-2010 11:24am
    #1
    Closed Accounts Posts: 6


    Hey All,

    I've a stupid question but I'm hoping someone help me with.

    I have a string "Doctor, Patient, Address1, Address2" and I want to extract the Patient field. I was using string.split to do this, however my lecturer has said the preferred method is to use both Mid and Instr. I honestly cant see how using Mid and Instr can work.

    Can anyone explain to me how??

    Cheers in advance!!!


    
    Dim str as String
    str = "Doctor, Patient, Address1, Address2"
    
    Msgbox(str.Split(str(1))
    


Comments

  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    perhaps your lecturer wants you to show that you understand how the split actually works and can do your own implementation.


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    post up some code and we can help.

    Hint look at the keywords provide mid, instr


  • Closed Accounts Posts: 6 p_brennan_


    Beano wrote: »
    perhaps your lecturer wants you to show that you understand how the split actually works and can do your own implementation.

    I understand your point however when I showed him my version using split he simple said that its not the same as using MID and Instr together. I'm more just stumped what MID and Instr will do that strip wont?

    @amen the code snippet I posted is essentially what I want to do but its using split.

    If anyone even has an example of how Instr and MID can be used together I would be really grateful I cant seem to find an example online.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Find out what instr and mid do and what they return. MID takes three arguments (I think) the string, the starting point, and the length of the sub-string being sought.

    So if I have the string "The quick brown fox" and I want to extract the word quick I need to start at character 5 and read 5 characters. How do I do that with MID?

    Also, how do I know if the sub-string I'm looking for is present in the original string and how do I find its starting character's position in the string? You've got everything you need to do this, you just have to learn the instr and mid functions and then work out how exactly to use them.

    Sit down with pen and paper and work out the steps to get at the required substring, then implement that in code.

    While split is a good solution, I'd hazard a guess that your lecturer wants you to demonstrate not just that you can use instr and mid, but they also want to see some algorithm development too.


  • Closed Accounts Posts: 6 p_brennan_


    Cheers for the reply Evil Phil!

    Ok so just a few points here, I dont know how long each string is in the delimited string i.e it could be

    "John Doe, Boards.ie, Cork, Galway". or
    "Ireland, England, France, Wales"


    If I want to extract the second value "Boards.ie" I know I can get the position of the first comma using InStr but then what? should I get the position of the second comma (since I know the position of the first) and read the chars in between? I'm never going to know the length of the string I'm looking for or what it is And then the same question for the fourth.

    Maybe I'm just not understanding this whole thing correctly :S

    Evil Phil wrote: »
    Find out what instr and mid do and what they return. MID takes three arguments (I think) the string, the starting point, and the length of the sub-string being sought.

    So if I have the string "The quick brown fox" and I want to extract the word quick I need to start at character 5 and read 5 characters. How do I do that with MID?

    Also, how do I know if the sub-string I'm looking for is present in the original string and how do I find its starting character's position in the string? You've got everything you need to do this, you just have to learn the instr and mid functions and then work out how exactly to use them.

    Sit down with pen and paper and work out the steps to get at the required substring, then implement that in code.

    While split is a good solution, I'd hazard a guess that your lecturer wants you to demonstrate not just that you can use instr and mid, but they also want to see some algorithm development too.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 611 ✭✭✭brianwalshcork


    Sounds like the lecturer either wants you to just use Mid and InStr instead of Split... so you can either do this as a once off, or write your own MySplit function (extra points for having a variable delimiter!).

    Either way, your approach below is correct - what ever is between the delimiters is what you want, regardless if the length.
    p_brennan_ wrote: »
    Cheers for the reply Evil Phil!

    Ok so just a few points here, I dont know how long each string is in the delimited string i.e it could be

    "John Doe, Boards.ie, Cork, Galway". or
    "Ireland, England, France, Wales"


    If I want to extract the second value "Boards.ie" I know I can get the position of the first comma using InStr but then what? should I get the position of the second comma (since I know the position of the first) and read the chars in between? I'm never going to know the length of the string I'm looking for or what it is And then the same question for the fourth.

    Maybe I'm just not understanding this whole thing correctly :S


  • Closed Accounts Posts: 6 p_brennan_


    Sounds like the lecturer either wants you to just use Mid and InStr instead of Split... so you can either do this as a once off, or write your own MySplit function (extra points for having a variable delimiter!).

    Either way, your approach below is correct - what ever is between the delimiters is what you want, regardless if the length.

    Cool! Cheers for the comment all though I'm still confused a little bit. If I want the 4th item does that mean I have to find all three comma's firstly and then read to the end of the string?


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    If I want the 4th item does that mean I have to find all three comma's firstly and then read to the end of the string

    yes
    I'm never going to know the length of the string

    extremely common in the real word. Most of the time you will have data but not know its exact length, value etc.


Advertisement