How to escape slash (/) in VBScript? - vbscript

How do you escape the forward slash character (/) in VBScript? For example, in the following string:
bob = "VU administration/front desk"

You don't escape it: it doesn't mean anything special in php or vbscript, and therefore doesn't need to be escaped. The only character you need to escape in vbscript is the double quote, which escapes itself:
MyString = "He said, ""Here's how you escape a double quote in vbscript. Slash characters -- both forward (/) and back (\) -- don't mean anything, even when used with common control characters like \n or \t."""
Similarly, in php a backslash escapes itself, but a forward slash doesn't need any special handling.

This is so wrong I have to comment on it. The question is about VBScript, not PHP, so who cares what PHP does with a slash? In VB, the forward slash / does have special meaning. The character / represents DOUBLE. If your code editor displays this character as DOUBLE or you get an error about converting to DOUBLE when you don't mean to convert to DOUBLE then you probably need to fix something in your string.
For instance, VB treats the forward slash / in this query string as DOUBLE and the program would fail here because you cant convert select replace(convert(char(10),pih.updateon,111), from text to double.
Dim sQuery As String = "select replace(convert(char(10),pih.updateon,111),"/","-") as stopdate from myTable"
The problem however will not likely be an issue with escaping a slash. Chances are you goofed somewhere else. In this case, if you typed the string correctly, it would look like this:
Dim sQuery As String = "select replace(convert(char(10),pih.updateon,111),'/','-') as stopdate from myTable"
Hope that example helps understand where you might be going wrong if you are having issues with slashes.
Addendum: In VBScript and VB, the chief difference being VB is compiled and VBScript is interpreted, the syntax in this case is handled the same way.
d-bo

Related

Single backslash for Ruby gsub replacement value?

Does anyone know how to provide a single backslash as the replacement value in Ruby's gsub method? I thought using double backslashes for the replacement value would result in a single backslash but it results in two backslashes.
Example: "a\b".gsub("\", "\\")
Result: a\\b
I also get the same result using a block:
Example: "a\b".gsub("\"){"\\"}
Result: a\\b
Obviously I can't use a single backslash for the replacement value since that would just serve to escape the quote that follows it. I've also tried using single (as opposed to double) quotes around the replacement value but still get two backslashes in the result.
EDIT: Thanks to the commenters I now realize my confusion was with how the Rails console reports the result of the operation (i.e. a\\b). Although the strings 'a\b' and 'a\\b' appear to be different, they both have the same length:
'a\b'.length (3)
'a\\b'.length (3)
You can represent a single backslash by either "\\" or '\\'. Try this in irb, where
"\\".size
correctly outputs 1, showing that you indeed have only one character in this string, not 2 as you think. You can also do a
puts "\\"
Similarily, your example
puts("a\b".gsub("\", "\\"))
correctly prints
a\b

What's the point of escaping a single backslash in single quotes in Ruby?

I don't quite understand what's the point of escaping a single backslash when you have a string in single quotes in Ruby. Why does Ruby treats backslashes 'differently'?
backslashes are an escape character so if you were to write '\' would think you were trying to escape the '.
Otherwise if it treated single character strings differentls you wanted to write ' you would have to use double quotes, which will quickly get harder to maintain when you need to remember which quotes to use when.
If your question is actually "What is the point of the language design requiring us to escape a backslash in single (as opposed to double) quotes", then that is to allow single quotes to appear within a string literal written with single quotes. In order to do that, there must be an escape character for single quotes, which is the backslash, and then, the escape character itself needs to be escaped.

What is the difference between \ and \\ in file path

What is the difference between single slash and double slash in file path for Windows operating system such as
c:\\Personal\MyFolder\\MyFile.jpg
and
c:\Personal\MyFolder\MyFile.jpg
What if I use the single or double slash because I have tried both for storing images in my code (in webconfig file) and both of them work fine.
Is there any difference??
Windows ignores double backslashes. So while the second syntax with \ is correct and you should use that one, the first with \\ works too.
The only exception is double-backslash at the very beginning of a path that indicates a UNC path.
See Universal Naming Convention.
Though note that in many programming languages like C, C++, Java, C#, Python, PHP, Perl, a backslash works as an escape character in string literals. As such, it needs to be escaped itself (usually with another backslash). So in these languages, you usually need to use a double backslash in the string literal to actually get a single backslash for a path. So for example in C#, the following string literal is actually interpreted as C:\Personal\MyFolder\MyFile.jpg:
var path = "C:\\Personal\\MyFolder\\MyFile.jpg";
Though there are alternative syntaxes. For example in C#, you can use the following syntax with the same result:
var path = #"C:\Personal\MyFolder\MyFile.jpg";

Custom code highlight Notepad++

I am creating a custom code highlight for notepad++. What I want to do is the following:
some fieldnames are writen in the code with a ' in front of their name, for exampe
if 'variable = "test" then ...
I would like to highlight these words, but notepad++ does not seem to allow a delimiter starting with ' and ending with a space, not does it allow space as an escape character. Also, using ' as a keyword and enabling prefix mode has no effect. Anyone has a suggestion? Should I use another expression to let notepad recognise the space/' ?
Thanks in advance!
If you only need to highlight a single word, you can use a keyword in prefix mode. However when using single or double quotes in a keyword, they need to be escaped with a backslash. So your keyword would be:
\'
This may not be possible in notepad++. I can get the behavior you want using a character other than a single quote, like a back-tic, but it doesn't seem to work with single or double quotes. I suspect those characters are treated special within the syntax highlighter.

How to replace single quotes with escaped single quotes in ruby

I'm trying to replace single quotes (') with escaped single quotes (\') in a string in ruby 1.9.3 and 1.8.7.
The exact problem string is "Are you sure you want to delete '%#'". This string should become "Are you sure you want to delete \'%#\'"
Using .gsub!(/\'/,"\'") leads to the following string "Are you sure you want to %#'%#".
Any ideas on what's going on?
String#gsub in the form gsub(exp,replacement) has odd quirks affecting the replacement string which sometimes require lots of escaping slashes. Ruby users are frequently directed to use the block form instead:
str.gsub(/'/){ "\\'" }
If you want to do away with escaping altogether, consider using an alternate string literal form:
str.gsub(/'/){ %q(\') }
Once you get used to seeing these types of literals, using them to avoid escape sequences can make your code much more readable.
\' in a substitution replacement string means "The portion of the original string after the match". So str.gsub!(/\'/,"\\'") replaces the ' character with everything after it - which is what you've noticed.
You need to further escape the backslash in the replacement. .gsub(/'/,"\\\\'") works in my irb console:
irb(main):059:0> puts a.gsub(/'/,"\\\\'")
Are you sure you want to delete \'%#\'
You need to escape the backslash. What about this?
"Are you sure you want to delete '%#'".gsub(/(?=')/, "\\")
# => "Are you sure you want to delete \\'%#\\'"
The above should be what you want. Your expected result is wrong. There is no way to literally see a single backslash when it means literally a backslash.

Resources