Well, I need to read columns and rows from a .txt
and get only the data I need.
Example: The file "sample.txt" has the following content
C = Columns
R = Rows
C1 C2 C3
R1 0 0 0 3694440008091082330089 ALBINA HANSEN OF OLMEDO 00000075000000022091401520117873
I need the following data for each row:
0003694440008091082330089 ALBINA HANSEN DE OLMEDO 00000075000000022091401520117873
(The series of numbers and letters above the value of the first line)
First Value: Starts at C19 to C25 (2330089)
Second Value: Starts at C28 to C50 (ALBINA HANSEN DE OLMEDO)
Third Value: Starts at C64 to C68 (75000)
Fourth Value: Starts at C73 to C78 (220914)
Fifth Value: It is only the C80 column (1)
And I need to display the data in a message box:
I have the following code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\cogentvaio\Desktop\Banco Itau\TXT-FOMENTO ANGELIUS.txt")
MsgBox(fileReader)
End Sub
End Class
This allows me to read the entire contents of .txt
Hopefully you can help me with this, I'm learning to program in vb.net.
I'll show you my code, I can not see the boundaries made with substrinng, I used the code you suggested,
Imports System
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\cogentvaio\Desktop\Banco Itau\ejemplo\-TXT-ITAU ANGELIUS.txt")
Dim line As String
For Each line In fileReader
If line.Length > 80 Then
Dim c1 = line.Substring(18, 7)
Dim c2 = line.Substring(28, 50)
Dim c3 = line.Substring(64, 68)
Dim c4 = line.Substring(73, 78)
Dim c5 = line.Substring(80)
End If
Next
MessageBox.Show(fileReader)
End Sub
End Class
And what I got was this
IMAGE_PHOTO
I don't know where is my mistake
string.Substring is your friend here. This method extracts substring from a string starting from a char index and getting back the requested number of chars (length). Also remember that an array (a string is an array of chars) starts always at index zero, so your values Cx values should be decremented by one.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Instead of ReadAllText, use ReadAllLines to have your input splitted in an array of strings
Dim fileReader = System.IO.File.ReadAllLines(......)
for each line in fileReader
' To avoid nasty exceptions be sure that the line is at least 81 chars long
if line.Length > 80 Then
Dim c1 = line.Substring(18, 7)
Dim c2 = line.Substring(27, 23)
Dim c3 = line.Substring(63, 5)
Dim c4 = line.Substring(72, 6)
Dim c5 = line.Substring(79, 1)
.... process these values....
.... for example ....
Dim newLine = string.Join(" ", c1,c2,c3,c4,c5)
MessageBox.Show(newLine)
End if
Next
End Sub
This changes takes a line, extract the part and creates a new string with only the parts required joined together with a space. Then prints out this new text. Of course, you want a join of all the rows with these subparts you need to add the newLine variable to a List( Of String) an then do a final join of the single rows
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Instead of ReadAllText, use ReadAllLines to have your input splitted in an array of strings
Dim fileReader = System.IO.File.ReadAllLines(......)
Dim subText = New StringBuilder()
for each line in fileReader
if line.Length > 80 Then
....
Dim newLine = string.Join(" ", c1,c2,c3,c4,c5)
subText.Add(newLine)
End if
Next
MessageBox.Show(string.Join(Environment.NewLine, subText))
However your indexes doesn't seem to correctly point to the data required.
This substring extraction seems to find correctly your data
Dim c1 = line.Substring(18, 7)
Dim c2 = line.Substring(26, 23)
Dim c3 = line.Substring(56, 5)
Dim c4 = line.Substring(65, 6)
Dim c5 = line.Substring(72, 1)
Related
I have a list of members with their stats and i need to split it by lines every 5 lines with a seperator.
It goes until 1000+ lines
Code :
Dim newString As String = RichTextBox1.Text.Replace(vbCr, ";").Replace(vbLf, ";")
Dim separator As Char = CChar(";")
Dim sArr As String() = newString.Split(separator)
Dim indexOfSplit As Integer = 4
Dim sFinal As String = Join(sArr.Take(indexOfSplit).ToArray, separator) & vbNewLine &
Join(sArr.Skip(indexOfSplit).ToArray, separator)
RichTextBox2.Text = sFinal
Output :
Desired Output :
The problem with my code is it does the job only for 1 line and I need it to be all lines in the string
The RichTextBox has a Lines property that returns an array of the lines. I used a StringBuilder to build the string for RichTextBox2. The For loop increments by 4 instead of the default 1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ParseRTB()
End Sub
Private Sub ParseRTB()
Dim lines = RichTextBox1.Lines
Dim sb As New StringBuilder
For i = 0 To lines.Count - 4 Step 4
sb.AppendLine($"{lines(i)};{lines(i + 1)};{lines(i + 2)};{lines(i + 3)}")
Next
RichTextBox2.Text = sb.ToString
End Sub
Could do this with LINQ:
RichTextBox1.Lines _
.Select(Function(s,i) New With { .L = i\4, .S = s }) _
.GroupBy(Function(a) a.L) _
.Select(Function(g) string.Join(";"c, g.Select(Function(a) a.S))
This takes the lines of the rtb and projects them to a new sequence that includes a number that is the line number divided by 4 (so Howard's lines get 0, Roma's get 1 etc), then the input is grouped by that number and the grouping (a set of strings) is joined into a single string using semicolons..
Let me start by saying that I'm new to any language of coding besides G-code, and I've researched this until my fingers hurt. I've actually been working on this project for a little over a year now on my own, and this is the first hurdle I haven't been able to find my way around.
I'm creating an editor for cnc G-code, and i'm trying to add a Re-number function to it. I'm using a multi line richtextbox to display the the G-code to the user. I'm trying to edit each line of code that starts with the character "N", and if a line doesn't start with that character then it's left alone.
I figured the best way to do this would be to loop thru the RTB and pass each line into an array. Then I could use an If statement to see if a cell in the array started with the char "N" or in my case "blockLetter". Then use the replace function to correct the line Number.
This is what I have so far.
Dim increment As Integer = txtLNIncrement.Text
Dim blockLetter As String = txtLNStartTxt.Text
Dim count As Integer = 0
Dim block As Integer = count + increment
For Each cell As String In frmNC.NcTextBox.Lines
If cell.StartsWith(blockLetter) Then
Dim newCell As String = cell.Replace(blockLetter, block)
block = block + increment
MessageBox.Show(newCell)
End If
Next
Example of G-code that needs to be renumbered:
N50 M01
N60 T0101 (TOOL NAME)
N70 M41
N80 G96 S350
N90 M03
N100 M08
This is what I want:
N10 M01
N20 T0101 (TOOL NAME)
N30 M41
N40 G96 S350
N50 M03
N60 M08
This is what I'm getting when I run the code above:
1050 M01
2060 T0101 (TOOL NAME)
3070 M41
4080 G96 S350
5090 M03
60100 M08
I believe my issue is that the cell.replace is splitting each cell at the "N" character, and dropping it all together. Thus adding what I want to see in front of the existing numbers, minus the "N" character. How can I overwrite the existing block number to the correct ascending block number, and retain the "N" character? Am I going about this in the correct way, or is there a better way? Any help is greatly appreciated.
Try something like this out:
Private increment As Integer = 10
Private blockLetter As String = "N"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newLine As String
Dim values() As String
Dim lineNumber As Integer = 0
Dim lines As New List(Of String)(NcTextBox.Lines)
For i As Integer = 0 To lines.Count - 1
If lines(i).TrimStart().StartsWith(blockLetter) Then
values = lines(i).TrimStart(" " & blockLetter.ToCharArray).Split(" ")
lineNumber = lineNumber + increment
values(0) = lineNumber
newLine = blockLetter & String.Join(" ", values)
lines(i) = newLine
End If
Next
NcTextBox.Lines = lines.ToArray
End Sub
it's very simple:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim blockLetter As String = "N"
Dim increment As Integer = 1
For i As Integer = 0 To RichTextBox1.Lines.Length - 1 Step 1
Dim fullLine As String = RichTextBox1.Lines(i)
If fullLine.StartsWith(blockLetter) Then
Dim numbering As String = fullLine.Remove(RichTextBox1.Lines(i).IndexOf(" "))
Dim block As Integer = numbering.Substring(1)
Dim newCell As String = blockLetter & block + increment
MessageBox.Show(newCell)
End If
Next
End Sub
Result:
The Label1.Text will increase with button click.
It's all about Substring() starting from index 1 after 'N', so you will get the number.
Good luck with your coding!
I have this code that I have been working on where the whole goal is to get the content of 2 files, sort it in order from lowest to greatest and output it. The error that I have been getting is that the numbers in the array do sort but not in order from lowest to greatest.
I have three text files labelled Input1.txt and Input2.txt and an output text file.
The content in Input1 is as follows:
5
3
2
1
9
12
34
The content in Input2 is as follows:
4
13
16
23
56
-7
Here is the code that I have so far:
Imports System.IO
Public Class Form1
'Calling up a filepath for saving the users input plus altered input [file names]
Public filePath As String = "Input1.TXT"
Public filePath2 As String = "Input2.TXT"
Public outputFile As String = "Output.txt"
Public objReader As New System.IO.StreamReader(filePath)
Public objReader1 As New System.IO.StreamReader(filePath2)
Dim line As String
'Temp Variables
Dim TempS As String
Dim TempY As String
Dim iPass As Integer
Dim iTemp As Integer
'Declaring variable for how many numbers there are in TextFileNumbes.txt (I have listed 6 numbers in this case)
Dim numbers(100) As String
'Declaring variable for creating a counter for reading all the numbers in TextFileNumbers.txt
Dim i As Integer = 0
'Event - To Load Name From TextFile
Public Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
If objReader.Peek() <> -1 Then
'Read one line from the input textfile
TempS = objReader.ReadLine()
'Declaring code for placing the numbers into an array
numbers(i) = TempS
'Add name read to listbox
ListBox1.Items.Add(TempS)
'Add numbers to the listBox titled "Data Combined From Both Files"
ListBox2.Items.Add(numbers(i))
'Increment Array
i += 1
Else
'Prompts User End of File Has Been Reached
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If objReader1.Peek() <> -1 Then
'Read one line from the input textfile
TempY = objReader1.ReadLine()
'Declaring code for placing the numbers into an array
numbers(i) = TempY
'Add name read to listbox
lstBoxInputFile.Items.Add(TempY)
'Declaring code for the numbers/data stored in TextFileNumbers to be passed onto the OriginalData listbox
ListBox2.Items.Add(numbers(i))
'Increment array counter
i += 1
Else
'If file not located, prompts user that the file isn't there
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
'Event - save the user input plus altered text
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
End Sub
'Event to clear output textfile
Public Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
For iPass = 1 To numbers.Length - 1
For i = 0 To numbers.Length - 2
If numbers(i) > numbers(i + 1) Then
iTemp = numbers(i)
numbers(i) = numbers(i + 1)
numbers(i + 1) = iTemp
End If
Next i
Next iPass
Dim sortOut As String
For i = 0 To numbers.Length - 1
If Not String.IsNullOrEmpty(numbers(i)) Then
ListBox3.Items.Add(numbers(i))
End If
Next
MessageBox.Show("The Data has been sorted!")
End Sub
End Class
The output that I have been getting is as follows: -7, 1, 12, 13, 16, 2, 23,3, 34,4, 5, 56, 9 when it should be -7, 1, 2, 3, 4, 5, 9, 12, 13, 16, 23, 34, 56
Your biggest problem that perhaps you're trying to run before you can walk.
You're reading the data from a text file into an array of strings and then trying to compare string values. This line ..
If numbers(i) > numbers(i + 1) Then
Is comparing strings, not integers. What is actually happening when you compare a string to see which is greater is that the first character (not number, it could be an 'a' or '/' or '3') it then compares the value of the character code. and then decides which is a higher value and does this for all the characters in each string that you're comparing. Also if the number of characters in a string is different, then the string with the least number of characters will be deemed less than the longer string. Kind makes sense.
So .. looking at a particular triplet in your list of numbers, "16", "2" and "23"
The comparison code has compared "16" and "2" at some point and because the first characters are "1" and "2", "16" is less than "2".
The comparison between "2" and "23" is slightly different be cause although the first characters are the same, "2" is shorter than "23" and because of this "2" is less than "23"
To compare numbers properly, you have to convert or parse the each string value into a number value and store it as a number - not a string. Have a look at this and compare the differences with your code ..
Public Class Form1
'Calling up a filepath for saving the users input plus altered input [file names]
Public filePath As String = "Input1.TXT"
Public filePath2 As String = "Input2.TXT"
Public outputFile As String = "Output.txt"
Public objReader As New System.IO.StreamReader(filePath)
Public objReader1 As New System.IO.StreamReader(filePath2)
'Temp Variables
Dim TempS As Integer
Dim TempY As Integer
Dim iPass As Integer
Dim iTemp As Integer
'Declaring variable for how many numbers there are in TextFileNumbes.txt (I have listed 6 numbers in this case)
Dim numbers(100) As Integer
'Declaring variable for creating a counter for reading all the numbers in TextFileNumbers.txt
Dim i As Integer = 0
'Event - To Load Name From TextFile
Public Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
If objReader.Peek() <> -1 Then
'Read one line from the input textfile
TempS = Integer.Parse(objReader.ReadLine())
'Declaring code for placing the numbers into an array
numbers(i) = TempS
'Add name read to listbox
ListBox1.Items.Add(TempS)
'Add numbers to the listBox titled "Data Combined From Both Files"
ListBox2.Items.Add(numbers(i))
'Increment Array
i += 1
Else
'Prompts User End of File Has Been Reached
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If objReader1.Peek() <> -1 Then
'Read one line from the input textfile
TempY = Integer.Parse(objReader1.ReadLine())
'Declaring code for placing the numbers into an array
numbers(i) = TempY
'Add name read to listbox
lstBoxInputFile.Items.Add(TempY.ToString)
'Declaring code for the numbers/data stored in TextFileNumbers to be passed onto the OriginalData listbox
ListBox2.Items.Add(numbers(i).ToString)
'Increment array counter
i += 1
Else
'If file not located, prompts user that the file isn't there
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
'Event - save the user input plus altered text
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
End Sub
'Event to clear output textfile
Public Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
For iPass = 1 To numbers.Length - 1
For i = 0 To numbers.Length - 2
If numbers(i) > numbers(i + 1) Then
iTemp = numbers(i)
numbers(i) = numbers(i + 1)
numbers(i + 1) = iTemp
End If
Next i
Next iPass
For i = 0 To numbers.Length - 1
ListBox3.Items.Add(numbers(i).ToString)
Next
MessageBox.Show("The Data has been sorted!")
End Sub
End Class
All your temporary variables now defined as integers. When VB.Net is comparing actual numbers and not strings, 1 is now seen as less than 16. Voila.
The lines that declare the variables line and sortout have been removed in my example because your code doesn't use them.
You'll also notice that I've added .ToString in several places. This isn't strictly necessary as VB will happily convert Integers to Strings in the background, but I've added them to let you know that it happens.
can you help me how to get Index of the same string and replace it one by one with another string?
Here my example code :
For i As Integer = 0 To 10
Dim str As String = "abcd,abcd,abcd,abcd,abcd,abcd,abcd,abcd"
Dim replace As String = "efgh"
Dim value As String
value = str.Replace("abcd", replace)
TextBox4.AppendText(value)
Next
The value will be result : efgh,efgh,efgh,efgh,efgh...
How i can create the result like this :
efgh,abcd,abcd,abcd,abcd,abcd...
for the next loop it will be like this :
abcd,efgh,abcd,abcd,abcd,abcd...
for the next loop it will be like this :
abcd,abcd,efgh,abcd,abcd,abcd...
Thank you
There are lots of ways this could be done. One perhaps inefficient way would be to split the string at the commas, replace the appropriate item in the resulting array, and then join the array back up.
Dim str As String = "abcd,abcd,abcd,abcd,abcd,abcd,abcd,abcd"
Dim replace As String = "efgh"
For i As Integer = 0 To str.Count(Function(x) x = ","c)
Dim strParts = str.Split(","c)
strParts(i) = replace
Dim value As String = String.Join(",", strParts)
Console.WriteLine(value)
Next
Output:
efgh,abcd,abcd,abcd,abcd,abcd,abcd,abcd
abcd,efgh,abcd,abcd,abcd,abcd,abcd,abcd
abcd,abcd,efgh,abcd,abcd,abcd,abcd,abcd
abcd,abcd,abcd,efgh,abcd,abcd,abcd,abcd
abcd,abcd,abcd,abcd,efgh,abcd,abcd,abcd
abcd,abcd,abcd,abcd,abcd,efgh,abcd,abcd
abcd,abcd,abcd,abcd,abcd,abcd,efgh,abcd
abcd,abcd,abcd,abcd,abcd,abcd,abcd,efgh
As #Mark mentioned there are lots of ways this could be done. One of this ways to add and remove ranges in a string is to use the StringBuilder.
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 35 Step 5
Dim sDefaultString As String = "abcd,abcd,abcd,abcd,abcd,abcd,abcd,abcd"
Dim sbText = New StringBuilder(sDefaultString)
sbText.Remove(i, 4)
sbText.Insert(i, "efgh")
TextBox1.AppendText(sbText.ToString & vbNewLine)
Next
End Sub
End Class
New TechGuy on this site. Thought i'd make this resourceful since im getting ready to graduate and want more programming practice. I have a question:
I tried looping through a text file I created and I wanted to replace each multiple of 3 with the word "changed". I created a text file and entered numbers 1-15 on each line. My code is below but for some reason it would only change the number 3 and 13. I tried using this link as a resource (Loop through the lines of a text file in VB.NET) but that wasnt to helpful. Anyway here is my code, can someone help with what i'm doing wrong?
Public Class numbers
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim OpenFilePrompt As New OpenFileDialog
openFilePrompt.ShowDialog()
Dim currentReader As New System.IO.StreamReader(OpenFilePrompt.FileName)
txtInput.Text = currentReader.ReadToEnd
Dim a As String
Dim numbers As New List(Of String)
a = txtInput.Text
numbers.Add(a)
Dim b As Integer
For i = 0 To numbers.Count Step 3
b = b + 3
TextBox2.Text = (numbers.Item(i).Replace(b.ToString, "Changed"))
Next
End Sub
End Class
You are setting numbers (the first item) to a, which is txtInput.Text.
Then you have a one size list which is completely useless!
You should use Mod instead.
Divides two numbers and returns only the remainder.
So just check if Integer Mod 3 equals 0.
Also consider using File.ReadAllLines(String), Val(String), String.Join(String, String()) and File.WriteAllLines(String, String()).
Dim Content() As String = File.ReadAllLines(OpenFilePrompt.FileName)
For Index As Integer = 0 To Content.Length - 1
Dim Number As Integer = Val(Content(Index))
If Number Mod 3 = 0 Then
Content(Index) = "changed"
End If
Next
txtInput.Text = String.Join(vbCrLf, Content)
File.WriteAllLines(OpenFilePrompt.FileName, Content)