-
Aug 21st, 2013, 10:59 AM
#1
Thread Starter
Lively Member
replace string at a specific location in a txt file
Hi,
I have opened a txt file in a richbox and I want to know how can I replace a string inside the file?
All examples I've found up to now it shows how to replace a specific "word".
But my "word" will be different every time I open the file, so I want to replace a location of the string.
I always want to modify the first line in the richbox; and replace 00A6 with the content of another textbox;
and replace 0508 with the content of a second textbox.
:100010 00A605080C280005040209020929030307A4
Can anybody tell me how to do it?
Thank you.
-
Aug 21st, 2013, 11:07 AM
#2
Re: replace string at a specific location in a txt file
I hate to be a party pooper and all that but if the user is going to all the trouble of typing an entry into a textbox and a second entry in another textbox and then pressing a button or something to trigger the transfer, wouldn't it just be easier to select the eight characters and simply type the entries in directly to replace them?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Aug 21st, 2013, 11:44 AM
#3
Member
Re: replace string at a specific location in a txt file
Try remove and insert after you find the position of what you need, replace is not the best idea if the value is constantly changing. Let's say we put all the text into textbox1 and the replacement in textbox2.
Code:
Dim x As Integer
x = InStr(1, Textbox1.Text, "00A6") + 3
TextBox1.Text = TextBox1.Text.Remove(x, 4)
TextBox1.Text = TextBox1.Text.Insert(x, Textbox2.Text)
-
Aug 21st, 2013, 12:23 PM
#4
Re: replace string at a specific location in a txt file
Are 00A6 and 0508 change?
-
Aug 22nd, 2013, 03:43 AM
#5
Thread Starter
Lively Member
Re: replace string at a specific location in a txt file
Originally Posted by ident
Are 00A6 and 0508 change?
Of course they change,
as I said the "word" that I want to change will be different every time I open the file.
daniel64 thanks for the sample code, but this is replacing a specific "word".
I need something to replace a "location" in the file.
Regards.
-
Aug 22nd, 2013, 07:54 AM
#6
Hyperactive Member
Re: replace string at a specific location in a txt file
Will the first string to remove always be 4 characters long, and the second always be 4 characters long? If so (might need some modification),
By modifying parts of the code above to do some string manipulation:
Code:
Dim newString As String = oldString.Substring(0,7) & textBox1.Text & oldString.Remove(0,15) & textBox2.Text & oldString.Remove(0,19)
-
Aug 22nd, 2013, 12:09 PM
#7
Re: replace string at a specific location in a txt file
00A60508 is hardly a word. Just because something may seem clear to you it may not be to others. I asked a question to varify my concern before spending my own personal free time writing code that may be wrong. I completely agree with Dunfiddlings point about application logic here.
Regular Expressions are not always the answer. They are not magic and do have over heads. I like to practice using regex as much as possible lately. It's for my own learning so take the answer as you wish.
vb Code:
Imports System.Text.RegularExpressions Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Max length 4 chars Me.PatternOneTextBox.MaxLength = 4 Me.PatternTwoTextBox.MaxLength = 4 ' This is our magic text file ' IO.File.ReadAllLines(String, String) Dim magicFile = {":10001000A605080C280005040209020929030307A4", "Magic Line Two", "Magic Line Three", "Magic Line Four"} ' Display lines Me.FileContentsRichTextBox.Lines = magicFile End Sub Private Sub PerformTaskButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles PerformTaskButton.Click Dim tb1 As TextBox = Me.PatternOneTextBox Dim tb2 As TextBox = Me.PatternTwoTextBox If tb1.Text.Trim <> String.Empty AndAlso tb2.Text.Trim <> String.Empty Then ' Create a new collection of lines Dim lines As New List(Of String)(Me.FileContentsRichTextBox.Lines) Dim pattern As String = tb1.Text.Trim & tb2.Text.Trim ' Using regex replace with a positive look behind ' Insert the new replaced line using regular expressions ' See post for explanation. lines.Insert(0, Regex.Replace(lines(0), "(?<=^.{7}).{8}", pattern)) ' Remove original line from the collection. lines.RemoveAt(1) ' Convert the collection to a string array. Me.FileContentsRichTextBox.Lines = lines.ToArray End If End Sub End Class
Assert that the regex below can be matched, with the match ending at this position (?<=^.{7})
Assert position at the beginning of the string ^
Match any single character that is not a line break character .{7}
Exactly 7 times {7}
Match any single character that is not a line break character .{8}
Exactly 8 times {8}
Last edited by ident; Aug 22nd, 2013 at 12:12 PM.
Reason: tag misstake
-
Aug 22nd, 2013, 12:17 PM
#8
Member
Re: replace string at a specific location in a txt file
edit: ident just joined to help you, removing my comment
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|