Stripping Quotes from a String

tpra21

Active member
Joined
Oct 21, 2006
Messages
26
Programming Experience
1-3
I apologize if this answer is elseware, but I have looked for hours now with no luck. I am reading a comma-delimited text file and parsing the record into fields using Split().

I have a customer name fields that contains the following:

custName = "Main Street Produce" (including the quotes)

How do I get the quotes out of the string so that I just have the following:

custName = Main Street Produce

The quotes look nasty when I display the string field in the listbox, that is why I need them removed. I tried the following:

custName.Replace(""", "") but VB.Net doesn't like the three consecutive quotes.

Thanks, Adam
 
..or if you want to preserve any other double quote characters within the string use String.Trim method (or even String.Substring method).
 
I apologize if this answer is elseware, but I have looked for hours now with no luck. I am reading a comma-delimited text file and parsing the record into fields using Split().

I recommend that you re-evaluate what is actually the problem.. in this case youre trying to write your own CSV parser, and its a job that has been done before by microsoft and others..

Microsoft's way is to use the Jet.OLEDB database driver, conenct it to the directory the csv file is in, and then say "SELECT * FROM filename#csv"

Additionally you can include a schema.ini file to describe the file format because the driver will attempt to interpret the data as numbers etc for you.. hecne a column of 001, 002, 003 becomes numbers 1,2,3 even if enclosed in quotes


-

For not databased (and non interpreting) ways to read a text file, check out a free utility library called JHLTools.dll - it comes with a csv parser that works like a file reader - you just establish a stream to the file and then read the file line at a time from the parser. It returns you an array of all the string elements on that line.

the dll is implemented in C# but this doesnt stop you using it in vb - just add a reference to it and away you go
 
I tried the following with no luck:

Try custName.Replace("""", "")

I don't get a syntax error, but it is if the command isn't recognizing the quotes as the character needing to be stripped. Is there any character that I must place before a quote to let vb know that that is what I want removed, like PHP uses the '\' character?

I'll keep giving it a try. And CJard, thanks for the advice, I'll check out that library. Do you know where I is located?

Thanks everyone, Adam
 
I found this code compliments of BLUMonde (Slightly changed), and it works for trimming the leading quotes, but I can't get the right trim to work. ANy ideas?


VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Microsoft.VisualBasic.Right(glCustName.Trim, 1) = """" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Strip out quotes" Then[/COLOR][/SIZE]
[SIZE=2]glCustName = glCustName.Trim.Substring(0, glCustName.Length)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Microsoft.VisualBasic.Left(glCustName.Trim, 1) = """" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Strip out quotes" Then[/COLOR][/SIZE]
[SIZE=2]glCustName = glCustName.Trim.Substring(1)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]


Thanks, Adam
 
I tried the following with no luck:

Try custName.Replace("""", "")

You are remembering to save the result, arent you?

result = original.Replace(whatever..)


-


Do you know where I is located?

Thanks everyone, Adam

Sorry, i made it a bit hard to find!
http://www.heikniemi.net/jhlib/



Using it is a doddle:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] csvDecoder [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] JouniHeikniemi.Tools.Strings.CsvReader =[/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] JouniHeikniemi.Tools.Strings.CsvReader[/SIZE]
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] csvElements [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]() = csvDecoder.GetCsvLine()
[/SIZE]

csvElements is a string array containing all the elements of the csv live. Every call to GetCsvLine() advances the reader, so call it 100 times for a 100 line file
 
I must say, this is embarassing :eek: . I was not saving the result to the replace function, and now that I am, it works great! Thanks to everyone!! Sometimes the simple issues seem to be the toughest for me.

CJard, thanks for that library, I will definitely throw that in the collection for some good use.

Thanks, Adam
 
You should absolutely not use the Replace function to remove starting and ending double-quote characters because that will also remove double-quote characters within the string. Use the string.Trim function method. You can specify which character(s) you want to trim off from beginning and end of string with the second overload of that method which takes a paramarray of chars as parameter. Example:
VB.NET:
'plain string: "She said "hi", then left."
'
Dim s As String = """She said ""hi"", then left."""
s = s.Trim("""")
'
'result plain: She said "hi", then left.
 
Back
Top