Convert multiline string into a single line - vb.net

I would like to do the following thing but I can't - Textbox1.Text
line 1 = 1 2
line 2 = 3 4
line 3 = 1 7
line 4 = 4 9
I want to return this to look like this: yes, and with doubles like 1 1 and so on
Textbox1.Text >Expected Output
line 1 = 1
line 2 = 2
line 3 = 3
line 4 = 4
line 5 = 1
line 6 = 7
line 7 = 4
line 8 = 9
each digit must be on a separate line. that is, on each line that digit, there are now 2 digits on a line.
Code:
source = source.Replace(vbLf, "").Replace(vbCr, "")

You can split original text into values and then combine them together into new lines of text
textbox.Lines = textbox.Lines.
SelectMany(Function(ln) ln.Split(" "c, StringSplitOptions.RemoveEmptyEntries).
ToArray()

each digit must be on a separate line. that is, on each line that
digit, there are now 2 digits on a line.
I'm assuming you don't actually have line x = in your TextBox.
Rather you have:
1 2
3 4
1 7
4 9
and want:
1
2
3
4
1
7
4
9
If so, here's a one-liner that can do it for you...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Lines = String.Join(" ", TextBox1.Lines).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
End Sub

Related

How to solve this triangle problem in Visual Basic? I don't know if I'm doing it right

Make a program that reads a positive integer no greater than 10, and prints a triangle of numbers as follows:
If the number read were 5, then it should print:
1
22
333
4444
55555
The program must reread another number until the number entered is greater than 10.
I have tried this, but I don't know if it is correct:
Dim num As Integer
Dim i As Integer
num = InputBox(" enter a number")
For i = 1 To num Step 1
If i = 1 Then
ListBox1.Items.Add(1)
ElseIf i = 2 Then
ListBox1.Items.Add(22)
ElseIf i = 3 Then
ListBox1.Items.Add(333)
ElseIf i = 4 Then
ListBox1.Items.Add(4444)
ElseIf i = 5 Then
ListBox1.Items.Add(55555)
ElseIf i = 6 Then
ListBox1.Items.Add(666666)
ElseIf i = 7 Then
ListBox1.Items.Add(7777777)
ElseIf i = 8 Then
ListBox1.Items.Add(88888888)
ElseIf i = 9 Then
ListBox1.Items.Add(999999999)
ElseIf i = 10 Then
ListBox1.Items.Add(1010101010101010101010)
End If
Next

Eliminate Duplicate from a Textbox

I have Textbox1.Lines (separated by a space)
5 7 10 12 12
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
1 1 7 14 19
2 4 7 8 10
How should I remove duplicates from each line and display something like this:
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
2 4 7 8 10
that is, to remove from the entire line that contains a duplicate number on the same line.
Private Sub BttGamblerInput_Click(sender As Object, e As EventArgs) Handles BttGamblerInput.Click
On Error Resume Next
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TxtGamblerImput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Not worked.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each line In TxtGamblerOutput.Text
Dim originalArray = TxtGamblerOutput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Dim checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length Then
TxtGamblerOutput.Text = checkArray
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
End Sub
Code 3:
Dim numbers() As Integer = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}
' get the duplicates
Dim dups As New Dictionary(Of Integer, Integer) ' number, count of number
For i = 0 To numbers.Length - 1
If dups.ContainsKey(numbers(i)) Then
dups(numbers(i)) += 1
Else
dups.Add(numbers(i), 1)
End If
Next
' display them
For Each dup In dups
If dup.Value > 1 Then
Console.WriteLine(dup.Key)
End If
Next
Console.ReadLine()
I haven't coded or tested anything, but you can use the Distinct method from List(of T) and compare the length of the output to the current List. Another approach is to use an ArrayList (c.f. https://stackoverflow.com/a/13428210/9101981 for the bones of getting unique elements).
Pseudo-ish Code using an Array:
for each line in the text input
originalArray = line.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length then
' Your code here to display the valid line
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
Some additional reading:
https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8
and, in particular, note the recommendation to use List.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8
noting that this is the Linq Distinct documentation

multiplication table in a textbox

