Let s = my string
Something like:
set s to quoted form of s
gives 'my string'
Which is only single quotes around the string and something like:
set s to ("\"" & s & "\"")
gives \"my string\"
Which adds the backslashes to the string for some reason.
If you display the string in your last example, you'll see that the script indeed has gotten double quotes around them.
The reason the double quotes are shown with backslashes internally, is that AppleScript needs to make a difference between the literal backslashes, is that you use double quotes, for say assigning a string to a variable. So usual double quotes has a distinct purpose in AppleScript, and it needs to tell a difference between those, and the literal double qoutes that you want to use in any variables. When your literal double quotes are put to display, then they are shown as usual double qoutes, also if they are written into a text file for instance, that you open with an editor, then you'll realize, that AppleScript perform escaping "behind the scenes", into an internal representation, that makes the data work with the conventions of AppleScript, so that any data doesn't break the script, when the script compiles first and foremost.
Try using the code below to display it.
tell application (path to frontmost application as text)
display alert s
end tell
Then you'll realize that you have gotten "normal" double qoutes in your string for display.
Related
i would like to create a string in the following exact format :
"\"\"\"\nThis is a beautiful world.\n"
But the code :
test ="\"\"\"\nthis is a beautiful world.\n"
test
gives the output :
'"""\nthis is a beautiful world.'
please help in getting an exact text.
My string test should look exactly like the string it has been initialized to. but after initialization, it actually gives the output as mentioned. i want to concatenate the test string to another string
the symbol "\" is called an escape character in most programming languages. this is used to add symbols to a string that might not be easy to add. eg - to add a double quote into a string, we add the \" to the string. eg -
a = "he said, \"hello\" to me"
this would give the output as -
he said "hello" to me
here, the "\" acts as a symbol for the code which allows it to recognize characters which might raise errors other-wise.
to include a backslash in your code, add an extra backslash to it. eg -
a = "\\"
here, the value of a is \.
if you still haven't been able to understand it, try - this tutorial
for your code,try this -
test = "\\\"\\\"\\\"\\nThis is a beautiful world.\\n"
and if you also want the double quotes at the ends,
test = "\"\\\"\\\"\\\"\\nThis is a beautiful world.\\n\""
The first thing to note is, that just typing in the variable name when running python interactively returns the canonical string representation of the object and not (necessarily) the plain value of the object.
For strings this means (among other things) that quotes are added around the output (in your example the outermost single quotes) and any newlines are replaced with "\n".
This means that, although your output does show "\n" the actual string contains a newline character in its place.
The check what a string looks like, you should use the print() function to, well, print it.
>>> test = "\"\"\"\nthis is a beautiful world."
>>> test
'"""\nthis is a beautiful world.'
>>> print(test)
"""
this is a beautiful world.
>>>
Also, when running the code from a file, lines just containing variable names will not result in any output.
To answer the question
There are a few ways to handle that.
Assuming that the desired output is
"""\nThis is a beautiful world.\n
i.e. the outermost double quotes are not supposed to be part of the string, that is
While using double quotes ("…") to denote strings: escape any \ or " by prepending it with \:
>>> test ="\\\"\\\"\\\"\\nthis is a beautiful world.\\n"
>>> print(test)
\"\"\"\nthis is a beautiful world.\n
Within regular strings \ is used to designate control character. For example: \n is interpreted as newline, \b would be a backspace. If you need to have a \ in a string, you need actually write two \\.
If you are usually using "…" for string notation, this allows for a more consistent coding style but it is (especially in this case) quite ugly and might be hard to understand at a glance.
As your string contains a lot of " characters, just use single quotes ('…') to designate the string. This removes the need to escape ":
>>> test = '\\"\\"\\"\\nthis is a beautiful world.\\n'
>>> print(test)
\"\"\"\nthis is a beautiful world.\n
This is less consistent (if "…" is usually used for strings, but allows the code to be quite a bit closer to the desired output.
Use raw strings (r'…' or r"…") to disable the interpretation of control characters and allow the use of " within the string:
>>> test = r'\"\"\"\nthis is a beautiful world.\n'
>>> print(test)
\"\"\"\nthis is a beautiful world.\n"
or even
>>> test = r"\"\"\"\nthis is a beautiful world.\n"
>>> print(test)
\"\"\"\nthis is a beautiful world.\n"
This allows the code to be identical to the desired output, but it has some limitations when it comes to freely mixing " and ' within a single string as it is not possible to escape the quotation marks within the string without also adding \ to the string output. This can be seen in the second example, where we use \" to escape the double quote within r"…" in the code but where the \s are still present in the output. While this works well in this specific case, I would recommend against using \' within r'…' or \" within r"…" to avoid confusion.
I want to modify a string with replace in Smarty.
{$var|replace:'aaa':'bbb'}
Simple. But I need to replace this:
{$var|replace:'value="AA"':'value="$string_b"'}
I've also tried with the following syntax with no success:
{$var|replace:'value="AA"':'value="'$string_b'"'}
{$var|replace:'value="AA"':'value="`$string_b`"'}
{$var|replace:'value="AA"':'value=""$string_b""'}
you have to enclose the string in double quotes if you want smarty to recognize variables inside, so in your case you also have to escape the double quotes inside the string:
{$var|replace:'value="AA"':"value=\"{$string_b}\""}
Using brackets around the variable is not necessary but recommended
In the end I managed to find the right syntax to use.
{$var|replace:"value=\"AA\"":"value=\"$string\"}
I have a CouchDB database that contains some daily updated status information. I can use curl to query the database from my bash prompt:
curl 'http://localhost:5984/db/_design/state/_view/stateinfo?group=true&startkey=\["2016-02-10",0\]&endkey=\["2016-02-10\u9999",\{\}\]'
Now I want to write a little function dbq in my .bash_aliases that can be used like so: dbq mm-dd where mm-dd is a date such as 02-10. I tried (among other variations):
dlq() { curl \'http://localhost:5984/db/_design/state/_view/stateinfo?group=true\&startkey=\[2016-{$1},0\]\&endkey=\[2016-{$1}\u9999,\{\}\] \' ;}
I just can't get the argument interpolation (of $1) and the escaping of brackets, quotes, slashes and ampersands in the bash function right. Any ideas on how to do this properly? (BTW, this escaping business when transitioning from a working bash prompt solution towards a reusable bash function with arguments has slowed me down several times. Any hints and pointers to some helpful resources are much appreciated.)
I find the fact that you need to pass backslashes in your original command very odd, and worry a bit that it may indicate that your backend is vulnerable to some sort of remote-code-execution attack. But putting that aside:
The thing to remember about bash quoting is that the rules around ' are very short and simple, but are so short and simple that they often surprise people. Specifically, the rule is this: absolutely nothing is special until the next ' mark. Nothing. Not backslashes, not newlines, nothing. Everything gets passed through.
This is different from most programming languages, which trips people up.
In your original command line, when you pass this:
curl 'http://localhost:5984/db/_design/state/_view/stateinfo?group=true&startkey=\["2016-02-10",0\]&endkey=\["2016-02-10\u9999",\{\}\]'
That means that the argument to curl is literally
http://localhost:5984/db/_design/state/_view/stateinfo?group=true&startkey=\["2016-02-10",0\]&endkey=\["2016-02-10\u9999",\{\}\]
with all the backslashes intact.
Now, in your function the easiest way to pass what you want is to use the fact that two adjacent strings in bash get passed together as a single argument, and do:
dlq() {
curl 'http://localhost:5984/db/_design/state/_view/stateinfo?group=true&startkey=\["2016-'"$1"'",0\]&endkey=\["2016-'"$2"'\u9999",\{\}\]'
}
Note that what I have there is a string in single quotes that ends with "2016-, then a string in double quotes that is "$1", then a string in single quotes that begins with a double quote character and continues until it also ends in "2016- and then a double quoted "$2", and then a single quoted string until the end of the line.
The body of a function is effectively quoted (one reason they are superior to aliases), so you don't need to escape the single quote or anything inside them. You do, however, need to use double quotes so that $1 is expanded.
Something like...
dlq() {
baseurl="http://localhost:5984/db/_design/state/_view/stateinfo"
curl "$baseurl?group=true&startkey=[2016-{$1},0\]&endkey=[2016-{$1}\u9999,{}]"
}
(Please double-check the parameters; I'm not sure what was escaped erroneously and what might need escaping for the API.)
What’s the difference between single (') and double (") quotes in Vim? Does it make speed differences? Is it better to use one or another when running functions inside it? Does it matter at all?
I’m interested specifically in their use in the .vimrc file.
I’m asking because I find people use both in the same thing, and I’m wondering what are the differences. I tried to Google this, but wasn’t able to find anything.
Double quotes allow for interpolation whereas single quotes do not.
For example, using double quotes :echo "foo\nbar" will output foo and bar on separate lines whereas :echo 'foo\nbar' will not interpret \n as a line break and will output foo\nbar literally.
For more info on different types of quotes type :h 41.2 for the help file and read the part near the end of the section with the heading STRING VARIABLES AND CONSTANTS.
This said, don't confuse quotes for strings with the double quote at the beginning of a line comment. Single quotes never start line comments, only double quotes do.
While running an R-plugin in SPSS, I receive a Windows path string as input e.g.
'C:\Users\mhermans\somefile.csv'
I would like to use that path in subsequent R code, but then the slashes need to be replaced with forward slashes, otherwise R interprets it as escapes (eg. "\U used without hex digits" errors).
I have however not been able to find a function that can replace the backslashes with foward slashes or double escape them. All those functions assume those characters are escaped.
So, is there something along the lines of:
>gsub('\\', '/', 'C:\Users\mhermans')
C:/Users/mhermans
You can try to use the 'allowEscapes' argument in scan()
X=scan(what="character",allowEscapes=F)
C:\Users\mhermans\somefile.csv
print(X)
[1] "C:\\Users\\mhermans\\somefile.csv"
As of version 4.0, introduced in April 2020, R provides a syntax for specifying raw strings. The string in the example can be written as:
path <- r"(C:\Users\mhermans\somefile.csv)"
From ?Quotes:
Raw character constants are also available using a syntax similar to the one used in C++: r"(...)" with ... any character sequence, except that it must not contain the closing sequence )". The delimiter pairs [] and {} can also be used, and R can be used in place of r. For additional flexibility, a number of dashes can be placed between the opening quote and the opening delimiter, as long as the same number of dashes appear between the closing delimiter and the closing quote.
First you need to get it assigned to a name:
pathname <- 'C:\\Users\\mhermans\\somefile.csv'
Notice that in order to get it into a name vector you needed to double them all, which gives a hint about how you could use regex. Actually, if you read it in from a text file, then R will do all the doubling for you. Mind you it not really doubling the backslashes. It is being stored as a single backslash, but it's being displayed like that and needs to be input like that from the console. Otherwise the R interpreter tries (and often fails) to turn it into a special character. And to compound the problem, regex uses the backslash as an escape as well. So to detect an escape with grep or sub or gsub you need to quadruple the backslashes
gsub("\\\\", "/", pathname)
# [1] "C:/Users/mhermans/somefile.csv"
You needed to doubly "double" the backslashes. The first of each couple of \'s is to signal to the grep machine that what next comes is a literal.
Consider:
nchar("\\A")
# returns `[1] 2`
If file E:\Data\junk.txt contains the following text (without quotes): C:\Users\mhermans\somefile.csv
You may get a warning with the following statement, but it will work:
texinp <- readLines("E:\\Data\\junk.txt")
If file E:\Data\junk.txt contains the following text (with quotes): "C:\Users\mhermans\somefile.csv"
The above readlines statement might also give you a warning, but will now contain:
"\"C:\Users\mhermans\somefile.csv\""
So, to get what you want, make sure there aren't quotes in the incoming file, and use:
texinp <- suppressWarnings(readLines("E:\\Data\\junk.txt"))