Latest update on October 19, 2020 at 04:00 AM by Irene Burn .

How to use single and double quotation marks in strings? In all programming languages, you must open and close your string with quotation marks, but you don't have to, if your interpreter doesn't consider the quotes (in the case of a string that does not contain spaces). Why the empty spaces? Because very often it means we are moving to another function.

How to add a quotation mark without truncating the string?

If your message is truncated or you receive an error message after adding quotes/apostrophe to a string, continue reading.

In PHP/HTML

In PHP you can define your string with quotes and apostrophes.

A simple technique to avoid errors is to count the number of quotation marks or apostrophes, If it is odd, then it will certainly generate an error.

For example:

$mystring = "hello,";

In the case you need to add quotes:

$mystring = 'hello "John",';

(Change the character that delimits your string)

Or:

$mystring = "hello "John",";

(Use the backslash -> Escape character)

This should be avoided:

$mystring = "He"llo,";

(Number of quotes is odd) PHP will stop at the second quotation mark and then wait for the end of instruction (the semicolon), but instead it will find the letter l, which is out of the string. Hence PHP will generate an error message.

Another frequent examples:

Combining PHP/HTML:

echo '';

Or:

echo "";

This is the same kind of situation, where we want PHP to add a string of characters containing quotes within a database.

In C language

The only solution is to make use of the escape character:

const char * mystring = Hello "John",";

Apostrophes can be used to declare ONLY ONE character:

char one_character = 'B';

VisualBasic 2.3 and ASCII

We will change a little bit. With VisualBasic:

Dim example as String

example = "Je m' appelle : " & chr$(34) & "Zeurtchreinshrentizer" & chr$(34)

In this case we make use of the ASCII code (34) : The quote, which will be interpreted later.

Test it with:

Dim example as String

example = "Je m' appelle : " & chr$(34) & "Zeurtchreinshrentizer" & chr$(34)

MsgBox example

Photo: © Everypixel

Tags:
Tomasz David
Tomasz David

Leave a Comment