|
-
Jul 14th, 2012, 11:03 PM
#1
Thread Starter
Junior Member
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?
-
Jul 15th, 2012, 01:10 AM
#2
Addicted Member
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
.
-
Jul 15th, 2012, 03:02 AM
#3
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:
Dim str As String = New String(Textbox2.Text.Where(Function(c) Not Char.IsWhiteSpace(c)).ToArray)
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 15th, 2012, 02:49 PM
#4
Thread Starter
Junior Member
Re: how do you use MID function in visual basics to search for a certain number of ch
 Originally Posted by AceInfinity
Or if compiling in .NET 3.5 or higher... LINQ
vb Code:
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!!!!!!!!!!
-
Jul 15th, 2012, 10:01 PM
#5
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
-
Jul 15th, 2012, 10:19 PM
#6
Re: how do you use MID function in visual basics to search for a certain number of ch
 Originally Posted by Sarah GOod
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:
Dim str As String = "1 2 3 4 5" Dim startpos As Integer = 0 Dim NonSpaceCount As Integer = 3 Dim x As Integer = str.IndexOf(str.First(Function(i) str.Substring(0, str.IndexOf(i)).Count(Function(c) Not Char.IsWhiteSpace(c)) = NonSpaceCount)) 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.
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 16th, 2012, 01:09 AM
#7
Thread Starter
Junior Member
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 !!!!!!!!!!"
 Originally Posted by AceInfinity
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:
Dim str As String = "1 2 3 4 5"
Dim startpos As Integer = 0
Dim NonSpaceCount As Integer = 3
Dim x As Integer = str.IndexOf(str.First(Function(i) str.Substring(0, str.IndexOf(i)).Count(Function(c) Not Char.IsWhiteSpace(c)) = NonSpaceCount))
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.
I have yet to test, but it makes sense in my head right now.
-
Jul 16th, 2012, 01:14 AM
#8
Thread Starter
Junior Member
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.
-
Jul 16th, 2012, 01:16 AM
#9
Thread Starter
Junior Member
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?
-
Jul 16th, 2012, 01:21 AM
#10
Thread Starter
Junior Member
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
-
Jul 16th, 2012, 01:22 AM
#11
Thread Starter
Junior Member
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.
-
Jul 16th, 2012, 01:23 AM
#12
Thread Starter
Junior Member
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...
-
Jul 16th, 2012, 05:42 AM
#13
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.
-
Jul 16th, 2012, 06:39 AM
#14
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
-
Jul 17th, 2012, 04:28 PM
#15
Thread Starter
Junior Member
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 
 Originally Posted by AceInfinity
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:
Dim str As String = "1 2 3 4 5"
Dim startpos As Integer = 0
Dim NonSpaceCount As Integer = 3
Dim x As Integer = str.IndexOf(str.First(Function(i) str.Substring(0, str.IndexOf(i)).Count(Function(c) Not Char.IsWhiteSpace(c)) = NonSpaceCount))
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.
I have yet to test, but it makes sense in my head right now.
-
Jul 17th, 2012, 08:25 PM
#16
Re: how do you use MID function in visual basics to search for a certain number of ch
 Originally Posted by techgnome
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
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 18th, 2012, 02:56 AM
#17
Re: how do you use MID function in visual basics to search for a certain number of ch
Try this?
vb Code:
Dim homesaverStorage1 = Textbox2.Text.Replace(" ", "").Substring(1, 255)
'...etc
Kris
-
Jul 18th, 2012, 02:15 PM
#18
Re: how do you use MID function in visual basics to search for a certain number of ch
 Originally Posted by i00
Try this?
vb Code:
Dim homesaverStorage1 = Textbox2.Text.Replace(" ", "").Substring(1, 255) '...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
<<<------------
.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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|