I have a string filled with the contents of a textbox (pretty large).
I want to search through it and display all occurances of this word. In addition I need the searchresult to display some charachters in the string before and after the actual searchterm to get the context for the word.
The code below is part of a code that takes keywords from a listbox one by one using For Each. The code displays the first occurance of a word together with the characters in front and after the word - and stop there. It will also display "no Match for: searched word" if not found.
As stated in the subject of this question - I need it to search the whole string and display all matches for a particular word together with the surrounding characters.
Where = InStr(txtScrape.Text, Search)
If Where <> 0 Then
txtScrape.Focus()
txtScrape.SelectionStart = Where - 10
txtScrape.SelectionLength = Where + 50
Result = txtScrape.SelectedText
AllResults = AllResults + Result
Else
AllResults = AllResults + "No Match for: " & item
End If
I recommend that you can split the string into long sentences by special symbols, such as , : ? .
Split(Char[])
You can refer to the following code.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = ""
Dim Index As Integer
Dim longStr() As String
Dim str = TextBox3.Text
longStr = TextBox1.Text.Split(New Char() {CChar(":"), CChar(","), CChar("."), CChar("?"), CChar("!")})
Index = 0
For Each TheStr In longStr
If TheStr.Contains(str) Then
RichTextBox1.AppendText(longStr(Index) & vbCrLf)
End If
Index = Index + 1
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "....."
End Sub
End Class
Result:
Try like this:
Dim ArrStr() As String
Dim Index As Integer
Dim TheStr As String
Dim MatchFound As Boolean
MatchFound = False
ArrStr = Split(txtScrape.text," ")
Index = 1
For Each TheStr In ArrStr
If TheStr = Search Then
Console.WriteLine(Index)
MatchFound = True
End If
Index = Index + 1
Next
Console.WriteLine(MatchFound)
Inside the If statement you will get the index there. And MatchFound is the Boolean value if match found.
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 am trying to put a value based on two cascading combo boxes from a database table into a text box. Filling the two cascading combo boxes works. However when I try to put the result in the text box I run into trouble. I am using the following code :
Private Sub CmbPlaasnaam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbPlaasnaam.SelectedIndexChanged
If CmbAliasnaam.SelectedIndex > -1 AndAlso CmbPlaasnaam.SelectedIndex > -1 Then
Dim aliasnaam As String =
CmbAliasnaam.Items(CmbAliasnaam.SelectedIndex).ToString
Dim plaasnaam As String =
CmbPlaasnaam.Items(CmbPlaasnaam.SelectedIndex).ToString
Dim qry = From zc As SkeduleringsDatabasis6DataSet.OesskattingsRow In SkeduleringsDatabasis6DataSet.Oesskattings
Where zc.Aliasnaam = aliasnaam AndAlso
zc.Plaasnaam = plaasnaam
Select zc.Plaasnommer
txtPlaasnommer.Text = zc.plaasnommer
End If
End Sub
Use this line :
txtPlaasnommer.Text = qry.FirstOrDefault.ToString()
zc on your last line does not exists
you should use qry variable
like:
if qry.Count>0 Then
txtPlaasnommer.Text = qry.first
End If
zc is the inner linq variable and qry is the result that you should use
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
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)