| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | Python 3.8 prerelease | 250318T232107Z | Lucenapo |
| 006 | TIBASIC | 171117T015319Z | Khuldrae |
| 003 | Tcl | 171117T001008Z | sergiol |
| 008 | Python | 140119T175419Z | AMK |
| 017 | Befunge 98 3 | 140109T193905Z | Justin |
| 012 | Update Python | 140110T084113Z | flonk |
| 007 | huh? | 140112T164039Z | Timtech |
| 016 | GolfScript | 140111T045622Z | Ilmari K |
| 010 | Scala REPL | 140111T000651Z | Mika |
| 126 | JavaScript | 140110T141311Z | Doorknob |
| 004 | PowerShell | 140110T202942Z | goric |
| 040 | JavaScript | 140110T134715Z | Doorknob |
| 003 | Shell | 140109T192232Z | MvG |
| 004 | Perl | 140109T180919Z | primo |
| 004 | C | 140109T175530Z | MvG |
Python 3.8 (pre-release), 11 9 7 bytes
""#"*0"
The expression with quotes is ""#"*0" which is just "" as the comment is ignored. The expression without quotes is "#"*0 which is the empty string by product.
TI-BASIC, 6 bytes
""→Str1:"
"→Str1:
Both of these end up storing an empty string to Ans (and to Str1). The second works because storing to a variable also stores to Ans.
I also found another 6-byter:
"":":"
":":
Both of these will return ":".
I have checked every shorter chain matching "[":]{0-3}", and none meet the specifications.
Python (8 characters)
""""""""
and
""""""
both return empty string
Is based on idea of MvG
Befunge 98 - 3, 4, 6, 17
In these programs, I'll assume you just want to get a string, but you don't care if the program terminates. In other words, you want a code snippet that can be placed in the code somewhere. (Note: in Befunge, whatever is on the stack is a string. It is also a number)
This code snippet must be placed on the first part of the first line.
"g"
When the quotes are removed, g acts like 00g, so it pushes a g. Either way, it pushes a g.
""""
Works in Befunge 93 as well; it always pushes nothing.
This one is more interesting
""z"z"
It will always push z. z is a no-op so although the IP executes the command, it does nothing (I could have used , but z is more interesting).
This one only has one set of ", and is quine-like:
">:0g\1+:f`1+jb<"
It always pushes the contents of the string, but when the quotes are removed, it does it in a quine-like way. This must be placed on the first part of the first line, but adjustments can be made to put it elsewhere
Update: Python (12 chars)
program
""+"+""+"+""
"+"+""+"+"
both giving
'++'
Python (28 chars)
program
"".__str__()+".__str__()+"""
".__str__()+".__str__()+""
both giving
'.__str__()+'
Python (38 chars)
program
"".replace("+","+")+".replace("+","")"
".replace("+","+")+".replace("+","")
both giving
'.replace(,)'
huh?, 7
"Ouch!"
It works because both "Ouch!" and Ouch! are both invalid files, so the interpreter will return Ouch!
GolfScript: 16 chars
OK, since my 3-char solution "1" has been disqualified by a rules change, here's the straightforward solution in GolfScript:
"{`[46 126]+}.~"
With the quotes, this is just a double-quoted string literal. Without the quotes, it's a quine. The only slightly trickly part is that, to literally comply with the rule prohibiting alternative string delimiters, I have to encode the string '.~' as an array of ASCII codes [46 126] instead.
Scala REPL (10 chars)
"0+"++"+0"
0+"++"+0
are both returning:
0++0
0++0
Explanation: In the first case ++ treats the left string as a sequence of chars and concatenates the next sequence of chars and rebuilds a string. In the second case, + converts the first and last 0 to strings.
Note that everything is even a palindrom.
Other interesting solution using blocks (55 chars)
"{val o="*{val o=0;o}+".substring(9,12);val i="*0+";o}"
{val o="*{val o=0;o}+".substring(9,12);val i="*0+";o}
return both smileys:
;o}
;o}
Explanation: See the below decomposition
"{val o="*{val o=0;o}+".substring(9,12);val i="*0+";o}"
--multiplied by 0=>"" --------- returns "" ----- result
{val o="*{val o=0;o}+".substring(9,12);val i="*0+";o}
-------- o equals ";o}"-------- -i ignored- o
JavaScript, 28 27 15 12 (6 if result can be non-string (still works with output))
This one's pretty interesting.
"+"*1+"+"+""
The basic explanation is that "string" * 1 is NaN, and +"string" is also NaN. Therefore, it results in the string "NaN+" with or without the outside quotes.
For further explanation, it gets parsed as ("+" * 1) + ("+") + ("") with the outside quotes, and (+ "*1+") + "+" without.
The version that works if the output can be non-string:
"+"*""
Which does work when outputting (as JavaScript is weakly typed):
alert("+"*"") // NaN
alert(+"*") // NaN
Old version:
"+".replace(/./,NaN)+"+"+""
With the quotes outside, it is parsed as
("+".replace(/./, NaN)) + ("+") + ("")
"NaN" + "+" + ""
"NaN+"
Without the quotes, it gets parsed as
(+".replace(/./,NaN)+") + ("+")
// +"any string" evaluates to NaN
NaN + "+"
"NaN+"
PowerShell, 4 chars
"$?"
$?
Output:
TRUE
TRUE
This uses the automatic variable $?, which is a Boolean containing the execution status of the last run command. The only effect of wrapping it in quotes is that it is converted to a string prior to being printed, which is a transparent operation.
JavaScript, 40
"(f=function (){return'(f='+f+')()'})()"
When you remove the quotes, it becomes an immediately invoked function expression, and this function happens to return '(f='+f+')()', which is the entirety of the string. (Absuing Function#toString. ;))
Perl - 4 bytes
"$~"
Test program:
print"$~";
print$~;
Ouput:
STDOUT
STDOUT
C (4 chars)
The expression
""""
satisfies the minimal requirement of three characters. It encodes the empty string, no matter even if you omit one level of quotes:
""
If you want the resulting string to be at least three characters, here is an expression of length 10 which results in three characters of string, namely three spaces:
"" " "
" "
If you don't like spaces either, here are four printable characters in a length 12 expression:
""/**/"/**/"
"/**/"/**/
Putting these expressions into a small program:
#include <stdio.h>
#define CASE(s) printf("'%s'\n", s)
int main() {
CASE("""");
CASE("");
CASE("" " ");
CASE(" " );
CASE(""/**/"/**/");
CASE("/**/"/**/);
}
Output:
''
''
' '
' '
'/**/'
'/**/'