you will probably have to run the program to understand, but i want my loop:
For i = 1 To final
txttable.Text += vbCrLf + CStr(i) + " "
Next
to be in the same line as the rest.
so it looks something like this if i enter 5
5
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 8 12 15
... to 5
txttable.Text is a textbox
maybe i will have to rewrite some of my code to make it work, dont be shy if you have any suggestions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim final As Integer
Integer.TryParse(txtfinal.Text, final)
Select Case final
Case 2 To 20
txttable.Text = (final).ToString + vbCrLf
For i = 1 To final
txttable.Text += vbCrLf + CStr(i) + " "
Next
For i = 1 To final
txttable.Text += vbTab + CStr(i) + " "
Next
txttable.AppendText(Environment.NewLine)
txttable.AppendText(Environment.NewLine)
For ligne As Integer = 1 To final
For col As Integer = 1 To final
txttable.Text += vbTab + (ligne * col).ToString
Next
txttable.Text += vbCrLf
Next
Case Else
MessageBox.Show("veuillez entrez une valeur entre 2 et 20 inclusivement")
txtfinal.Clear()
txtfinal.Focus()
End Select
End Sub
End Class
Try something more like...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim final As Integer
If Integer.TryParse(txtfinal.Text, final) Then
If final >= 2 AndAlso final <= 20 Then
Dim sb As New System.Text.StringBuilder
sb.AppendLine(final.ToString)
For col As Integer = 1 To final
sb.Append(vbTab & col.ToString() & " ")
Next
sb.Append(vbCrLf & vbCrLf)
For row As Integer = 1 To final
sb.Append(row.ToString)
For col As Integer = 1 To final
sb.Append(vbTab & (row * col).ToString)
Next
sb.Append(vbCrLf)
Next
txttable.Text = sb.ToString
Exit Sub
End If
End If
MessageBox.Show("veuillez entrez une valeur entre 2 et 20 inclusivement")
txtfinal.Clear()
txtfinal.Focus()
End Sub
Which produced:
5
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
when 5 was entered.

How to count the number of spaces and characters in vb.net?

I want to count the number of spaces and characters inputted in a textbox field.
Example
textbox1.text - " 1 2 3 4 5 6 7 a b c"
I want to count the number of spaces and the characters
Then the result must be..
Dim spaces as integer, char as integer
spaces = 10 , char = 10
Dim spaceCount, lettercount As Integer
spaceCount= 0
lettercount = 0
Dim s As String = " 1 2 3 4 5 6 7 a b c"
For Each c As Char In s
If c = " " Then
spaceCount+= 1
Else
lettercount += 1
End If
Next
MsgBox(charcount)
MsgBox(lettercount)
For Each will iterate each character within the string then it will check whether c is a space or not. if it is a space then increment the spaceCount else increment the lettrtCount
You can do as you want in a sort way.
Try it
Dim count As Integer = 0
textbox1.text = " 1 2 3 4 5 6 7 a b c"
count = textbox1.text.Split(" ").Length -1
Dim longstring As String = "aab c d e"
Dim TotalLength As Integer = longstring.Length
Dim TotalSpaces() As String = Split(longstring, " "
Dim TotalChars As Integer = TotalLength - (TotalSpaces.Length - 1)

how to tokenize chart data for csv file in classic asp

Hi below is the data sample for chart data which I read from URL
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
Now in this I want to save this data into a csv file as below
Time A B c
25 3 1 8
75 8 9 7
100 6 7 3
125 7 8 9
150 5 4 56
175 3 7 9
200 2 2 111
225 7 5 8
Actually I have to first read the data into an array in which each element will contain one line of the data file, Now I will have to split the each element by space and store it in a two dimensional array, now for each ith index of each array I have to create a coma separated line then put "\n" at the end of line , and at last save this to a csv file . I don't know much about the syntax of vb-script and classic asp that's why I am facing this problem . Please help me
To get you started:
Sub doReOrder(sFSpecI, sFSpecO, sCol1)
Dim oTS : Set oTS = goFS.OpenTextFile(sFSpecI)
Dim sData : sData = oTS.ReadLine() ' ARRAY ' ' 3 8
Dim aParts : aParts = Split(sData, " ")
Dim nCols : nCols = CByte(aParts(3)) ' Count vs UBound vs Data!
Dim nRows : nRows = CByte(aParts(4))
Dim nRows2 : nRows2 = nRows + 1
oTS.SkipLine ' Y
' get table in one string, prepend col1 name, clean '
sData = "'" & sCol1 & "' " & Replace(oTS.ReadAll(), vbCrLf, " ")
sData = Trim(Replace(sData, "'", """"))
' get table in flat array
aParts = Split(sData, " ")
oTS.Close
Set oTS = goFS.CreateTextFile(sFSpecO, True)
' WScript.Echo Join(aParts, "|")
ReDim aData(nCols) ' hold one (new) row to prep for Join
Dim nRow
For nRow = 0 To nRows
Dim nCol
For nCol = 0 To nCols
' magic column hopping
aData(nCol) = aParts(nRow + nCol * nRows2)
Next
oTS.WriteLine Join(aData, ",")
Next
oTS.Close
End Sub
Test:
Dim sFSpecI : sFSpecI = "..\Data\f1.txt"
Dim sFSpecO : sFSpecO = "..\Data\f1.csv"
Dim sCol1 : sCol1 = "Time" ' dangerous - possibly reserved in SQL
WScript.Echo goFS.OpenTextFile(sFSpecI).ReadAll()
doReOrder sFSpecI, sFSpecO, sCol1
WScript.Echo goFS.OpenTextFile(sFSpecO).ReadAll()
Output:
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
"Time","A","B","C"
25,3,1,8
75,8,9,7
100,6,7,3
125,7,8,6
150,5,4,56
175,3,7,9
200,2,2,111
225,7,5,8

Categories