This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace " in vb.net
How to put data containing double-quotes in string variable?
Aug 01, 2003 02:30 AM | LINK
I need to store a string that contains words in double quotes. The complete string data is presented to the variable in double quotes, but the next double quotes within the data ends the string and the remaining text gives a syntax error. How can I place literal double-quotes in a string variable? For example:
Dim MyVar as string = "stm_bm(["menu53d0",400,"","blank2.gif",0,"","",0,0,250,0,1000,1,0,0,""],this);"
You can escape (this is how this principle is called) the double quotes by prefixing them with another double quote. You can put them in a string as follows:
Dim MyVar as string = "some text ""hello"" hello"
This will give the MyVar variable a value of some text "hello" hello.
Related
I have a multi-column combobox on an Access form (Access 2019 x64, if that matters) and would like to use its AddItem() method to populate it as described in this documentation. Citing from it:
For multiple-column lists, use semicolons to delimit the strings for
each column (for example, "1010;red;large" for a three-column list).
If the Item argument contains fewer strings than columns in the
control, items will be added starting with the left-most column. If
the Item argument contains more strings than columns in the control,
the extra strings are ignored.
So it is impossible to add a string for a certain column when the string itself already contains a semicolon?
Surround the column value which contains the semi-colon with double-quotes, e.g.:
AddItem """Column1;A"";Column2;Column3"
Will yield:
+-----------+---------+---------+
| Column1;A | Column2 | Column3 |
+-----------+---------+---------+
All credits go to #Lee Mac who answered my original question completely. However, the answer immediately raised the question how we should treat strings which already have quotes in them.
Therefore, I have written the following function in VBA which seems to solve the problem completely. The comments should be self-explaining. Please comment if you find an error.
I have posted this as separate answer because it's way too long for a comment, and because it seems that the community does not prefer such answers edited into the original question.
'Notes:
'
'The following function quotes an arbitrary string so that it can be
'used as part of the parameter to the ComboBox.AddItem() method.
'Writing that function was necessary because there is no easy way to
'use strings which already contain the ; character as part of that
'parameter, or which even contain the ; character as well as double
'quotes; see also Microsoft's documentation.
'
'To construct the parameter for the AddItem() method, the following
'must be done:
'
'For EACH column, the source string must be quoted using the following
'function. Then, all QUOTED strings must be concatenated, separating
'them only by one ; character.
'
'Successfully tested 2019-10-03 in Access 2019 / 64-bit / VBA with a
'standard combobox on a form.
Public Function QuoteStringForValueList(sString As String) As String
Dim sTemp As String, sDDQ As String
'String containing two double quotes to make reading easier
sDDQ = Chr(34) & Chr(34)
'Replace each double quote by four double quotes
sTemp = Replace(sString, Chr(34), sDDQ & sDDQ)
'Add two leading and two trailing double quotes
QuoteStringForValueList = sDDQ & sTemp & sDDQ
End Function
I'm trying to make a string that only contains 1 double quotation mark. Every time I try to type insert only 1 quotation mark it adds another as a pair. Here's an example of what I want and what I get instead:
This is what I want:
textbox.Text = """ 'I only want 1 double quotation mark like so
This is what I get instead:
textbox.Text = """" 'Visual studio adds in another quote, making it 2 instead of 1
How would I get visual studio to only insert 1 double quote rather than 2?
im not a .net developer but i believe every programming language has this what we called scape sequence. in java we can do it like this
String str = "\"dude google first\".";
The double quotation is just escape that is used within a string to indicate a single quotation
This question already has answers here:
Escaping quotes in a string in VB6
(7 answers)
Closed 8 years ago.
I am trying to make one of my variables equal to the following:
Dim Quote as string
Quote = "Letter Size 11 x 17""
I need the single quote/inch symbol in the text but i do not know how to make VBA recognize it without giving me and error saying i have to end quote it.
Try one of these. It seems to do what you want (depending on where you want the ":
Sub test()
Dim Quote As String
Quote = "Letter Size 11 x 17"""
Debug.Print Quote
Quote = "Letter Size 11"" x 17"""
Debug.Print Quote
End Sub
To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))
In following code, the usage of the string "“" (i.e. a left double quotation mark inside a string) results in a compile error in VB.NET:
StringVar = Replace(StringVar, "“", "“")
What’s going on here?
It seems as if you want to replace curly quotes with their HTML code equivalent.
On the first glance, your code is absolutely correct. The problem is that VB allows curly quotes in place of regular quotes in code (because Unicode is great, right?). That is, the following codes are all equivalent:
Dim str = "hello"
Dim str = “hello”
Dim str = "hello“
Now, if you want to use a quotation mark inside a string, VB doesn’t know whether the quotation mark is supposed to end the string or not. In C#, this would be fixed by escaping the quotation mark, i.e. in place of """ you’d write "\"". In VB, the same is done by doubling the quotation mark, i.e. """".
Back to your curly quote. The same as for straight quotes applies according to the VB language specification (¶1.6.4). So to write a curly quote in code, try the following:
StringVar = Replace(StringVar, "““", "“")
Unfortunately, I cannot try this code now and it’s altogether possible that the IDE simply replaces this by straight quotes. If that’s the case, an alternative is to use Chr or ChrW with the character code of the “left double quotation mark”:
StringVar = Replace(StringVar, ChrW(&H201C), "“")
Or, for symmetry, written in decimal (but I prefer hexadecimal for character codes):
StringVar = Replace(StringVar, ChrW(8220), "“")
Something else: the Replace function will probably soon be deprecated and doesn’t work everywhere (e.g. Windows Phone 7). Instead, use the Replace method of the String class:
StringVar = StringVar.Replace(, ChrW(8220), "“")
See http://msdn.microsoft.com/en-us/library/613dxh46%28v=vs.71%29.aspx
Try this:
StringVar = Replace(StringVar, "“", ChrW(&H8220))
It looks like you're searching for the ChrW function in the Microsoft.VisualBasic namespace, which is used to convert a Unicode character code into the actual character.
If you're trying to replace straight quotes in a string with curly quotes, try the following code:
'Declare a string that uses straight quotes
Dim origString As String = "This string uses ""quotes"" around a word."
'Create a new string by replacing the straight quotes from the original string
'with left-facing curly quotes
Dim newString As String = origString.Replace("""", ChrW(8220))
'Display the result
MessageBox.Show(newString)
Or, if you're trying to encode the left-facing curly quotes in a string by replacing them with an alternate notation (assuming the one you used in the question is correct), try the following code:
'Declare a string that uses left-facing curly quotes
Dim origString As String = "This string uses fancy " & ChrW(8220) & _
"quotes" & ChrW(8220) & " around a word."
'Create a new string by replacing the curly quotes with an arbitrary string
Dim newString As String = origString.Replace(ChrW(8220), "“")
'Display the result
MessageBox.Show(newString)