Results 1 to 18 of 18

Thread: how do you use MID function in visual basics to search for a certain number of charac

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    how do you use MID function in visual basics to search for a certain number of charac

    how do you use MID function in visual basics to search for a certain number of characters but not counting space for example:

    Dim homesaverStorage1 As String = Mid(Textbox2.Text, 1, 255)
    Dim homesaverStorage2 As String = Mid(Textbox2.Text, 256, 255)
    Dim homesaverStorage3 As String = Mid(Textbox2.Text, 511, 255)
    Dim homesaverstorage4 As String = Mid(Textbox2.Text, 766, 255)
    Dim homesaverstorage5 As String = Mid(Textbox2.Text, 1021, 255)


    this method is taking the text in textbox 2 and storing them in 5 storages, but when we are counting the numbers, we are counting spaces. how do we exclude spaces?

  2. #2
    Addicted Member
    Join Date
    Dec 2009
    Location
    Arizona
    Posts
    185

    Re: how do you use MID function in visual basics to search for a certain number of ch

    I am not exactly sure what you are doing, but to avoid counting the spaces, you can remove them . . . I would put the text in a variable, then use the replace function to remove the spaces, then use you Mid function . . . for example:

    Dim str as String = Textbox2.Text
    str = Strings.Replace(str , " ", "")
    Dim homesaverStorage1 As String = Mid(str, 1, 255)
    This will change the count of characters though, because it will change the position of them in the string when it removes the spaces, so I am not
    sure if that is what you want . . . .
    Last edited by skywola; Jul 15th, 2012 at 01:16 AM.
    .
    We must question the story logic of having an all-knowing all-powerful God, who creates faulty Humans, and then blames them for his own mistakes.
    GENE RODDENBERRY
    .
    http://www.tachufind.com
    .

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Or if compiling in .NET 3.5 or higher... LINQ

    vb Code:
    1. Dim str As String = New String(Textbox2.Text.Where(Function(c) Not Char.IsWhiteSpace(c)).ToArray)
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Quote Originally Posted by AceInfinity View Post
    Or if compiling in .NET 3.5 or higher... LINQ

    vb Code:
    1. Dim str As String = New String(Textbox2.Text.Where(Function(c) Not Char.IsWhiteSpace(c)).ToArray)


    so I've tried this, however when I do this I did

    Code:
        Dim str As String = New String(Textbox2.Text.Where(Function(c) Not Char.IsWhiteSpace(c)).ToArray)
            Dim Homesaver11 As String = Textbox2.Text
            Dim homesaverStorage1 As String = Mid(str, 1, 255)
            Dim homesaverStorage2 As String = Mid(str, 256, 255)
            Dim homesaverStorage3 As String = Mid(str, 511, 255)
            Dim homesaverstorage4 As String = Mid(str, 766, 255)
            Dim homesaverstorage5 As String = Mid(str, 1021, 255)

    but now when I press hs1 button to copy the first homesaver storage1 it copys text box two but removes all the spaces. I want to search for 255 characters excluding spaces and store it in homesaverstorage1, but I want the spaces to show, i dont want it be showlikethisalloneword. LOL. THANKS A BUNCH.... !!!!!!!!!! BY THE WAY<HAPPY SUNDAYYY!!!!!!!!!!

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how do you use MID function in visual basics to search for a certain number of ch

    there isn't an overly simple way to do that... definately not with the mid function (which, arguably, you should be using anyways) ... in short, what you're going to have to do it loop one stinking character at a time, keeping track with a counter that you only increment when there isn't a space... adding the character to a string, until your counter hits 255, then you tie off your string, assign it where you want it, and reset your counter... it's going to be slow and inefficient...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Quote Originally Posted by Sarah GOod View Post
    so I've tried this, however when I do this I did

    Code:
        Dim str As String = New String(Textbox2.Text.Where(Function(c) Not Char.IsWhiteSpace(c)).ToArray)
            Dim Homesaver11 As String = Textbox2.Text
            Dim homesaverStorage1 As String = Mid(str, 1, 255)
            Dim homesaverStorage2 As String = Mid(str, 256, 255)
            Dim homesaverStorage3 As String = Mid(str, 511, 255)
            Dim homesaverstorage4 As String = Mid(str, 766, 255)
            Dim homesaverstorage5 As String = Mid(str, 1021, 255)

    but now when I press hs1 button to copy the first homesaver storage1 it copys text box two but removes all the spaces. I want to search for 255 characters excluding spaces and store it in homesaverstorage1, but I want the spaces to show, i dont want it be showlikethisalloneword. LOL. THANKS A BUNCH.... !!!!!!!!!! BY THE WAY<HAPPY SUNDAYYY!!!!!!!!!!
    Ahh, yeah, then it's basically as techgnome said, you're going to have to loop through every last character, and increment a counter if the current char you're on, is not a whitespace.

    EDIT: Actually no... i've got a better idea, using LINQ you can probably select an index based on a current position as a starting index, and count off where to leave off, based on where the index is in the string if you count off how many chars you want

    EDIT: Testing:
    vb Code:
    1. Dim str As String = "1 2 3 4 5"
    2.  
    3. Dim startpos As Integer = 0
    4. Dim NonSpaceCount As Integer = 3
    5. Dim x As Integer = str.IndexOf(str.First(Function(i) str.Substring(0, str.IndexOf(i)).Count(Function(c) Not Char.IsWhiteSpace(c)) = NonSpaceCount))
    6.  
    7. Console.WriteLine(str.Substring(startpos, x))

    Wow that's creative lol... Can't believe I came up with that. This will output "1 2 3". What my code does here, using LINQ, is it will find the first index of a char where from the start index position to whatever char it's checking, that substring value contains x number of chars where the char is not a whitespace value. This is interpretted based off of NonSpaceCount, and the start index for this demonstration is indicated by startpos here.

    We can keep NonSpaceCount the same, but in order to make use of this for what you're trying to do here, we're going to have to change the startpos variable accordingly. This is actually really cool. The way i've designed this LINQ expression. Errorhandling is still possibly required here for bad input data, although this does work

    EDIT: Actually this is PERFECT... As we loop through until the end of the string, we can keep grabbing substrings using my LINQ example here, and for each time probably just make the value of x, our new startpos value.

    vb Code:
    1. startpos = x

    I have yet to test, but it makes sense in my head right now.
    Last edited by AceInfinity; Jul 15th, 2012 at 11:16 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Hey babe..whats up..good evening, thanks alot again love for your assistance, ok..so I dont know if I am using your code correctly here because I am getting an error messge. I was going to test it out starting with one homesaver text box, and this is what I did [IMG][/IMG]


    and when I press the button copy to home saver notes an error pops up. am i doing this correctly ? ahh.i feel like i am such a blond. I really appreciate your assistance. Hope you've had a GREAT SUNDAY!!!! CLICK BUTTON>>>>DISPLAY " THANK YOU !!!!!!!!!!"

    Quote Originally Posted by AceInfinity View Post
    Ahh, yeah, then it's basically as techgnome said, you're going to have to loop through every last character, and increment a counter if the current char you're on, is not a whitespace.

    EDIT: Actually no... i've got a better idea, using LINQ you can probably select an index based on a current position as a starting index, and count off where to leave off, based on where the index is in the string if you count off how many chars you want

    EDIT: Testing:
    vb Code:
    1. Dim str As String = "1 2 3 4 5"
    2.  
    3. Dim startpos As Integer = 0
    4. Dim NonSpaceCount As Integer = 3
    5. Dim x As Integer = str.IndexOf(str.First(Function(i) str.Substring(0, str.IndexOf(i)).Count(Function(c) Not Char.IsWhiteSpace(c)) = NonSpaceCount))
    6.  
    7. Console.WriteLine(str.Substring(startpos, x))

    Wow that's creative lol... Can't believe I came up with that. This will output "1 2 3". What my code does here, using LINQ, is it will find the first index of a char where from the start index position to whatever char it's checking, that substring value contains x number of chars where the char is not a whitespace value. This is interpretted based off of NonSpaceCount, and the start index for this demonstration is indicated by startpos here.

    We can keep NonSpaceCount the same, but in order to make use of this for what you're trying to do here, we're going to have to change the startpos variable accordingly. This is actually really cool. The way i've designed this LINQ expression. Errorhandling is still possibly required here for bad input data, although this does work

    EDIT: Actually this is PERFECT... As we loop through until the end of the string, we can keep grabbing substrings using my LINQ example here, and for each time probably just make the value of x, our new startpos value.

    vb Code:
    1. startpos = x

    I have yet to test, but it makes sense in my head right now.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    wow.im so slow,,i just realized., homesaver1.text should = x. haha..let me try that.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    hmm. when i do that.. if i make homesaver1.textbox = x it still gives me the Sequence contains no matching element erroer for some reason, am I doing something wrong still love?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    and if i make str like in your example = to "1 2 3 4 5" then when I press copy to homesaver notes button it makes the first blue box for hs1 a value of 6, so I tried playing around with it and if I make str = "1 2 3 4 5 6" (because I was thinking maybe the output will be 7 for somereason.. haha..Hey i am trying to find a pattern.."yes laugh at me I know im so slow" ) but yes when i put that Str = "1 2 3 4 5 6" then when I try to even debug it automatically gives me that error even before I press the copy to homesaver notes button

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    I also keep seeing this message,,before the program loads ,,when i put these codes in ,,but it allows me to bypass it by pressing continue Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    WEll I know your probably sleeping right now.so GOOD NIGHT Sweet DreamZZZ...

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: how do you use MID function in visual basics to search for a certain number of ch

    What you're trying to do is going to be more complicated than you think. Tell us more about what you're doing and why you want to do this.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how do you use MID function in visual basics to search for a certain number of ch

    I notice that the variable is str while the Linq contains Str ... which make me wonder if it's using the Str function rather than the variable... try changing the name of the variable to something that isn't a VB function, then make the necessary changes in the LINQ statement hopefully then it should work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Re: how do you use MID function in visual basics to search for a certain number of ch

    HEYYY BABE>..Whats up. OMG..Thank you for all your assistance, and thank everyone else on this site. YOu guys have really showed me the way to my answers, and MUCH MORE!!!!!!!!!!!!!!!!!!!!!!!!! Dont be a stranger, keep in touch, who knows...maybe i may be helping you one day LOL







    Quote Originally Posted by AceInfinity View Post
    Ahh, yeah, then it's basically as techgnome said, you're going to have to loop through every last character, and increment a counter if the current char you're on, is not a whitespace.

    EDIT: Actually no... i've got a better idea, using LINQ you can probably select an index based on a current position as a starting index, and count off where to leave off, based on where the index is in the string if you count off how many chars you want

    EDIT: Testing:
    vb Code:
    1. Dim str As String = "1 2 3 4 5"
    2.  
    3. Dim startpos As Integer = 0
    4. Dim NonSpaceCount As Integer = 3
    5. Dim x As Integer = str.IndexOf(str.First(Function(i) str.Substring(0, str.IndexOf(i)).Count(Function(c) Not Char.IsWhiteSpace(c)) = NonSpaceCount))
    6.  
    7. Console.WriteLine(str.Substring(startpos, x))

    Wow that's creative lol... Can't believe I came up with that. This will output "1 2 3". What my code does here, using LINQ, is it will find the first index of a char where from the start index position to whatever char it's checking, that substring value contains x number of chars where the char is not a whitespace value. This is interpretted based off of NonSpaceCount, and the start index for this demonstration is indicated by startpos here.

    We can keep NonSpaceCount the same, but in order to make use of this for what you're trying to do here, we're going to have to change the startpos variable accordingly. This is actually really cool. The way i've designed this LINQ expression. Errorhandling is still possibly required here for bad input data, although this does work

    EDIT: Actually this is PERFECT... As we loop through until the end of the string, we can keep grabbing substrings using my LINQ example here, and for each time probably just make the value of x, our new startpos value.

    vb Code:
    1. startpos = x

    I have yet to test, but it makes sense in my head right now.

  16. #16
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Quote Originally Posted by techgnome View Post
    I notice that the variable is str while the Linq contains Str ... which make me wonder if it's using the Str function rather than the variable... try changing the name of the variable to something that isn't a VB function, then make the necessary changes in the LINQ statement hopefully then it should work.

    -tg
    No, the Str function would have been 'overrided' by the str variable i've declared anyways, even though that's a good point, i'm used to using str though, I should change that

    Maybe 'strObj'?

    You can't use the Str function though if you declare a variable as str in the way I have it above, the variable declaration would take presedence. And any params trying to be used for the Str function, would more be interpretted as char index's for the str variable itself.

    Nice catch though
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  17. #17
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Try this?

    vb Code:
    1. Dim homesaverStorage1 = Textbox2.Text.Replace(" ", "").Substring(1, 255)
    2. '...etc

    Kris

  18. #18
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: how do you use MID function in visual basics to search for a certain number of ch

    Quote Originally Posted by i00 View Post
    Try this?

    vb Code:
    1. Dim homesaverStorage1 = Textbox2.Text.Replace(" ", "").Substring(1, 255)
    2. '...etc

    Kris
    Read through the thread again this will not work. She wants to keep the spaces in the result but only make sure that the result itself contains 255 nonwhitespace chars... I posted an adaptable LINQ solution in post #6
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

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



Click Here to Expand Forum to Full Width