0% found this document useful (0 votes)
7 views

Replacing Value in .CSV file-VBForums

Uploaded by

phuockcn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Replacing Value in .CSV file-VBForums

Uploaded by

phuockcn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

16:05 13/06/2024 Replacing value in .

csv file-VBForums

User Name Password Log in Help Register

Remember Me?

Forum What's New? Advertiser Disclosure

New Posts FAQ Calendar Forum Actions Quick Links Advanced Search

VBForums Visual Basic Visual Basic .NET Replacing value in .csv file

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start
viewing messages, select the forum that you want to visit from the selection below. Click
Here
Results 1 to 6 of 6 to
Expand
Thread: Replacing value in .csv file Forum
to Full
Width
Thread Tools Display

Jul 25th, 2012, 09:58 AM #1

Train_Wreck
Replacing value in .csv file
Thread Starter
Junior Member Hey, i have a question: Is it possible to replace a value in a .csv file using VB????

Join Date: Jun 2012 Here is an example of what im talking about:


Posts: 24
Column1, Column2, Column3, Column4, Column5
Value1, Value2, Value3, ., Vlaue5

i need to replace ,., with , Value4,

Reply With Quote

Jul 25th, 2012, 10:03 AM #2

kareninstructor
Re: Replacing value in .csv file
Karen Payne MVP
You would read the file in say with TextFieldParser class line by line, place the data into a string array then write the
data back to the file after finishing looping thru the file.

The replacement happens as you are reading a line that meets critiera to change text. Use IO.File.WriteAllLines to
write the array back to disk.
Join Date: Jun 2008
Location: Oregon
Posts: 6,688

EF Core 7: Json columns


EF Power Tools tutorial
Working with the using directive in C#
Twitter

2014-Current

Reply With Quote

Jul 25th, 2012, 10:04 AM #3

.paul.
Re: Replacing value in .csv file
eXtreme Programmer
vb Code:
1. dim lines() as string = io.file.readalllines("csv.txt")
2. for x as integer = 0 to lines.getupperbound(0)
3. if lines(x).contains(".,") then
4. lines(x) = lines(x).replace(".", "Value4")
Join Date: May 2007 5. end if
Location: Chelmsford UK 6. next
Posts: 25,539 7.
8. io.file.writealllines("csv.txt", lines)

Coding Examples:
My Web Site | CodeBank Submissions
Features:
Sorting Techniques | DataGridView Printing | Average Strategies - Plugin Version
Online Games:
hangMan | masterMind | sudoku | crack the lock | numbers game | Fit Words | Fit Numbers | Crossword Hangman
Compiled Games:
Puzzle Games

https://www.vbforums.com/showthread.php?685013-Replacing-value-in-csv-file 1/3
16:05 13/06/2024 Replacing value in .csv file-VBForums

Reply With Quote

Jul 25th, 2012, 10:08 AM #4

Train_Wreck
Re: Replacing value in .csv file
Thread Starter
Junior Member thanks for the reply i will check out the links and post something if/when i figure out how to write the code.

Join Date: Jun 2012


Posts: 24

Reply With Quote

Jul 25th, 2012, 10:10 AM #5

ThomasJohnsen
Re: Replacing value in .csv file
Fanatic Member

Originally Posted by .paul.

vb Code:
1. dim lines() as string = io.file.readalllines("csv.txt")
2. for x as integer = 0 to lines.getupperbound(0)
Join Date: Jul 2010
3. if lines(x).contains(".,") then
Location: Denmark 4. lines(x) = lines(x).replace(".", "Value4")
Posts: 528 5. end if
6. next
7.
8. io.file.writealllines("csv.txt", lines)

This bit of code may not work as intended. Assuming any of value1, value2, value3 or value5 contain a ".", you would
end up replacing more "."s than the one we're interested in replacing. Using indexof would most likely be a better
option, depending on the input data ofc.

#EDIT: The following should work regardless of contents of the other fields (at least on the small sample of data I
tried it on):
Code:
Dim lines() As String = IO.File.ReadAllLines("csv.txt")
Dim i As Integer

For x As Integer = 0 To lines.GetUpperBound(0)


i = lines(x).IndexOf(".,")
If i > 0 Then
lines(x) = lines(x).Substring(0, i) & "value4" & lines(x).Substring(i + 1, lines(x
End If
Next

IO.File.WriteAllLines("csv.txt", lines)

Last edited by ThomasJohnsen; Jul 25th, 2012 at 10:39 AM.

In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to
much in his totality. (Melville: Moby Dick)

Reply With Quote

Jul 25th, 2012, 10:53 AM #6

Train_Wreck
Re: Replacing value in .csv file
Thread Starter
Junior Member
Originally Posted by .paul.
Join Date: Jun 2012
vb Code:
Posts: 24
1. dim lines() as string = io.file.readalllines("csv.txt")
2. for x as integer = 0 to lines.getupperbound(0)
3. if lines(x).contains(".,") then
4. lines(x) = lines(x).replace(".", "Value4")
5. end if
6. next
7.
8. io.file.writealllines("csv.txt", lines)

hey this worked perfectly!!!!!!! Thankz

Reply With Quote

Quick Navigation Visual Basic NET Top


https://www.vbforums.com/showthread.php?685013-Replacing-value-in-csv-file 2/3
16:05 13/06/2024 Replacing value in .csv file-VBForums
Quick Navigation Visual Basic .NET Top

« Previous Thread | Next Thread »

VBForums Visual Basic Visual Basic .NET Replacing value in .csv file

Tags for this Thread

asp.net, vb.net
View Tag Cloud

Posting Permissions

You may not post new threads BB code is On


You may not post replies Smilies are On
You may not post attachments [IMG] code is On
You may not edit your posts [VIDEO] code is On
HTML code is Off
Forum Rules

Contact Us VB Forums Top

Terms of Service | About Us | Privacy Notice | Contact Us | Advertise | California - Do Not Sell My Info
Copyright 2024 TechnologyAdvice. All Rights Reserved.

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact
how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products
available in the marketplace.
All times are GMT -5. The time now is 03:52 AM.

https://www.vbforums.com/showthread.php?685013-Replacing-value-in-csv-file 3/3

You might also like