Results 1 to 8 of 8

Thread: replace string at a specific location in a txt file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    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.

    :10001000A605080C280005040209020929030307A4
    Can anybody tell me how to do it?

    Thank you.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3
    Member
    Join Date
    Jun 2013
    Posts
    47

    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)

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: replace string at a specific location in a txt file

    Are 00A6 and 0508 change?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    Re: replace string at a specific location in a txt file

    Quote Originally Posted by ident View Post
    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.

  6. #6
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    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)

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object,
    6.                        ByVal e As System.EventArgs) Handles MyBase.Load
    7.         ' Max length 4 chars
    8.         Me.PatternOneTextBox.MaxLength = 4
    9.         Me.PatternTwoTextBox.MaxLength = 4
    10.  
    11.         ' This is our magic text file
    12.         ' IO.File.ReadAllLines(String, String)
    13.         Dim magicFile = {":10001000A605080C280005040209020929030307A4",
    14.                          "Magic Line Two",
    15.                          "Magic Line Three",
    16.                          "Magic Line Four"}
    17.         ' Display lines
    18.         Me.FileContentsRichTextBox.Lines = magicFile
    19.     End Sub
    20.  
    21.     Private Sub PerformTaskButton_Click(ByVal sender As System.Object,
    22.                                         ByVal e As System.EventArgs
    23.                                         ) Handles PerformTaskButton.Click
    24.  
    25.         Dim tb1 As TextBox = Me.PatternOneTextBox
    26.         Dim tb2 As TextBox = Me.PatternTwoTextBox
    27.  
    28.         If tb1.Text.Trim <> String.Empty AndAlso
    29.            tb2.Text.Trim <> String.Empty Then
    30.  
    31.             ' Create a new collection of lines
    32.             Dim lines As New List(Of String)(Me.FileContentsRichTextBox.Lines)
    33.             Dim pattern As String = tb1.Text.Trim & tb2.Text.Trim
    34.             ' Using regex replace with a positive look behind
    35.             ' Insert the new replaced line using regular expressions
    36.             ' See post for explanation.
    37.             lines.Insert(0, Regex.Replace(lines(0), "(?<=^.{7}).{8}", pattern))
    38.             ' Remove original line from the collection.
    39.             lines.RemoveAt(1)
    40.             ' Convert the collection to a string array.
    41.             Me.FileContentsRichTextBox.Lines = lines.ToArray
    42.         End If
    43.     End Sub
    44.  
    45.  
    46. 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}

  8. #8
    Member
    Join Date
    Jun 2013
    Posts
    47

    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
  •  



Click Here to Expand Forum to Full Width