How to remove Quote from a string in VB.NET

vineet

New member
Joined
Sep 2, 2006
Messages
4
Programming Experience
Beginner
Hi
I have a string...
lsstr="NR','CL','AS','CS"
I want to remove the single quotes from the starting and end of the string
So that it becomes...
lsstr='NR','CL','AS','CS'

Pls help me...
 
Does the string start and end with quotations as you have indicated here, while having apostrophes in the middle?

From the example your string starts with NR', and ends with 'CS

If thats the case and your looking to change those quotations to apostrophes its a simple:

myStr = lsstr.replace(chr(34),"'")

To replace the quotations (chr(34)) with an apostrophe. I might be misunderstanding your question though..
 
if your string is :: single quote single quote NR','CL','AS','CS single quote single quote, and you want to take out the single quote from the start and the end, then this shoul help you,

str=mid(lsstr,2,lsstr.length-2)

is this what you mean?
 
Nooooooo.. :/ We dont use Mid any more - thats for people who program in VB6 and havent gotten around to (or stubbornly refuse to) learning about the wonderful new world of .NET object orientation


Instead of Mid now, we'd use substring:

ResultString = OriginalString.Substring(1,OriginalString,Length - 2)



Please update your bookmarks/synapses accordingly :)



-
To answer the OP's question, how do we remove single quotes from each end of the string? First, some advice:

VB.NET:
[FONT=Courier New]A single quote is this:     [SIZE=7]'[/SIZE][/FONT]
[FONT=Courier New]A double quote is this:     [SIZE=7]"[/SIZE][/FONT]
[FONT=Courier New]Two single quotes in a row: [SIZE=7]''[/SIZE][/FONT]
[FONT=Courier New][SIZE=7][/SIZE][/FONT] 
[FONT=Courier New][SIZE=7][/SIZE][/FONT] 
[FONT=Courier New][SIZE=7][/SIZE][/FONT] 
[FONT=Courier New][SIZE=7](end of code)[/SIZE][/FONT]

Be sure you are asking for the right thing.. And when you post example strings, either post them in courier font, or use a code box because with courier as you can see above it is much easier to tell whether it is a single quote, double quote or two single quotes etc.

Of the two solutions offered, Hadina's concept is closer (even if the method is legacy we dont use any more), because raven's code will remove all the signle quotes from the string. There are other ways round this problem, it depends on the complxity you wish to put into solving it, and the data complexity you will encounter. With a regular expression doing the replacement you could ensure that the string did start and end with single quotes. The Substring method would not ensure this..
 
About removing quotes from a string

Does the string start and end with quotations as you have indicated here, while having apostrophes in the middle?

From the example your string starts with NR', and ends with 'CS

If thats the case and your looking to change those quotations to apostrophes its a simple:

myStr = lsstr.replace(chr(34),"'")

To replace the quotations (chr(34)) with an apostrophe. I might be misunderstanding your question though..

Hi U have understood it correctly.....
But the solution providied by u is not working.
With this statement my string is not changing at all..
 
Strings dont change once you have made them. All the string operations that (look like they) affect the content of the string, actually return a new String when they are done. If you want to keep the new String, then you have to keep the new String.

You also seem to not know the difference between single quotes and double quotes. Please be sure of the difference and be clear when asking your questions! :)
 
Hi U have understood it correctly.....
But the solution providied by u is not working.
With this statement my string is not changing at all..

Yea, your string wont change, as cj said. What will happen is that the variable myStr will be assigned a value of your string, modified as stated.

You could alternativly set:

lsstr = lsstr.replace(chr(34),"'")

to achieve what you are after, though again just note the fact that this is
a new string being assigned over the old.
 
Back
Top