loop through 2 strings and combine VB 2010 - vb.net

i have a simple task i think
my code generates 2 strings
string A (separated by line feeds)
1
2
3
4
String B (separated by line feeds)
5
6
7
8
i am trying to figure out how to combine these separate strings into one as if they were two columns next to each other, separated by a comma
the result would be string C
1,5
2,6
3,7
4,8
Thanks!

How about something like?
Dim A As String = "1" & vbCrLf & "2" & vbCrLf & "3" & vbCrLf & "4"
Dim B As String = "5" & vbCrLf & "6" & vbCrLf & "7" & vbCrLf & "8"
Debug.Print(A)
Debug.Print(B)
Dim tmp As New List(Of String)
tmp.AddRange(A.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries))
Dim values() As String = B.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To values.Length - 1
If i <= tmp.Count - 1 Then
tmp(i) = tmp(i) & "," & values(i)
Else
tmp.Add("," & values(i))
End If
Next
Dim C As String = String.Join(vbCrLf, tmp.ToArray)
Debug.Print(C)

Convert both strings to char[] (Char() - character array) (string.ToCharArray()).
Iterate thru the arrays.
You will need to check boundary conditions (e.g. what if the strings are different lengths).

'first string
Dim a As String = "abcd"
'second string
Dim b As String = "defghijk"
'Remove Line feeds
Dim tempA As String = a.Replace(vbLf, "")
Dim tempB As String = a.Replace(vbLf, "")
'Get the string with the larger size.
Dim largerSize As Integer = If(tempA.Length > tempB.Length, tempA.Length, tempB.Length)
'We use a string builder to build our new string
Dim sb As New StringBuilder()
'Loop on the larger size and only insert if inside range of string
For i As Integer = 0 To largerSize - 1
If i < tempA.Length Then
sb.Append(a(i))
sb.Append(",")
End If
If i < tempB.Length Then
sb.Append(b(i))
End If
sb.Append(vbLf)
Next
'this is the result
Dim combined As String = sb.ToString()
Edit answer to remove line feeds
Another Edit after string result edited
Another edit to make it VB.NET

I would use the wonderful Linq Zip! (I would first String.Split the 2 strings with the line-feed character) and then
Dim column1() As String = {"1", "2", "3", "4"}
Dim column2() As String = {"5", "6", "7", "8"}
Dim mixed= column1.Zip(column2, Function(first, second) first & "," & second)
edit:
oh, and then, if you want to have it back into 1 string, you can either use String.Join but Linq is fun too! So you can write :
Dim mixed= column1.Zip(column2, Function(first, second) first & "," & second)
.Select(i => i.Boo)
.Aggregate((i, j) => i + vbLf + j)
Did I mention that Linq is fun?

Related

Insert separate each digit by delimiter a comma

I have in Textbox1.Text these lines:
27
408
73
49
80
71
70
I want to put a comma between each number separately. I want to do this automatically, put a comma between characters.
like: note: where there are 3 characters, like 408, it will be 40,8 when it is 70, it will be 7,0. this I think I can do, if I have an example code that separates my characters with a comma.
2,7
40,8
7,3
4,9
8,0
7,1
7,0
Code: this code does not work properly. displays many values and incorrectly type 3.3,, 4,5,6,7,78, etc, and many lines. what he shouldn't do.
Dim XStrsLength = TextboxIndex1.Text.Length
Dim XStrs As List(Of String) = New List(Of String)
Dim str As String = TextboxIndex1.Text
Dim last As Integer
For interval As Integer = 1 To XStrsLength
Dim xstr As String = ""
For I As Integer = 0 To str.Length - interval - 1 Step interval
xstr &= str.Substring(I, interval) & ","
last = I
Next
xstr &= str.Substring(last + interval)
XStrs.Add(xstr)
Next interval
TextBox1.Text = String.Join("", XStrs)
Try this code. It parses whatever is in TextBox1 and places the result into TextBox2:
Private Sub Test()
Dim pieces() As String = TextBox1.Text.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Dim str As String = ""
For Each piece As String In pieces
str &= piece.Insert(piece.Length - 1, ",") & ControlChars.CrLf
Next
TextBox2.Text = str.Substring(0, str.Length - 2)
End Sub
you can use String.Insert(Integer, String) to insert a comma:
if the line has 2 chars: yourline.Insert(1, ",")
else if it has 3 chars: yourline.Insert(2, ",")

Remove description from list in RichTextBox

