473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Replace whole line of Text from textfile

33
Hi:
If I have text as below in a Text file "Sample.txt":

<STX>jjjjjjjjjjjjjjjjjjjjjjjjjjj<ETX>
<STX>hhhhhhhhhhhhhhhhhh<ETX>
<STX>666666666666666666666666<ETX>
<STX>999999999999999999999999<ETX>
<STX>11111<ETX>
<STX>333333<ETX>
<STX>kkkk<ETX>
<STX>555555<ETX>


How do I Replace line no 5 or any specific line with a new string/text?
Aug 7 '08 #1
1 1286
Curtis Rutland
3,256 Expert 2GB
Read the whole file into a string. Then, you can .Split it around a newline character ( '\n' in c#, not sure about VB). .Split returns an array. So if you want to replace line 5, you would change stringArray[4] to whatever you want. Remember that arrays are zero based.

Then, loop through your array, concatenating all your strings into one string with newlines after each one, and overwrite it back to the text file. Pretty simple, really.

We have a howto on reading/writing a text file in C#. The theory is no different in VB, just the syntax. You can also google String.Split if you need more information on how to do that.
Aug 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dmiller23462 | last post by:
Hi again guys.... This is my first source of help, by far the best ASP intellectual collective....Thanks in advance for any assistance...Anyway.... The forms I'm still working on (finishing...
4
by: Tor Inge Rislaa | last post by:
How add a line of text to a textfile? I need a way to open an existing file of type .txt insert a line of tekst and then close the file again. TIRislaa
1
by: Derek Hart | last post by:
Using VB.NET, I wish to open a text file that will have paragraph marks throughout it. To replace them, I can probably use the Replace command and replace vbcrlf. But can somebody give me sample...
6
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
1
by: shapper | last post by:
Hello, I have a XLST file where I have the phrase "HERE". Something like: <xsl:text>HERE</xsl:text> I want to replace "HERE" by "UPDATED" and save the file.
3
by: kris06 | last post by:
Hi All, I have a textfile and i need to format it, so that i can import it to the mysql database. The textfile contains 9 colums of data separated by a space. I want to replace only the...
0
by: Snoze | last post by:
Hello, I'm trying to make an application with C# that writes and reads some lines of a text file, but I want to read one specific line and save the text in a string variable. And the inverse...
3
by: tshad | last post by:
I need to replace a value in a string that could be any case: CREATE Create create And I don't want to change the case of the whole string. Just find the work "Create" and change it to...
5
by: kashif73 | last post by:
i have a text file which has a fixed length lines. How can I update a particular line (record ) basing on a record ID provided? below is my code which retrieves a particular line from the textfile &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.