Click here to Skip to main content
15,749,281 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have file in this format:

VB
No,Name,Sal,Location,ID
1,George,"£7,634",GBR,121
2,Mike,"£7,435",RUS,122
3,Bell,£835,ARG,123


My output file should be like this:

VB
Name,Sal,ID
George,"7,634",121
Mike,"7,435",122
Bell,835,123


I want to take off the £ sign from sal column values. I coded like this:

VB
Dim ioReader As New System.IO.Streamreader("C:\old.csv")
    ioLines="Name,Sal,ID"
    Dim ioWriter As New System.IO.StreamWriter("C:\new.csv")
    While Not ioLine = ""
        ioLine = ioFile.ReadLine
        Dim values As String()
        If ioLine <> "" Then
            values = ioLine.Split(",")
            Dim outPut as string=values(1) & values(2).substring(2,length(values(2)-1) & values(4)
            ioLines += System.Environment.NewLine & outPut
        EndIf
    EndWhile
    ioWriter.WriteLine(ioLines)

But I am not getting the right answer. I am getting values as 

    George,7,634   --- Wrong
    Mike,7,435     --- Wrong
    Bell,835,123   --- Correct

Any Suggestions please?
Posted
Updated 21-Jun-11 2:30am
v2

I have a solution for you!

It tool me considerable amount of time to solve the similar problem, only for splitting by blank space, but you can use my solution for your case.

I created this code to .NET simulate command pre-parsing into array of strings by blank space. It also takes into account quotation marks, to make it possible to avoid splitting inside quotation. I needed it to accelerate testing of my command line utility without using command line itself (and it helped me to fix number of bugs really fast).

Now, let me help you to locate my source code. Load my article: Enumeration-based Command Line Utility[^], see the section "6. CommandLine Testing". It explains SA.Universal.Utilities.CommandLineSimulationUtility.

Load the source code which comes to article and locate this utility in the demo/test application. This is the code you need.

Please report back to me if there is any problem with this code.

—SA
 
Share this answer
 
Comments
Abhinav S 22-Jun-11 2:15am    
Good answer. My 5.
Sergey Alexandrovich Kryukov 22-Jun-11 2:24am    
Thank you, Abhinav; I tried hard. :-)
--SA
Instead of using split I would simply use replace.
values = ioLine.Replace(@"£", string.empty);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jun-11 22:05pm    
Not as easy; the problem is pretty difficult: correct splitting taking into account quotation marks.

I happened to have the code which comes with my article, please see my solution.
--SA
Abhinav S 22-Jun-11 2:15am    
Ok will do.
I think this Link[^] will help you.
 
Share this answer
 
I would suggest to go with
VB
If ioLine <> "" Then
            ioLines &= System.Environment.NewLine & ioLine.Replace("£", "").Substring(ioLine.IndexOf(",") + 1)
EndIf
 
Share this answer
 
v3
Comments
Member 8001800 22-Jun-11 5:25am    
It says FirstIndexOf is not a member of string
Prerak Patel 22-Jun-11 5:37am    
Sorry mate, it is IndexOf.
Prerak Patel 22-Jun-11 6:21am    
Did this help now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900