I have a list of accounts in a RichTextBox:
username:password|description
username1:password1|description
username3:password3|description3
username4:password5|description6
username1:password3|description5
username2:password4|description5
username2:password3|description3
The amount of rows in the list is arbitrary.
I want to remove the "|description" part of each row. Any suggestions?
Use a Regex
Dim rx As New System.Text.RegularExpressions.Regex("\|(.+)")
RichTextBox1.Text = rx.Replace(RichTextBox1.Text, "")
This will remove any number of characters beginning with and including the pipe |. It will work with a RichTextBox having a Text property of any length (any number of rows).
If you want to leave just the password, you can do two replaces
Dim rx As New System.Text.RegularExpressions.Regex("\|(.+)")
RichTextBox1.Text = rx.Replace(RichTextBox1.Text, "")
rx = New Regex("(.+):")
RichTextBox1.Text = rx.Replace(RichTextBox1.Text, "")
Just remove anything up to and including the :
And just in case you ask, you could also remove the password with this
Dim rx As New System.Text.RegularExpressions.Regex(":(.+)\|")
RichTextBox1.Text = rx.Replace(RichTextBox1.Text, "|") ' or replace with ":"
Without regex?
Dim lines = RichTextBox1.Text.Split(vbLf)
Dim elements = lines.Select(Function(line) line.Split({":"c, "|"c}))
' password
RichTextBox1.Text = String.Join(vbLf, elements.Select(Function(element) element(1)))
' username:password
RichTextBox1.Text = String.Join(vbLf, elements.Select(Function(element) element(0) & ":" & element(1)))
' you get the idea
Spilt the text into an array then rewrite the items in the array to the text box
Dim arr1() As String=Split (rtb.Text,VbLf)
Dim finalstring As String= ""
For x As Integer=0 to Ubound(arr1) - 1
Dim arr2 () As String = Split (arr1 (x), "|")
finalstring += arr2 (0) & vbNewLine
Next
rtb.Text= finalstring
to show just the password; use the same technique as above
Dim arr1() As String=Split(rtb.Text,VbLf)
Dim finalstring As String= ""
For x As Integer = 0 to Ubound(arr1) - 1
Dim arr2 () As String = Split (arr1 (x), "|")
For x As Integer=0 to Ubound(arr2) - 1
Dim arr3 () As String = Split (arr2 (0), ":")
finalstring += arr3 (1) & vbNewLine
Next
Next
rtb.Text= finalstring

Pairs from a list of string in vb.net

I'm trying to pair elements of a list of string together, ie, out of a list of a,b,c,d; I'd like to see ab, bc, cd. Here's the code I have so far:
Dim C As New List(Of String)
For i = 0 To S.Count - 1
For j =i + 1 To S.Count - 1
C.Add("{" & S(i) & "," & S(j) & "}")
Next
Next
Dim value As String = String.Join(",", C)
TextBox2.Text = value
Currently, this code returns a power set of {a,b},{a,c},{a,d},{b,c},{b,d},{c,d}..
Is there an efficient way to do this?
It looks like you only want adjacent pairs so this should work for you:
Dim C As New List(Of String)
For i = 0 To S.Count - 2
C.Add("{" & S(i) & "," & S(i + 1) & "}")
Next
Dim value As String = String.Join(",", C)
TextBox2.Text = value
If so, simpler still:
Dim C As New List(Of String)
For i = 0 To S.Count - 2
C.Add(String.Format("{{{0},{1}}}", {S(i), S(i + 1)}))
Next
TextBox2.Text = String.Join(",", C)
As simple as that:
Dim input As String = "a,b,c,d"
Dim output As String = String.Empty
Dim toggleIgnoreComma As Boolean = True
For Each s As String In input
If s.Equals(","c) Then
If toggleIgnoreComma Then
toggleIgnoreComma = False
Continue For
Else
toggleIgnoreComma = True
End If
End If
output += s
Next

Split string by " " in VB.NET

Let’s say that this is my string:
1 2 3
I want to split the string by " " (space) and display every time part of my string.
This will do what you need:
Dim str As String = "1 2 3"
Dim strarr() As String
strarr = str.Split(" "c)
For Each s As String In strarr
MessageBox.Show(s)
Next

for loop for a string variable

this is my code -
for i as integer = 0 to rows.count - 1
output &= "Name =" & row(i)("Name")
output &= "lastName =" & row(i)("lastName")
... 50 more fields
next
i need the output to be like this
Applicant1Name = MikeApplicant1lastName = ditkaApplicant2Name = TomApplicant2lastName = Brady ...
how do i do this without putting the following code 50 times -
output &= "Applicant" & i.tostring() + 1 &"Name =" & row(i)("Name")
... and so on.
is there a way to make a for loop and run applicant 1,2,3,4.... in one shot?
thanks
Try:
Dim output as New StringBuilder("")
For i as Integer = 0 To rows.Count - 1
output.append("Applicant" + i.ToString())
Foreach(col as DataColumn in dt.Columns) ' The datatable where your rows are
Dim colName as string = col.ColumnName
output.append(colName & "=" & rows(i)(colName).ToString())
Next
If i < rows.Count - 1 Then output.Append("|")
Next
StringBuilder is faster for string concatenations, and if you keep your rows in a datatable (which I assume is happening because that's how it looks like you're accessing them), then you can just iterate through the columnnames at the top level.
You really cant as you are trying to append 50 different fields.
The only thing you can shorten is the variable name:
Dim strLN as String = row(i)("lastName")
Dim strFirstName as String = row(i)("firstName")
Then you simply put it all together
output &= strLN & strFirstName...etc
looks like you want to create an array of all the fields you have and then include a nested loop.
Dim fields As String() = {"Name", "LastName", "SSN", "Birthdate"}
Dim output As String = ""
For i As Integer = 1 To rows.count
For Each field As String In fields
output = String.Concat(output, "Applicant ", i, field, "=", row(i)(field), " ")
Next
Next