Adding literal single quotes to a string
Hi all,
I would like to add literal single quotes to a string, e.g. the end results of the string output should look like:
'this is the string'.
For example, in Octave you might do this like this with escape characters and double quotes:
my_var = "\'this is the string\'"
I don't believe this is supported directly in Compose, but is there any other way this possible to add literal single string characters in Compose?
Thanks!
Best - Andy
Best Answer
-
You're right! In previous versions you can use two single quotes, but I also get an error in 2022.3. Thanks for noticing that. My other way of doing it is:
[char(39), 'this is the string', char(39)]
1
Answers
-
Hey Andy,
Two single quotes should do the trick: ''this is the string'' will result in the string 'this is the string'.
Rafael
0 -
Hi Rafael, thanks but that gives me an error in Compose v2022.3, what version are you using?
Here's my result:
> myvar = ''this is the string''
Syntax error at line number 1 near character position 21
Maybe I'm missing something?
Also, I actually need to do some kind of string concatenation/manipulation because I need to add the single quotes to a variable,
my_var_name = 'name'
e.g.
my_new_var_with_single_literal_quotes = strcat('\'',my_var_name,'\'') (doesn't work but this is the idea)
What do you think?
Thanks,
Andy0 -
You're right! In previous versions you can use two single quotes, but I also get an error in 2022.3. Thanks for noticing that. My other way of doing it is:
[char(39), 'this is the string', char(39)]
1 -
Sweet, that works using char(39), thank you!
0 -
Andy, I was missing another single quote in the first method I gave you, that is why it was throwing an error. Typing '''this is the string''' should work as well.
1 -
Andy, I was missing another single quote in the first method I gave you, that is why it was throwing an error. Typing '''this is the string''' should work as well.
Thanks, the problem is that I need to add the single quotes to another variable by some method, I actually tried to use triple quotes but couldn't figure out how to get it to work, e.g. :
myvar = 'this is the string'
%myvar = this is the string
blah = [''',myvar,''']
%blah = ',myvar,' <--- this is the answer, but not what I wantIf you know another way to do it, please share for reference, but I was able to get along with the char(39) option, so I'm ok for now. It's just not very obvious to casual user.
Thanks,
Andy
0 -
What is going on is that the exterior single quotes delimit the string declaration and having two consecutive single quotes inside the declaration prints a single quote. Therefore, ['''', myvar, ''''] should work as you need.
0