🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

Adding literal single quotes to a string

User: "andyer"
Altair Employee
Updated by andyer

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

Find more posts tagged with

Sort by:
1 - 7 of 71
    User: "RSGarciarivas"
    Altair Employee
    Updated by RSGarciarivas

    Hey Andy,

    Two single quotes should do the trick: ''this is the string'' will result in the string 'this is the string'.

     

    Rafael

    User: "andyer"
    Altair Employee
    OP
    Updated by andyer

    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,


    Andy

    User: "RSGarciarivas"
    Altair Employee
    Accepted Answer
    Updated by RSGarciarivas

    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)]

    User: "andyer"
    Altair Employee
    OP
    Updated by andyer

    Sweet, that works using char(39), thank you! :)

    User: "RSGarciarivas"
    Altair Employee
    Updated by RSGarciarivas

    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.

    User: "andyer"
    Altair Employee
    OP
    Updated by andyer

    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 want

     

    If 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

    User: "RSGarciarivas"
    Altair Employee
    Updated by RSGarciarivas

    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.