I am creating an array as follows
Dim strFriends(0 to 6) As String
strFriends(0) = "Bianca"
strFriends(1) = "Jeana"
strFriends(2) = "Sam"
strFriends(3) = "Jenna"
strFriends(4) = "Erin"
strFriends(5) = "Carolyn"
strFriends(6) = "Kate"
Dim myFriends As String
myFriends = Join(strFriends, ", ")
MsgBox myFriends
This will produce the following string: "Bianca, Jeana, Sam, Jenna, Erin, Carolyn, Kate"
But I need to retrieve specific items in array and display them as list, something like this:
e.g: If I want to select from strFriends, Kate ,Sam and Bianca
It should list like
Kate
Sam
Bianca
How to perform the task. I am really new to VB so I am confused with this simple task. Can anyone help.
Thank you
Dim strFriends(0 To 6) As String
strFriends(0) = "Bianca"
strFriends(1) = "Jeana"
strFriends(2) = "Sam"
strFriends(3) = "Jenna"
strFriends(4) = "Erin"
strFriends(5) = "Carolyn"
strFriends(6) = "Kate"
Dim objOutput As Text.StringBuilder = New Text.StringBuilder()
For Each strFriend As String In strFriends
Select Case strFriend
Case "Kate", "Sam", "Bianca"
objOutput.AppendLine(strFriend)
End Select
Next
MessageBox.Show(objOutput.ToString())
Or
For Each strFriend As String In strFriends
If MyLogicToDetermineSelected(strFriend) Then
objOutput.AppendLine(strFriend)
End If
Next
It's not clear why you need to pick items from the original array when you already know which items you want, so I won't answer that part of the question. As for displaying each item on it's own line, you can do this with String.Join:
Dim chosenFriends As String() = {"Kate", "Sam", "Bianca"}
Dim output As String = String.Join(Environment.NewLine, chosenFriends)
Related
I have this string abcd, and I want to replace the a with [a|b] and the b with [c|d]
I try many ways to do it, like
Dim varString As String = "abcd"
varString = varString.Replace("a", "[a|b]")
varString = varString.Replace("b", "[c|d]")
The result I get is
[a|[c|d]][c|d]cd
Instead I want it like this
[a|b][c|d]cd
The problem is every time I use the replace function it backs to change the values I already replaced before so I replaced a with [a|b] but then when I do my second command to replace the b it changes the b in [a|b] that I just changed and I don't want this.
I tried to use StringBuilder but it gives the same result.
Please advise me,
I solved the problem by making an array in this way
Dim NewCommand As String = "abcd"
For i = 0 To LikeCommand.Length - 1
If LikeCommand(i) = "a" Then
NewCommand += "[a|b]"
ElseIf LikeCommand(i) = "b" Then
NewCommand += "[c|d]"
Else
NewCommand += LikeCommand(i)
End If
Next
LikeCommand = NewCommand
Or just switch the logic up. But obviously I'm thinking you're using a basic example for a more complex question.
dim varString as string = "abcd"
varString = varString.Replace("b" ,"[c|d]")
varString = varString.Replace("a" ,"[a|b]")
That would get you the desired results.
Im sure this code was working but cannot rememeber what i did to make it work.
I want to match fields first middle and last with wildcard in between to string user
Return (From q In context.users
Where (Join( {q.user_first, q.user_middle, q.user_last}, "%"))
.ToLower.Contains(user.ToLower)
Select q).ToList
The error message is:
LINQ to Entities does not recognize the method 'System.String
Join(System.String[], System.String)' method, and this method cannot
be translated into a store expression.
Hope someone finds this useful.
I created a computed column in user table called user_fullname:
(rtrim((coalesce([user_first]+' ','')+coalesce([user_middle]+' ',''))+coalesce([user_last]+' ','')))
The code below, splits the name into an array to work out what to search for.
Dim names As String() = user.Trim.Split(" ")
If names.Count = 2 Then
Dim fname As String = names(0)
Dim lname As String = " " & names(1)
Return (From q In context.users Where (q.user_fullname.StartsWith(fname) And q.user_fullname.Contains(lname)) Select q).AsQueryable
ElseIf names.Count = 1 Then
Return (From q In context.users Where (q.user_fullname.StartsWith(user) Or q.user_last.StartsWith(user)) Select q).AsQueryable
ElseIf names.Count = 3 Then
Dim fname As String = names(0)
Dim lname As String = names(2)
Dim mname As String = names(1)
Return (From q In context.users Where (q.user_first.StartsWith(fname) And q.user_middle.StartsWith(mname) And q.user_last.StartsWith(lname)) Select q).AsQueryable
Else
Return Nothing
Dim receipts As IQueryable(Of ReceiptEntity) = db.Receipts
'code to filter removed for brevity
Dim sorts() As String = SortExpression.Split(";")
For Each sort As String In sorts
Dim sortParts() As String = sort.Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.OrderBy(Of ReceiptEntity)(sortParts(0).ToString(), SortDirection.Ascending)
Else
receipts = receipts.OrderBy(Of ReceiptEntity)(sortParts(0).ToString(), SortDirection.Descending)
End If
Next
SortExpression comes in like "field1 true;field2 false;field3 true"
What I want to happen is for the query to have multiple order by fields, what is happening is that only the last order by is applied. What am I doing wrong here?
Here is what the working result looks like:
Dim receipts As IOrderedQueryable(Of ReceiptEntity) = db.Receipts.Include(Function(r) r.LineItems).Include(Function(r) r.Payments)
Dim sorts() As String = SortExpression.Split(";")
Dim sortParts() As String
sortParts = sorts(0).Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.OrderBy(sortParts(0).ToString())
Else
receipts = receipts.OrderByDescending(sortParts(0).ToString())
End If
For Each sort As String In sorts.Skip(1)
sortParts = sort.Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.ThenBy(sortParts(0).ToString())
Else
receipts = receipts.ThenByDescending(sortParts(0).ToString())
End If
Next
You have to use ThenBy instead of OrderBy for the second and all subsequent sort operations.
Ok, so im trying to make this program and i need this:
"Melternet Hello Melternet#gmail.com 5/7/2013" to be in different textbox's like this
"Textbox1 = Melternet"
"Textbox2 = Hello"
"Textbox3 = Melternet#gmail.com"
"Textbox4 = 5/7/2013"
So pretty much every space is a cut off line to add that text to a textbox and then it does the rest like the first one
How would i do something like that, thanks in advance.
Please Answer Back If Anyone Can Figure This Out Or Help Me, NEED THIS QUICK...
BTW: i'm using Visual Basic 2008.
Without more info...something like:
Dim data As String = "Melternet Hello Melternet#gmail.com 5/7/2013"
Dim values() As String = data.Split(" ")
If values.Length >= 4 Then
TextBox1.Text = values(0)
TextBox2.Text = values(1)
TextBox3.Text = values(2)
TextBox4.Text = values(3)
End If
It looks like you are simply splitting the entire input text string: "Melternet Hello Melternet#gmail.com 5/7/2013" wherever a space occurs, yes(?)
If your string has a variable number of words, then fill up textboxes programmatically, like, e.g.:
Dim mystr as String
mystr = "hello world I want to paste this to multiple textboxes"
Dim Buff() as String
Buff = Split(mystr," ")
For i As Integer = 0 to UBound(Buff)
Dim tb As New TextBox
str = Buff(i)
tb.Name = str
tb.Text = str
tb.Left = 50
tb.Top = 50 + 25 * i
tb.Width = 50
tb.TextAlign = HorizontalAlignment.Right
Me.Controls.Add(tb)
Next
In the above fashion, no matter what the value of mystr, or how many words are in the string (mystr), you can write them all to new textboxes that are created/placed dynamically on Form1, i.e. "Me".
I am creating a for each loop to take the words from a string and place them each into a text box. The program allows for up to "9" variables What I am trying to attempt is.
Foreach word in Words
i = i +1
Varible & i = word
txtCritical1.Text = variable & i
any ideas on a way to make this work?
Have a look through the MSDN article on For Each. It includes a sample using strings.
https://msdn.microsoft.com/en-us/library/5ebk1751.aspx
So i went with a simple if statement this does the trick. Each text box is filled in.
Dim details As String = EditEvent.EditDbTable.Rows(0).Item(13).ToString()
Dim words As String() = details.Split(New Char() {"«"})
Dim word As String
For Each word In words
i = i + 1
v = word
If i = 1 Then
txtCritical1.Text = v
ElseIf i = 2 Then
txtCritical2.Text = v
ElseIf ....
ElseIf i = 9 then
txtCritical2.text = v
Else
....
End If
Next