0

I'm trying to add quotations to a piece of string that is broken in 2 pieces with a variable in the middle.

I have tried many, many ways...many ways and failed.

Example:

Dim path as String = "C:\Users\" & CurrentUser & "\folder\path to something\"

I need the whole result to be in quotations to pass it to a command that requires the path with spaces in quotations.

"C:\Users\Nemo\Folder\Path to something\"

Any help is appreciated.

  • 4
    To combine parts of a path, it is usually best to use the [Path.Combine](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8) method. To add a double-quote, use `""""`. So, `Dim path as String = """" & Path.Combine("C:\Users\", CurrentUser, "\folder\path to something\") & """"`. – Andrew Morton Jul 24 '19 at 18:35
  • 1
    Possible duplicate of [How to add double quotes in a string literal](https://stackoverflow.com/questions/8574682/how-to-add-double-quotes-in-a-string-literal) – Andrew Morton Jul 24 '19 at 18:36
  • 1
    `ChrW(34) & TheString & ChrW(34)` – Jimi Jul 24 '19 at 18:37

2 Answers2

0

You should just be able to use "" inside the string assignment that you have. So with your example it would look like this:

Dim path As String = """C:\Users\" & CurrentUser & "\folder\path to something\"""
madreflection
  • 4,744
  • 3
  • 19
  • 29
  • Never mind. Now I see how the editing works. I had no idea I was editing someone else's answer. I thought my answer was changed somehow. Sorry. – Jamie Jul 26 '19 at 10:26
0

IMHO it is a good idea to use CHR(34) instead of actual quotes when concatenating strings. Your code should look like this:

Dim current user as string="Nemo"
Dim path as String = CHR(34) & "C:\Users\" & CurrentUser & "\folder\path to something\" & CHR(34)

The result will be: "C:\Users\Nemo\folder\path to something\"

Jamie
  • 555
  • 3
  • 14