-
Oct 12th, 2013, 04:00 AM
#1
Thread Starter
Lively Member
studio 2012 split string with if statement not sure how to go about this
hey guys
i have a working program that loops through a list and does stuff
in the textbox1 i would have lets say "aaa"
now i would like to add a job list to to script so if i put into the textbox "aaa,bbb,ccc"
it would do the whole job 3 times with all three instructions
i can split the textbox into an array like this
Dim joblist As String() = TextBox1.Text.Split(New Char() {","c})
and make a do while list with the size of the array and increment the array each time
but how do i check to see if the textbox has the character "," and only run the loop if it does
any ideas
-
Oct 12th, 2013, 04:10 AM
#2
Re: studio 2012 split string with if statement not sure how to go about this
VB.NET MVP 2008 - Present
-
Oct 12th, 2013, 04:19 AM
#3
Re: studio 2012 split string with if statement not sure how to go about this
You wouldn't use a Do loop in this case. There are four types of loops: For, For Each, Do and While. You should only be using a Do or While loop if you don't know exactly how many times you need to loop when you start. In this case, you know exactly how many times you need to loop, i.e. the number of elements in the array. In that case, a For loop would be more appropriate than a Do or While loop. This case is even more specific though, i.e. you want to perform an action for each element in the array. What sort of lop do you think would be most appropriate then?
To answer your question, don't bother. Just run the For Each loop and, if there's only one element, the loop will only run for one iteration.
-
Oct 12th, 2013, 07:33 AM
#4
Thread Starter
Lively Member
Re: studio 2012 split string with if statement not sure how to go about this
Originally Posted by jmcilhinney
You wouldn't use a Do loop in this case. There are four types of loops: For, For Each, Do and While. You should only be using a Do or While loop if you don't know exactly how many times you need to loop when you start. In this case, you know exactly how many times you need to loop, i.e. the number of elements in the array. In that case, a For loop would be more appropriate than a Do or While loop. This case is even more specific though, i.e. you want to perform an action for each element in the array. What sort of lop do you think would be most appropriate then?
To answer your question, don't bother. Just run the For Each loop and, if there's only one element, the loop will only run for one iteration.
i can't argue with that
the issue i have now is making the array when there is no "," in the textbox
using this
Dim joblist As String() = TextBox1.Text.Split(New Char() {","c})
it will work fine with "aaa,bbb,ccc,ddd"
but will error when there is only "aaa"
how would i overcome this
-
Oct 12th, 2013, 07:41 AM
#5
Re: studio 2012 split string with if statement not sure how to go about this
Firstly, if all you want to do is split on a single Char, there's no need to create a Char array. That overload of String.Split has its parameter declared as a ParamArray, meaning that it will accept a single Char array or zero, one or more discrete Chars. You only need this:
Code:
Dim joblist As String() = TextBox1.Text.Split(","c)
As for the "issue", there's no issue at all. If the String doesn't contain the delimiter(s) then Split will simply return an array with a single element containing the original String. As I said, if you use a For Each loop, it will just work. If you get an error then you're doing it wrong but we can't tellyou what's wrong with your code if we haven't seen it or the error.
-
Oct 12th, 2013, 07:44 AM
#6
Re: studio 2012 split string with if statement not sure how to go about this
The split should not error no matter what is in the textbox. Try it like this
Code:
Dim joblist() As String = TextBox1.Text.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
For x As Integer = 0 To joblist.Length - 1
doSomething(joblist(x))
Next
'or
Dim joblist() As String = TextBox1.Text.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
For Each s As String In joblist
doSomething(s)
Next
-
Oct 12th, 2013, 07:49 AM
#7
Thread Starter
Lively Member
Re: studio 2012 split string with if statement not sure how to go about this
its ok i sussed it
Dim joblist As String() = TextBox1.Text.Split(New Char() {","c})
Dim itm As Object
For Each itm In joblist
MsgBox(itm)
'' my code here
Next itm
thanx for help guyz
-
Oct 12th, 2013, 07:55 AM
#8
Re: studio 2012 split string with if statement not sure how to go about this
Dim itm As Object??? Ahhhhh what planet are you learning on? Go into options are turn option strict on. You don't need to declare a variable for the loop either way.
-
Oct 12th, 2013, 08:01 AM
#9
Re: studio 2012 split string with if statement not sure how to go about this
Does post 6 look like that? http://www.vbforums.com/showthread.p...=1#post4528193
If you are worried the string will throw an exception if it does not contain a , then you can either check the string contains the delimiter or maybe use regex.split,
vb Code:
Public Class Form1 Private Sub Example() Dim joblist() As String = System.Text.RegularExpressions.Regex.Split(Me.TextBox1.Text, ",") For Each job In joblist MessageBox.Show(job) Next End Sub End Class
Last edited by ident; Oct 12th, 2013 at 08:14 AM.
-
Oct 12th, 2013, 11:28 AM
#10
Thread Starter
Lively Member
Re: studio 2012 split string with if statement not sure how to go about this
Originally Posted by ident
Does post 6 look like that? http://www.vbforums.com/showthread.p...=1#post4528193
If you are worried the string will throw an exception if it does not contain a , then you can either check the string contains the delimiter or maybe use regex.split,
vb Code:
Public Class Form1
Private Sub Example()
Dim joblist() As String = System.Text.RegularExpressions.Regex.Split(Me.TextBox1.Text, ",")
For Each job In joblist
MessageBox.Show(job)
Next
End Sub
End Class
to be honest the way i learn is google something similar then alter it to work with and if i get issues i tend to ask for help and hopefully understand why it works
but my code works well now
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
|