Programming - Quotation marks, apostrophes and strings

February 2017

How to use single and double quotation marks in strings?


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, etc. ...

How to add a quotation mark without truncating the string?

Your message is truncated or receive an error message after adding quotes/apostrophe to a string.


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)
$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 '<iframe src="http://www.mysite.ty/mycode.html" height="500px" width="96%" style="align: center;"></iframe>';

echo "<iframe src=\"http://www.mysite.ty/mycode.html\" height=\"500px\" width=\"96%\" style=\"align: center;\"></iframe>";

This is the same kind of the 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 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 ... take the Visual Basic:
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 exemple as String  
exemple = "Je m' appelle : " & chr$(34) & "Zeurtchreinshrentizer" & chr$(34)  
MsgBox exemple

So I closed the string to implement the chr$().
You can use any ASCII codes with chr$().


Published by deri58. Latest update on April 3, 2013 at 04:21 AM by deri58.
This document, titled "Programming - Quotation marks, apostrophes and strings," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (