| | |
Search and Replace
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
I have written and application to search through a text file and find the following data and pull out the 5th varible and place in a text box.
P_SPEED
0 10 1100 0 .01 23 0
The next issue I am having to input, into a text box, a integer and replace the 5th variable in this data. There are several instances of this variable in the file and all will required to be replaced with the new value. After the file will need to be saved.
Any suggestion as to searching for this data and replacing it?
Thanks in advance for any help/direction....
P_SPEED
0 10 1100 0 .01 23 0
The next issue I am having to input, into a text box, a integer and replace the 5th variable in this data. There are several instances of this variable in the file and all will required to be replaced with the new value. After the file will need to be saved.
Any suggestion as to searching for this data and replacing it?
Thanks in advance for any help/direction....
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
I am new with vb.net and vb coding in general. So, it took me a while visiting several vb.net forums to help me through this development. In my ingnorance, I thought I could read through the text file find the data I needed and write it at that instance and be done. Rather what has to be done is read each line replace each line in a temp file, while replacing the data I need to replace. Once I have gone through the entire the text file, and replaced the data I needed, I then write the entire temp file to the directory. In this case I overwrote the file I pulled the original data from. For those who are software developers, this code may be very rough as it is my first attemp of creating an application.
The data below is a section of the data I had to go through in the text file. Going down through data I could not use the first 'P_SPEED' data string I came to, rather I had to find the second occurrance and every occurrance after to change the data string that followed. That is why I used a search for the 'SEQUENZs' first and then 'P_SPEED'.
#DataDir=.\
#1=.\VTX\SAMPLE.VTX
#2=.\FTX\SAMPLE.FTX
#3=.\STX\SAMPLE.STX
#4=.\CDS\SAMPLE.CDS
#5=.\PAR\Sample.par
#VERSION = v2.33.3, 08.08.2006 rd
#P_SPEED = W0
#OBJECTIV = 254
#KOORDORIENT = 0
P_SPEED
#[W0]
0 500 100 4000 .01 18 0
#<PARENT_DEF>
M
-52.189 16.33 0
SEQUENZs
50 0 0
P_GALVO
#<VOLATILE>
#[galvo1]
.5 .5 .4 0 .2 0
P_SPEED
0 10 1100 0 .01 25.3 0
C
-52.241 16.692 0 -52.089 17.357 0 -51.886 17.661 0
P_SPEED
0 10 1000 0 .01 25.3 0
L
-44.563 25.588 0
P_SPEED
0 10 1100 0 .01 25.3 0
I know this inexperience talking as I expect it to become more like a job in the future, but this experience was like playing my first computer based game and I am hooked and ready to tackle the next application....
Imports System.IO
Module WriteDateFile
Sub Main()
'- Location of the Text file
Dim fileName As String = WattChangeForm.Files.SelectedItem
'- Set the line counter
Dim lineNumber As Integer = 0
'- Open the text file/stream
Dim sr As StreamReader = New StreamReader(fileName)
'- Initialize name variables
Dim sequenzs As String = ""
Dim pspeed As String = ""
Dim speed As String = ""
Dim temp As String = ""
Dim textline As String = ""
Dim strFull As String = ""
Dim strOriginal As String = ""
Dim strReplace As String = ""
Dim fline
'- Cycle thru the text file 1 line at a time pulling
'- substrings into the variables initialized above
Do
textline = sr.ReadLine()
temp = temp + textline + vbCrLf
'- If it goes past the last line of the file,
'- it drops out of the loop
If textline <> Nothing Then
Try
sequenzs = textline.Substring(0, 8).Trim()
If sequenzs = "SEQUENZs" Then
Do
textline = sr.ReadLine()
temp = temp + textline + vbCrLf
If textline = "P_SPEED" Then
pspeed = textline.Substring(0, 7).Trim()
If pspeed = "P_SPEED" Then
textline = sr.ReadLine()
fline = Split(textline, " ")
'replace the current from the input current text box
'write it to the file
strFull = textline
strOriginal = fline(5)
strReplace = WattChangeForm.ChangeCurrentbox.Text
strFull = strFull.Replace(strOriginal, strReplace)
textline = strFull
temp = temp + textline + vbCrLf
End If
End If
Loop Until textline = Nothing
End If
Catch ex As Exception
'WattChangeForm.RichTextBox1.Text = "Error nothing to read"
End Try
End If
Loop Until textline = Nothing
'- Close the file/stream
sr.Close()
'after building the temp file with the new values, overwrite the
'.lso with the temp file data
Dim SW As StreamWriter = New StreamWriter(fileName)
SW.Write(temp)
SW.Close()
WattChangeForm.ChangeCurrentbox.Text = ""
WattChangeForm.RichTextBox1.Text = ""
Call ReadDataFile.Main()
End Sub
End ModuleThe data below is a section of the data I had to go through in the text file. Going down through data I could not use the first 'P_SPEED' data string I came to, rather I had to find the second occurrance and every occurrance after to change the data string that followed. That is why I used a search for the 'SEQUENZs' first and then 'P_SPEED'.
#DataDir=.\
#1=.\VTX\SAMPLE.VTX
#2=.\FTX\SAMPLE.FTX
#3=.\STX\SAMPLE.STX
#4=.\CDS\SAMPLE.CDS
#5=.\PAR\Sample.par
#VERSION = v2.33.3, 08.08.2006 rd
#P_SPEED = W0
#OBJECTIV = 254
#KOORDORIENT = 0
P_SPEED
#[W0]
0 500 100 4000 .01 18 0
#<PARENT_DEF>
M
-52.189 16.33 0
SEQUENZs
50 0 0
P_GALVO
#<VOLATILE>
#[galvo1]
.5 .5 .4 0 .2 0
P_SPEED
0 10 1100 0 .01 25.3 0
C
-52.241 16.692 0 -52.089 17.357 0 -51.886 17.661 0
P_SPEED
0 10 1000 0 .01 25.3 0
L
-44.563 25.588 0
P_SPEED
0 10 1100 0 .01 25.3 0
I know this inexperience talking as I expect it to become more like a job in the future, but this experience was like playing my first computer based game and I am hooked and ready to tackle the next application....
Last edited by Ancient Dragon; Jan 24th, 2009 at 4:02 pm. Reason: add code tags
I'm fully aware that this thread is quite old now (I found it on google), but I wanted to post something here:
Here is my own search and replace function(s) for strings.
Just read in a textfile and read every line as a string (and modify that string, then replace the line in that text file with the new string), until the end of file, using these two functions together to do the replacements.
It's only flaw is when the searchitem to replace is also contained in the actual replacement:
e.g.
Will still return "redundant box is redundant", but what I would ideally want it to return is "non-volatile redundant, hairy, and by all rights awesome box is non-volatile redundant, hairy, and by all rights awesome".
It's a flaw that I would like to fix, but I haven't come up with a solution yet (which means no good search-and-replace in my text editor yet).
Here is my own search and replace function(s) for strings.
Just read in a textfile and read every line as a string (and modify that string, then replace the line in that text file with the new string), until the end of file, using these two functions together to do the replacements.
VB.NET Syntax (Toggle Plain Text)
' My own equivalent of VB.NET's inbuilt "instr" function. Because I can Function InString(ByRef sMas As String, ByRef sDum As String) As Integer For i As Integer = 1 To sMas.Length Step 1 If Mid(sMas, i, sDum.Length) = sDum Then Return i End If Next Return 0 End Function Function SearchAndReplace(ByRef sMas As String, ByRef sDum As String, ByRef sRep As String) As String Do Until InString(sMas, sDum) = 0 Or InString(sRep, sDum) > 0 For i As Integer = 1 To sMas.Length Step 1 If Mid(sMas.ToLower, i, sDum.ToLower.Length) = sDum.ToLower Then sMas = (Mid(sMas, 1, i - 1) + sRep + Mid(sMas, i + sDum.Length, sMas.Length - i + 1 + sDum.Length)) Exit For End If Next Loop Return sMas End Function
e.g.
VB.NET Syntax (Toggle Plain Text)
Call System.Console.WriteLine(SearchAndReplace("redundant box is redundant", "redundant", "non-volatile redundant, hairy, and by all rights awesome"))
It's a flaw that I would like to fix, but I haven't come up with a solution yet (which means no good search-and-replace in my text editor yet).
Last edited by NightCrawler03X; Jan 24th, 2009 at 1:05 pm. Reason: Added certain details
VB.NET Syntax (Toggle Plain Text)
' Replaces (with sRep) only the first instance of sDum in the string sMas Function searchReplace(ByRef sMas As String, _ ByRef sDum As String, _ ByRef sRep As String) As String Try : sMas = Mid(sMas, 1, InStr(sMas, sDum) - 1) _ + sRep _ + Mid(sMas, InStr(sMas, sDum) + sDum.Length _ , sMas.Length - InStr(sMas, sDum) + 1 + sDum.Length) Catch ex As Exception : End Try : Return sMas End Function ' Replaces (with sRep) all instances of sDum in the string sMas Function allSearchReplace(ByRef sMas As String, _ ByRef sDum As String, _ ByRef sRep As String) As String Dim sTemp As String = "" : Do Until InStr(sMas, sDum) = 0 Try : sTemp += Mid(sMas, 1, InStr(sMas, sDum) - 1) + sRep _ : sMas = Mid(sMas, InStr(sMas, sDum) + sDum.Length _ , sMas.Length - InStr(sMas, sDum) + 1 + sDum.Length) Catch ex As Exception : End Try : Loop : Return sTemp + sMas End Function
![]() |
Similar Threads
- Search and replace multiple file (Python)
- Open, search, replace data, save, close .txt file (VB.NET)
- string replace (C++)
- Datagrid Search and Replace (Visual Basic 4 / 5 / 6)
- Quick search & replace with c-strings? (C)
- search and replace (Java)
- XHTML Complient parser? (HTML and CSS)
- Using Search Engine Friendly PHP URLs (PHP)
Other Threads in the VB.NET Forum
- Previous Thread: DataGridView Problem
- Next Thread: send & receive sms
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2005 2008 access account application arithmetic array arrays basic bing button buttons c# center check checkbox code convert crystalreport data database datagrid datagridview date dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy images inline input insert intel internet listview mobile monitor ms net networking objects output panel passingparameters picturebox picturebox1 port position print printing problem project read remove save searchbox searchvb.net select serial server shutdown soap sorting survey table tcp temperature text textbox timer timespan toolbox trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet visual visualbasic visualbasic.net visualstudio2008 web webbrowser winforms wpf year





