Strip comments from a string: Difference between revisions
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(43 intermediate revisions by 28 users not shown) | |||
Line 1: | Line 1: | ||
{{clarify task}} |
{{clarify task}} |
||
{{task|Basic language learning}} |
{{task|Basic language learning}} |
||
[[Category:String manipulation]] |
|||
The task is to remove text that follow any of a set of comment markers, (in these examples either a hash or a semicolon) from a string or input line. |
The task is to remove text that follow any of a set of comment markers, (in these examples either a hash or a semicolon) from a string or input line. |
||
Line 26: | Line 27: | ||
{{Template:Strings}} |
|||
;Related task: |
|||
* [[Strip block comments]] |
|||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
<syntaxhighlight lang="11l">F remove_comments(line, sep) |
|||
V? p = line.find(sep) |
|||
I p != N |
|||
R line[0.<p].rtrim(‘ ’) |
|||
R line |
|||
print(remove_comments(‘apples ; pears # and bananas’, (‘;’, ‘#’))) |
|||
print(remove_comments(‘apples ; pears # and bananas’, ‘#’)) |
|||
print(remove_comments(‘apples ; pears # and bananas’, ‘!’))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
apples |
|||
apples ; pears |
|||
apples ; pears # and bananas |
|||
</pre> |
|||
=={{header|68000 Assembly}}== |
|||
<syntaxhighlight lang="68000devpac">StripComments: |
|||
;prints a string but stops at the comment character |
|||
;INPUT: D7 = comment character(s) of choice |
|||
;A0 = source address of string |
|||
;up to four can be used, each takes up a different 8 bits of the register |
|||
;to omit an argument, leave its bits as zero. |
|||
.loop: |
|||
MOVE.B (A0)+,D0 |
|||
CMP.B #0,D0 ;check for null terminator |
|||
beq .done |
|||
CMP.B D7,D0 ;check the first comment char |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B D7,D0 ;check the second comment char |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B D7,D0 ;check the third comment char |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B D7,D0 ;check the fourth comment char |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B #' ',D0 |
|||
BNE dontCheckNext |
|||
MOVE.B (A0),D1 ;look ahead one character, if that character is a comment char or null terminator, stop here |
|||
CMP.B #0,D1 |
|||
beq .done |
|||
CMP.B D7,D1 |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B D7,D1 |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B D7,D1 |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
CMP.B D7,D1 |
|||
beq .done |
|||
ROR.L #8,D7 |
|||
dontCheckNext: |
|||
jsr PrintChar |
|||
bra .loop |
|||
.done: |
|||
rts |
|||
TestString: |
|||
dc.b "apples ; pears # and bananas",0</syntaxhighlight> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">PROC Strip(CHAR ARRAY text,chars,result) |
|||
BYTE i,j,pos,found |
|||
pos=text(0) |
|||
FOR i=1 TO text(0) |
|||
DO |
|||
found=0 |
|||
FOR j=1 TO chars(0) |
|||
DO |
|||
IF text(i)=chars(j) THEN |
|||
found=1 EXIT |
|||
FI |
|||
OD |
|||
IF found THEN |
|||
pos=i-1 EXIT |
|||
FI |
|||
OD |
|||
WHILE pos>0 AND text(pos)=' |
|||
DO |
|||
pos==-1 |
|||
OD |
|||
SCopyS(result,text,1,pos) |
|||
RETURN |
|||
PROC Test(CHAR ARRAY text,chars) |
|||
CHAR ARRAY result(255) |
|||
Strip(text,chars,result) |
|||
PrintF("""%S"", ""%S"" -> ""%S""%E%E",text,chars,result) |
|||
RETURN |
|||
PROC Main() |
|||
Test("apples, pears # and bananas","#;") |
|||
Test("apples, pears ; and bananas","#;") |
|||
Test("qwerty # asdfg ; zxcvb","#") |
|||
Test("qwerty # asdfg ; zxcvb",";") |
|||
Test(" ;this is a comment","#;") |
|||
Test("#this is a comment","#;") |
|||
Test(" ",";") |
|||
Test("","#") |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strip_comments_from_a_string.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
"apples, pears # and bananas", "#;" -> "apples, pears" |
|||
"apples, pears ; and bananas", "#;" -> "apples, pears" |
|||
"qwerty # asdfg ; zxcvb", "#" -> "qwerty" |
|||
"qwerty # asdfg ; zxcvb", ";" -> "qwerty # asdfg" |
|||
" ;this is a comment", "#;" -> "" |
|||
"#this is a comment", "#;" -> "" |
|||
" ", ";" -> "" |
|||
"", "#" -> "" |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Program is |
procedure Program is |
||
Comment_Characters : String := "#;"; |
Comment_Characters : String := "#;"; |
||
Line 50: | Line 193: | ||
end; |
end; |
||
end loop; |
end loop; |
||
end Program;</ |
end Program;</syntaxhighlight> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">strip_comments(data b) |
||
{ |
{ |
||
b.size(b.look(0, ";#")).bf_drop(" \t").bb_drop(" \t"); |
b.size(b.look(0, ";#")).bf_drop(" \t").bb_drop(" \t"); |
||
Line 65: | Line 208: | ||
0; |
0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 71: | Line 214: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}} |
||
< |
<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script # |
||
PROC trim comment = (STRING line, CHAR marker)STRING:( |
PROC trim comment = (STRING line, CHAR marker)STRING:( |
||
Line 96: | Line 239: | ||
grep in string(re marker, line, index, NIL); |
grep in string(re marker, line, index, NIL); |
||
print((q, line[:index-1], q, new line)) |
print((q, line[:index-1], q, new line)) |
||
END CO</ |
END CO</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 108: | Line 251: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Leading and trailing spaces are removed from the result, as per the March 29 2011 task version. |
Leading and trailing spaces are removed from the result, as per the March 29 2011 task version. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% determines the non-comment portion of the string s, startPos and endPos are % |
% determines the non-comment portion of the string s, startPos and endPos are % |
||
% returned set to the beginning and ending character positions (indexed from 0) % |
% returned set to the beginning and ending character positions (indexed from 0) % |
||
Line 150: | Line 293: | ||
testStripComments( " apples, pears" ) |
testStripComments( " apples, pears" ) |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 160: | Line 303: | ||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
< |
<syntaxhighlight lang="applesoftbasic">10 LET C$ = ";#" |
||
20 S$(1)="APPLES, PEARS # AND BANANAS" |
20 S$(1)="APPLES, PEARS # AND BANANAS" |
||
30 S$(2)="APPLES, PEARS ; AND BANANAS" |
30 S$(2)="APPLES, PEARS ; AND BANANAS" |
||
Line 185: | Line 328: | ||
250 IF A$ = " " THEN NEXT I |
250 IF A$ = " " THEN NEXT I |
||
260 LET S$ = MID$(S$, 1, I) |
260 LET S$ = MID$(S$, 1, I) |
||
270 RETURN</ |
270 RETURN</syntaxhighlight> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">stripComments: function [str][ |
|||
strip replace str {/[#;].+/} "" |
|||
] |
|||
loop ["apples, pears # and bananas", "apples, pears ; and bananas"] 'str [ |
|||
print [str "->" stripComments str] |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>apples, pears # and bananas -> apples, pears |
|||
apples, pears ; and bananas -> apples, pears</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Delims := "#;" |
||
str := "apples, pears # and bananas" |
str := "apples, pears # and bananas" |
||
str2:= "apples, pears, `; and bananas" ; needed to escape the ; since that is AHK's comment marker |
str2:= "apples, pears, `; and bananas" ; needed to escape the ; since that is AHK's comment marker |
||
Line 206: | Line 364: | ||
} |
} |
||
return String1 |
return String1 |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 212: | Line 370: | ||
apples, pears, |
apples, pears, |
||
</pre> |
</pre> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
It was always said in discussion, the task is not really stripping comments. It's only a truncation. |
It was always said in discussion, the task is not really stripping comments. It's only a truncation. |
||
<syntaxhighlight lang="autoit"> |
|||
<lang AutoIt> |
|||
Dim $Line1 = "apples, pears # and bananas" |
Dim $Line1 = "apples, pears # and bananas" |
||
Dim $Line2 = "apples, pears ; and bananas" |
Dim $Line2 = "apples, pears ; and bananas" |
||
Line 234: | Line 391: | ||
Next |
Next |
||
EndFunc ;==>_StripAtMarker |
EndFunc ;==>_StripAtMarker |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 248: | Line 405: | ||
That means: the comment starts with the first semicolon outside a string. |
That means: the comment starts with the first semicolon outside a string. |
||
<syntaxhighlight lang="autoit"> |
|||
<lang AutoIt> |
|||
Dim $aLines[4] = _ |
Dim $aLines[4] = _ |
||
[ _ |
[ _ |
||
Line 305: | Line 462: | ||
Next |
Next |
||
EndFunc ;==>_LineStripComment |
EndFunc ;==>_LineStripComment |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 331: | Line 488: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">#!/usr/local/bin/awk -f |
||
{ |
{ |
||
sub("[ \t]*[#;].*$","",$0); |
sub("[ \t]*[#;].*$","",$0); |
||
print; |
print; |
||
}</ |
}</syntaxhighlight> |
||
=={{header| |
=={{header|BASIC}}== |
||
==={{header|ANSI BASIC}}=== |
|||
<lang bbcbasic> marker$ = "#;" |
|||
{{works with|Decimal BASIC}} |
|||
PRINT FNstripcomment("apples, pears # and bananas", marker$) |
|||
<syntaxhighlight lang="basic">100 DECLARE EXTERNAL FUNCTION FNstripcomment$ |
|||
PRINT FNstripcomment("apples, pears ; and bananas", marker$) |
|||
110 LET marker$="#;" |
|||
PRINT FNstripcomment(" apples, pears ", marker$) |
|||
120 PRINT """";FNstripcomment$("apples, pears # and bananas", marker$);"""" |
|||
END |
|||
130 PRINT """";FNstripcomment$("apples, pears ; and bananas", marker$);"""" |
|||
140 PRINT """";FNstripcomment$(" apples, pears ", marker$);"""" |
|||
DEF FNstripcomment(text$, delim$) |
|||
150 END |
|||
LOCAL I%, D% |
|||
160 ! |
|||
FOR I% = 1 TO LEN(delim$) |
|||
170 EXTERNAL FUNCTION FNstripcomment$(text$, delim$) |
|||
180 FOR I=1 TO LEN(delim$) |
|||
IF D% text$ = LEFT$(text$, D%-1) |
|||
190 LET D = POS(text$, delim$(I:I)) |
|||
NEXT I% |
|||
200 IF D>0 THEN LET text$ = text$(1:D-1) |
|||
210 NEXT I |
|||
WHILE LEFT$(text$) = " " text$ = RIGHT$(text$) : ENDWHILE |
|||
220 LET FNstripcomment$=RTRIM$(text$) |
|||
= text$</lang> |
|||
230 END FUNCTION</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
"apples, pears" |
|||
"apples, pears" |
|||
" apples, pears"</pre> |
|||
==={{header|BASIC256}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="freebasic">arraybase 1 |
|||
dim s$(4) |
|||
s$[1] = "apples, pears # and bananas" |
|||
s$[2] = "apples, pears ; and bananas" |
|||
s$[3] = "# this is a comment" |
|||
s$[4] = " # this is a comment with leading whitespace" |
|||
for i = 1 to 4 |
|||
call stripComment(s$[i], "#;") |
|||
print s$[i], " => Length = "; length(s$[i]) |
|||
next i |
|||
end |
|||
subroutine stripComment(s$, commentMarkers$) |
|||
if s$ = "" then return |
|||
i = instr(s$, commentMarkers$) |
|||
if i > 0 then |
|||
s$ = left$(s$, i - 1) |
|||
s$ = trim(s$) # removes both leading and trailing whitespace |
|||
end if |
|||
end subroutine</syntaxhighlight> |
|||
{{out}} |
|||
<pre>apples, pears # and bananas => Length = 27 |
|||
apples, pears ; and bananas => Length = 27 |
|||
# this is a comment => Length = 19 |
|||
# this is a comment with leading whitespace => Length = 45</pre> |
|||
=={{header|BQN}}== |
|||
<code>StripW</code> only removes spaces. <code>StripC</code> uses that in conjunction with a filter that removes comments. |
|||
<syntaxhighlight lang="bqn">StripW←((∨`∧∨`⌾⌽)' '⊸≠)⊸/ |
|||
StripC←StripW (¬·∨`∊⟜"#;")⊸/ |
|||
•Show StripC " apples, pears # and bananas " |
|||
•Show StripC " apples, pears ; and bananas"</syntaxhighlight> |
|||
<syntaxhighlight lang="text">"apples, pears" |
|||
"apples, pears"</syntaxhighlight> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( " apples, pears # and bananas |
||
oranges, mangos ; and a durian" |
oranges, mangos ; and a durian" |
||
: ?text |
: ?text |
||
Line 375: | Line 577: | ||
& !newText \n cleanUp$!text:?newText |
& !newText \n cleanUp$!text:?newText |
||
& out$(str$!newText) |
& out$(str$!newText) |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 382: | Line 584: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
int main() |
int main() |
||
Line 407: | Line 609: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 419: | Line 621: | ||
The modified string is : apples, pears |
The modified string is : apples, pears |
||
Do you want to repeat (y/n): n |
Do you want to repeat (y/n): n |
||
</pre> |
|||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp"> |
|||
using System.Text.RegularExpressions; |
|||
string RemoveComments(string str, string delimiter) |
|||
{ |
|||
//regular expression to find a character (delimiter) and |
|||
// replace it and everything following it with an empty string. |
|||
//.Trim() will remove all beginning and ending white space. |
|||
return Regex.Replace(str, delimiter + ".+", string.Empty).Trim(); |
|||
} |
|||
</syntaxhighlight> |
|||
Sample output: |
|||
<pre> |
|||
Console.WriteLine(RemoveComments("apples, pears # and bananas", "#")); |
|||
Console.WriteLine(RemoveComments("apples, pears ; and bananas", ";")); |
|||
apples, pears |
|||
apples, pears |
|||
</pre> |
</pre> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 444: | Line 667: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre> |
<pre> |
||
Line 453: | Line 676: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|Clojure}}== |
||
<syntaxhighlight lang="clojure">> (apply str (take-while #(not (#{\# \;} %)) "apples # comment")) |
|||
"apples "</syntaxhighlight> |
|||
=={{header|COBOL}}== |
|||
<lang csharp> |
|||
{{works with|GnuCOBOL}} |
|||
using System.Text.RegularExpressions; |
|||
<syntaxhighlight lang="cobol"> identification division. |
|||
program-id. StripComments. |
|||
data division. |
|||
string RemoveComments(string str, string delimiter) |
|||
working-storage section. |
|||
01 line-text pic x(64). |
|||
// replace it and everything following it with an empty string. |
|||
//.Trim() will remove all beginning and ending white space. |
|||
return Regex.Replace(str, delimiter + ".+", string.Empty).Trim(); |
|||
} |
|||
</lang> |
|||
Sample output: |
|||
<pre> |
|||
Console.WriteLine(RemoveComments("apples, pears # and bananas", "#")); |
|||
Console.WriteLine(RemoveComments("apples, pears ; and bananas", ";")); |
|||
apples, pears |
|||
apples, pears |
|||
</pre> |
|||
procedure division. |
|||
=={{header|Clojure}}== |
|||
main. |
|||
<lang clojure>> (apply str (take-while #(not (#{\# \;} %)) "apples # comment")) |
|||
move "apples, pears # and bananas" to line-text |
|||
"apples "</lang> |
|||
perform show-striped-text |
|||
move "apples, pears ; and bananas" to line-text |
|||
perform show-striped-text |
|||
stop run |
|||
. |
|||
show-striped-text. |
|||
unstring line-text delimited by "#" or ";" into line-text |
|||
display quote, function trim(line-text), quote |
|||
.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>"apples, pears" |
|||
"apples, pears"</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun strip-comments (s cs) |
||
"Truncate s at the first occurrence of a character in cs." |
"Truncate s at the first occurrence of a character in cs." |
||
(defun comment-char-p (c) |
(defun comment-char-p (c) |
||
(some #'(lambda (x) (char= x c)) cs)) |
(some #'(lambda (x) (char= x c)) cs)) |
||
(let ((pos (position-if #'comment-char-p s))) |
(let ((pos (position-if #'comment-char-p s))) |
||
(subseq s 0 pos)))</ |
(subseq s 0 pos)))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 499: | Line 728: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.regex; |
||
string remove1LineComment(in string s, in string pat=";#") { |
string remove1LineComment(in string s, in string pat=";#") { |
||
Line 511: | Line 740: | ||
writeln(s, "\n====>\n", s.remove1LineComment()); |
writeln(s, "\n====>\n", s.remove1LineComment()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>apples, pears # and bananas |
<pre>apples, pears # and bananas |
||
Line 520: | Line 749: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program StripComments; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 537: | Line 766: | ||
Writeln('apples, pears ; and bananas --> ' + DoStripComments('apples, pears ; and bananas',';')); |
Writeln('apples, pears ; and bananas --> ' + DoStripComments('apples, pears ; and bananas',';')); |
||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi">function StripComments(s : String) : String; |
||
begin |
begin |
||
var p := FindDelimiter('#;', s); |
var p := FindDelimiter('#;', s); |
||
Line 549: | Line 778: | ||
PrintLn(StripComments('apples, pears # and bananas')); |
PrintLn(StripComments('apples, pears # and bananas')); |
||
PrintLn(StripComments('apples, pears ; and bananas'));</ |
PrintLn(StripComments('apples, pears ; and bananas'));</syntaxhighlight> |
||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang=easylang> |
|||
func$ strip s$ . |
|||
i = 1 |
|||
repeat |
|||
c$ = substr s$ i 1 |
|||
until c$ = "#" or c$ = ";" or c$ = "" |
|||
if c$ = " " and sp = 0 |
|||
sp = i |
|||
elif c$ <> " " |
|||
sp = 0 |
|||
. |
|||
i += 1 |
|||
. |
|||
if sp = 0 |
|||
sp = i |
|||
. |
|||
return substr s$ 1 (sp - 1) |
|||
. |
|||
print strip "Regular string" & "." |
|||
print strip "With a hash# a comment" & "." |
|||
print strip "With a hash # a comment" & "." |
|||
print strip "With a semicolon ; a comment" & "." |
|||
print strip "No comment " & "." |
|||
</syntaxhighlight> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( strip_comments_from_string ). |
-module( strip_comments_from_string ). |
||
Line 569: | Line 825: | ||
not_comment( $; ) -> false; |
not_comment( $; ) -> false; |
||
not_comment( _ ) -> true. |
not_comment( _ ) -> true. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 579: | Line 835: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">let stripComments s = |
||
s |
s |
||
|> Seq.takeWhile (fun c -> c <> '#' && c <> ';') |
|> Seq.takeWhile (fun c -> c <> '#' && c <> ';') |
||
|> Seq.map System.Char.ToString |
|> Seq.map System.Char.ToString |
||
|> Seq.fold (+) ""</ |
|> Seq.fold (+) ""</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USE: sequences.extras |
||
: strip-comments ( str -- str' ) |
: strip-comments ( str -- str' ) |
||
[ "#;" member? not ] take-while "" like ;</ |
[ "#;" member? not ] take-while "" like ;</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Using a regular expression: |
Using a regular expression: |
||
< |
<syntaxhighlight lang="fantom">class Main |
||
{ |
{ |
||
static Str removeComment (Str str) |
static Str removeComment (Str str) |
||
Line 610: | Line 866: | ||
echo (removeComment ("String with comment ; here")) |
echo (removeComment ("String with comment ; here")) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 617: | Line 873: | ||
NOTES: |
NOTES: |
||
1. SCAN |
1. SKIP and SCAN are not ANS Forth but are common in most Forth systems as either library functions or resident. |
||
2. -TRAILING is a resident function in most Forth systems. Shown for clarity. |
2. -TRAILING is a resident function in most Forth systems. Shown for clarity. |
||
Tested with Swift Forth on OS/X, GForth on Windows |
|||
Tested with GForth on Windows |
|||
<LANG FORTH>\ Rosetta Code Strip Comment |
|||
<syntaxhighlight lang="forth">\ Rosetta Code Strip Comment |
|||
: LASTCHAR ( addr len -- addr len c) 2DUP + C@ ; |
: LASTCHAR ( addr len -- addr len c) 2DUP + C@ ; |
||
: COMMENT? ( char -- ? ) S" #;" ROT SCAN NIP ; \ test char for "#;" |
|||
: |
: -LEADING ( addr len -- addr' len') BL SKIP ; \ remove leading space characters |
||
: -COMMENT ( addr len -- addr len') \ removes # or ; comments |
: -COMMENT ( addr len -- addr len') \ removes # or ; comments |
||
Line 630: | Line 888: | ||
BEGIN |
BEGIN |
||
LASTCHAR COMMENT? 0= |
LASTCHAR COMMENT? 0= |
||
WHILE \ while not a comment char... |
WHILE \ while not a comment char... |
||
1- \ reduce length by 1 |
1- \ reduce length by 1 |
||
REPEAT |
REPEAT |
||
1- ; \ remove 1 more (the comment char) |
1- ; \ remove 1 more (the comment char) |
||
: -TRAILING ( adr len -- |
: -TRAILING ( adr len -- addr len') \ remove trailing spaces |
||
1- |
1- |
||
BEGIN |
BEGIN |
||
LASTCHAR BL = |
LASTCHAR BL = |
||
WHILE |
WHILE \ while lastchar = blank |
||
1- |
1- \ reduce length by 1 |
||
REPEAT |
REPEAT |
||
1+ ; |
1+ ; |
||
: COMMENT-STRIP ( addr len -- addr 'len) -COMMENT |
: COMMENT-STRIP ( addr len -- addr 'len) -LEADING -COMMENT -TRAILING ; |
||
</syntaxhighlight>Tested at the Forth console |
|||
<pre>S" apples, pears # and bananas" COMMENT-STRIP TYPE apples, pears ok |
|||
Tested at the Forth console |
|||
S" apples, pears ; and bananas" COMMENT-STRIP TYPE apples, pears ok</pre> |
|||
<lang Forth>S" apples, pears # and bananas" COMMENT-STRIP TYPE apples, pears ok |
|||
S" apples, pears ; and bananas" COMMENT-STRIP TYPE apples, pears ok</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran">!**************************************************** |
||
module string_routines |
module string_routines |
||
!**************************************************** |
!**************************************************** |
||
Line 695: | Line 951: | ||
!**************************************************** |
!**************************************************** |
||
end program main |
end program main |
||
!****************************************************</ |
!****************************************************</syntaxhighlight> |
||
output: |
output: |
||
<pre> |
<pre> |
||
Line 703: | Line 959: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Sub stripComment(s As String, commentMarkers As String) |
Sub stripComment(s As String, commentMarkers As String) |
||
Line 729: | Line 985: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 738: | Line 994: | ||
=> Length = 0 |
=> Length = 0 |
||
</pre> |
</pre> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasid"> |
|||
include "NSLog.incl" |
|||
local fn StripCommentsFromString( string as CFStringRef ) as CFStringRef |
|||
CFRange range = fn StringRangeOfCharacterFromSet( string, fn CharacterSetWithCharactersInString( @"#;" ) ) |
|||
if ( range.location != NSNotFound ) |
|||
string = fn StringSubstringToIndex( string, range.location ) |
|||
string = fn StringByTrimmingCharactersInSet( string, fn CharacterSetWhitespaceSet ) |
|||
end if |
|||
end fn = string |
|||
NSLog(@"%@",fn StripCommentsFromString(@"apples, pears # and bananas")) |
|||
NSLog(@"%@",fn StripCommentsFromString(@"apples, pears ; and bananas")) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 766: | Line 1,040: | ||
fmt.Printf("stripped: %q\n", stripComment(s)) |
fmt.Printf("stripped: %q\n", stripComment(s)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 778: | Line 1,052: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def stripComments = { it.replaceAll(/\s*[#;].*$/, '') }</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="groovy">assert 'apples, pears' == stripComments('apples, pears # and bananas') |
||
assert 'apples, pears' == stripComments('apples, pears ; and bananas')</ |
assert 'apples, pears' == stripComments('apples, pears ; and bananas')</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">ms = ";#" |
||
main = getContents >>= |
main = getContents >>= |
||
mapM_ (putStrLn . takeWhile (`notElem` ms)) . lines</ |
mapM_ (putStrLn . takeWhile (`notElem` ms)) . lines</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon"># strip_comments: |
||
# return part of string up to first character in 'markers', |
# return part of string up to first character in 'markers', |
||
# or else the whole string if no comment marker is present |
# or else the whole string if no comment marker is present |
||
Line 801: | Line 1,075: | ||
write (strip_comments ("apples, pears # and bananas", cset ("#;"))) |
write (strip_comments ("apples, pears # and bananas", cset ("#;"))) |
||
write (strip_comments ("apples, pears ; and bananas", cset ("#;"))) |
write (strip_comments ("apples, pears ; and bananas", cset ("#;"))) |
||
end</ |
end</syntaxhighlight> |
||
Output: |
Output: |
||
Line 811: | Line 1,085: | ||
=={{header|Inform 7}}== |
=={{header|Inform 7}}== |
||
< |
<syntaxhighlight lang="inform7">Home is a room. |
||
When play begins: |
When play begins: |
||
Line 821: | Line 1,095: | ||
say "[T] -> "; |
say "[T] -> "; |
||
replace the regular expression "<#;>.*$" in T with ""; |
replace the regular expression "<#;>.*$" in T with ""; |
||
say "[T][line break]".</ |
say "[T][line break]".</syntaxhighlight> |
||
Since square brackets have a special meaning in strings, Inform's regular expression syntax uses angle brackets for character grouping. |
Since square brackets have a special meaning in strings, Inform's regular expression syntax uses angle brackets for character grouping. |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution 1''' (mask & filter): < |
'''Solution 1''' (mask & filter): <syntaxhighlight lang="j">strip=: dltb@(#~ *./\@:-.@e.&';#')</syntaxhighlight> |
||
'''Solution 2''' (index & cut): < |
'''Solution 2''' (index & cut): <syntaxhighlight lang="j">strip=: dltb@({.~ <./@i.&';#')</syntaxhighlight> |
||
'''Example''':< |
'''Example''':<syntaxhighlight lang="j"> dquote strip ' apples, pears # and bananas' NB. quote result to show stripped whitespace |
||
"apples, pears" |
"apples, pears" |
||
strip ' apples, pears ; and bananas' |
strip ' apples, pears ; and bananas' |
||
apples, pears</syntaxhighlight> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.io.*; |
||
public class StripLineComments{ |
public class StripLineComments{ |
||
Line 857: | Line 1,131: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES5=== |
===ES5=== |
||
< |
<syntaxhighlight lang="javascript">function stripComments(s) { |
||
var re1 = /^\s+|\s+$/g; // Strip leading and trailing spaces |
var re1 = /^\s+|\s+$/g; // Strip leading and trailing spaces |
||
var re2 = /\s*[#;].+$/g; // Strip everything after # or ; to the end of the line, including preceding spaces |
var re2 = /\s*[#;].+$/g; // Strip everything after # or ; to the end of the line, including preceding spaces |
||
Line 873: | Line 1,146: | ||
alert(stripComments(s1) + '\n' + stripComments(s2)); |
alert(stripComments(s1) + '\n' + stripComments(s2)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
A more efficient version that caches the regular expressions in a closure: |
A more efficient version that caches the regular expressions in a closure: |
||
< |
<syntaxhighlight lang="javascript">var stripComments = (function () { |
||
var re1 = /^\s+|\s+$/g; |
var re1 = /^\s+|\s+$/g; |
||
var re2 = /\s*[#;].+$/g; |
var re2 = /\s*[#;].+$/g; |
||
Line 884: | Line 1,157: | ||
}; |
}; |
||
}()); |
}()); |
||
</syntaxhighlight> |
|||
</lang> |
|||
A difference with the two versions is that in the first, all declarations are processed before code is executed so the function declaration can be after the code that calls it. However in the second example, the expression creating the function must be executed before the function is available, so it must be before the code that calls it. |
A difference with the two versions is that in the first, all declarations are processed before code is executed so the function declaration can be after the code that calls it. However in the second example, the expression creating the function must be executed before the function is available, so it must be before the code that calls it. |
||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 949: | Line 1,222: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>apples, pears |
<pre>apples, pears |
||
Line 956: | Line 1,229: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
If your version of jq has regex support, the task can be accomplished with the following one-liner: |
If your version of jq has regex support, the task can be accomplished with the following one-liner: |
||
< |
<syntaxhighlight lang="jq">sub("[#;].*";"") | sub("^\\s+";"") | sub("\\s+$";"")</syntaxhighlight> |
||
Otherwise, we can define strip_comment as a jq filter, as follows. For clarity, the helper functions are presented as top-level functions. |
Otherwise, we can define strip_comment as a jq filter, as follows. For clarity, the helper functions are presented as top-level functions. |
||
< |
<syntaxhighlight lang="jq"># define whitespace here as a tab, space, newline, return or form-feed character: |
||
def is_whitespace: . as $in | " \n\r\f\t" | index($in); |
def is_whitespace: . as $in | " \n\r\f\t" | index($in); |
||
Line 979: | Line 1,252: | ||
end ) as $ix |
end ) as $ix |
||
| if $ix then .[0:$ix] else . end |
| if $ix then .[0:$ix] else . end |
||
| trim;</ |
| trim;</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">" abc ; def # ghi" | strip_comment</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">"abc"</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<tt>striplinecomment</tt> is designed to be flexible and robust. By default <tt>#</tt> and <tt>;</tt> are considered comment defining characters, but any characters can be used by passing them as the string <tt>cchars</tt>. All such characters are escaped in the regular expression used to eliminate comments to allow characters special to the Regex language (e.g. <tt>^</tt>, <tt>$</tt>, <tt>[</tt>) to be used as a comment character. |
<tt>striplinecomment</tt> is designed to be flexible and robust. By default <tt>#</tt> and <tt>;</tt> are considered comment defining characters, but any characters can be used by passing them as the string <tt>cchars</tt>. All such characters are escaped in the regular expression used to eliminate comments to allow characters special to the Regex language (e.g. <tt>^</tt>, <tt>$</tt>, <tt>[</tt>) to be used as a comment character. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
using Printf |
|||
function striplinecomment{T<:String,U<:String}(a::T, cchars::U="#;") |
|||
function striplinecomment(a::String, cchars::String="#;") |
|||
b = strip(a) |
b = strip(a) |
||
0 < length(cchars) || return b |
0 < length(cchars) || return b |
||
for c in cchars |
for c in cchars |
||
r = Regex(@sprintf "\\%c.*" c) |
r = Regex(@sprintf "\\%c.*" c) |
||
b = replace(b, r |
b = replace(b, r => "") |
||
end |
end |
||
strip(b) |
strip(b) |
||
end |
end |
||
tests = |
tests = ("apples, pears # and bananas", |
||
"apples, pears ; and bananas", |
"apples, pears ; and bananas", |
||
" apples, pears & bananas ", |
" apples, pears & bananas ", |
||
" # " |
" # ") |
||
for t in tests |
for t in tests |
||
Line 1,010: | Line 1,285: | ||
println(" \"", s, "\"") |
println(" \"", s, "\"") |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,034: | Line 1,309: | ||
It then removes whitespace from the beginning and end of the resulting string: |
It then removes whitespace from the beginning and end of the resulting string: |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
val r = Regex("""(/\*.*\*/|//.*$)""") |
val r = Regex("""(/\*.*\*/|//.*$)""") |
||
Line 1,048: | Line 1,323: | ||
) |
) |
||
for (string in strings) println(stripComments(string)) |
for (string in strings) println(stripComments(string)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,059: | Line 1,334: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">string1$ = "apples, pears # and bananas" |
||
string2$ = "pears;, " + chr$(34) + "apples ; " + chr$(34) + " an;d bananas" |
string2$ = "pears;, " + chr$(34) + "apples ; " + chr$(34) + " an;d bananas" |
||
commentMarker$ = "; #" |
commentMarker$ = "; #" |
||
Line 1,074: | Line 1,349: | ||
next i |
next i |
||
parse$ = Left$(string$, (i - 1)) |
parse$ = Left$(string$, (i - 1)) |
||
End Function</ |
End Function</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">comment_symbols = ";#" |
||
s1 = "apples, pears # and bananas" |
s1 = "apples, pears # and bananas" |
||
Line 1,083: | Line 1,358: | ||
print ( string.match( s1, "[^"..comment_symbols.."]+" ) ) |
print ( string.match( s1, "[^"..comment_symbols.."]+" ) ) |
||
print ( string.match( s2, "[^"..comment_symbols.."]+" ) )</ |
print ( string.match( s2, "[^"..comment_symbols.."]+" ) )</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">> use StringTools in map( Trim@Take, [ "\t\t apples, pears \t# and bananas", " apples, pears ; and bananas \t" ], "#;" ) end; |
||
["apples, pears", "apples, pears"]</ |
["apples, pears", "apples, pears"]</syntaxhighlight> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">a = "apples, pears # and bananas |
||
apples, pears ; and bananas"; |
apples, pears ; and bananas"; |
||
b = StringReplace[a, RegularExpression["[ ]+[#;].+[\n]"] -> "\n"]; |
b = StringReplace[a, RegularExpression["[ ]+[#;].+[\n]"] -> "\n"]; |
||
StringReplace[b, RegularExpression["[ ]+[#;].+$"] -> ""] // FullForm</ |
StringReplace[b, RegularExpression["[ ]+[#;].+$"] -> ""] // FullForm</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>"apples, pears\napples, pears"</pre> |
<pre>"apples, pears\napples, pears"</pre> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function line = stripcomment(line) |
||
e = min([find(line=='#',1),find(line==';',1)]); |
e = min([find(line=='#',1),find(line==';',1)]); |
||
if ~isempty(e) |
if ~isempty(e) |
||
Line 1,108: | Line 1,381: | ||
end; |
end; |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>>> stripcomment('apples, pears # and bananas\n') |
<pre>>> stripcomment('apples, pears # and bananas\n') |
||
Line 1,116: | Line 1,389: | ||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
< |
<syntaxhighlight lang="miniscript">strip = function(test) |
||
comment = test.indexOf("#") |
comment = test.indexOf("#") |
||
if comment == null then comment = test.indexOf(";") |
if comment == null then comment = test.indexOf(";") |
||
Line 1,129: | Line 1,402: | ||
print strip("This is a semicolon test ; a comment") + "." |
print strip("This is a semicolon test ; a comment") + "." |
||
print strip("This is a no comment test ") + "." |
print strip("This is a no comment test ") + "." |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,138: | Line 1,411: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
proc removeComments(line |
proc removeComments(line: string; sep: char): string = |
||
line.split(sep)[0].strip(leading = false) |
line.split(sep)[0].strip(leading = false) |
||
const |
|||
Str1 = "apples, pears # and bananas" |
|||
Str2 = "apples, pears ; and bananas" |
|||
echo "Original: “$#”" % Str1 |
|||
echo removeComments("apples, pears # and bananas", '#') |
|||
echo "Stripped: “$#”" % Str1.removeComments('#') |
|||
echo removeComments("apples, pears ; and bananas", ';')</lang> |
|||
echo "Original: “$#”" % Str2 |
|||
echo "Stripped: “$#”" % Str2.removeComments(';')</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Original: “apples, pears # and bananas” |
|||
Stripped: “apples, pears” |
|||
Original: “apples, pears ; and bananas” |
|||
Stripped: “apples, pears”</pre> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">use System.IO.File; |
||
class StripComments { |
class StripComments { |
||
Line 1,175: | Line 1,460: | ||
}; |
}; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let strip_comments str = |
||
let len = String.length str in |
let len = String.length str in |
||
let rec aux print i = |
let rec aux print i = |
||
Line 1,197: | Line 1,482: | ||
strip_comments "apples, pears # and bananas\n"; |
strip_comments "apples, pears # and bananas\n"; |
||
strip_comments "apples, pears ; and bananas\n"; |
strip_comments "apples, pears ; and bananas\n"; |
||
;;</ |
;;</syntaxhighlight> |
||
or with an imperative style: |
or with an imperative style: |
||
< |
<syntaxhighlight lang="ocaml">let strip_comments = |
||
let print = ref true in |
let print = ref true in |
||
String.iter (function |
String.iter (function |
||
| ';' | '#' -> print := false |
| ';' | '#' -> print := false |
||
| '\n' -> print_char '\n'; print := true |
| '\n' -> print_char '\n'; print := true |
||
| c -> if !print then print_char c)</ |
| c -> if !print then print_char c)</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: stripComments(s, markers) |
||
| firstMarker | |
| firstMarker | |
||
markers map(#[ s indexOf ]) reduce(#min) ->firstMarker |
markers map(#[ s indexOf ]) reduce(#min) ->firstMarker |
||
s firstMarker ifNotNull: [ left(firstMarker 1 - ) ] strip ;</ |
s firstMarker ifNotNull: [ left(firstMarker 1 - ) ] strip ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,228: | Line 1,513: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">while (<>) |
||
{ |
{ |
||
s/[#;].*$//s; # remove comment |
s/[#;].*$//s; # remove comment |
||
Line 1,234: | Line 1,519: | ||
s/\s+$//; # remove trailing whitespace |
s/\s+$//; # remove trailing whitespace |
||
print |
print |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>$*IN.slurp.subst(/ \h* <[ # ; ]> \N* /, '', :g).print</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Added a couple of things that can go wrong with something nowhere near sufficiently smart or for that matter language-specific enough.<br> |
Added a couple of things that can go wrong with something nowhere near sufficiently smart or for that matter language-specific enough.<br> |
||
A line comment inside a block comment (eg " /* left -- right */") could also be very dodgy, and perhaps vice versa. |
A line comment inside a block comment (eg " /* left -- right */") could also be very dodgy, and perhaps vice versa. |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<lang Phix>function strip_comments(string s, sequence comments={"#",";"}) |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
for i=1 to length(comments) do |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">comments</span><span style="color: #0000FF;">={</span><span style="color: #008000;">"#"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">";"</span><span style="color: #0000FF;">})</span> |
|||
integer k = match(comments[i],s) |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">comments</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
if k then |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">comments</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
s = s[1..k-1] |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span> |
|||
s = trim_tail(s) |
|||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> |
|||
end if |
|||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
end for |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
return s |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end function |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
?strip_comments("apples, pears # and bananas") |
|||
?strip_comments("apples, pears ; and bananas") |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"apples, pears # and bananas"</span><span style="color: #0000FF;">)</span> |
|||
?strip_comments("apples, pears and bananas ") |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"apples, pears ; and bananas"</span><span style="color: #0000FF;">)</span> |
|||
?strip_comments(" WS_CAPTION = #00C00000, -- = WS_BORDER+WS_DLGFRAME") |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"apples, pears and bananas "</span><span style="color: #0000FF;">)</span> |
|||
?strip_comments(" WS_CAPTION = #00C00000, -- = WS_BORDER+WS_DLGFRAME",{"--"}) |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" WS_CAPTION = #00C00000, -- = WS_BORDER+WS_DLGFRAME"</span><span style="color: #0000FF;">)</span> |
|||
?strip_comments(" title = \"--Title--\"",{"--"})</lang> |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" WS_CAPTION = #00C00000, -- = WS_BORDER+WS_DLGFRAME"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"--"</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">strip_comments</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" title = \"--Title--\""</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"--"</span><span style="color: #0000FF;">})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,270: | Line 1,555: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(for Str '("apples, pears # and bananas" "apples, pears ; and bananas") |
||
(prinl (car (split (chop Str) "#" ";"))) )</ |
(prinl (car (split (chop Str) "#" ";"))) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,279: | Line 1,564: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">k = search(text, '#;'); |
||
if k = 0 then put skip list (text); |
if k = 0 then put skip list (text); |
||
else put skip list (substr(text, 1, k-1));</ |
else put skip list (substr(text, 1, k-1));</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
This version is implemented as a state automata to strip multiple lines of comments. |
This version is implemented as a state automata to strip multiple lines of comments. |
||
< |
<syntaxhighlight lang="prolog">stripcomment(A,B) :- stripcomment(A,B,a). |
||
stripcomment([A|AL],[A|BL],a) :- \+ A=0';, \+ A=0'# , \+ A=10, \+ A=13 , stripcomment(AL,BL,a). |
stripcomment([A|AL],[A|BL],a) :- \+ A=0';, \+ A=0'# , \+ A=10, \+ A=13 , stripcomment(AL,BL,a). |
||
stripcomment([A|AL], BL ,a) :- ( A=0';; A=0'#), \+ A=10, \+ A=13 , stripcomment(AL,BL,b). |
stripcomment([A|AL], BL ,a) :- ( A=0';; A=0'#), \+ A=10, \+ A=13 , stripcomment(AL,BL,b). |
||
Line 1,296: | Line 1,581: | ||
apples, pears # and bananas", |
apples, pears # and bananas", |
||
stripcomment(In,Out), |
stripcomment(In,Out), |
||
format("~s~n",[Out]).</ |
format("~s~n",[Out]).</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,304: | Line 1,589: | ||
</pre> |
</pre> |
||
This version uses prolog's pattern matching with two append/3 to strip 1 line. |
This version uses prolog's pattern matching with two append/3 to strip 1 line. |
||
< |
<syntaxhighlight lang="prolog">strip_1comment(A,D) :- ((S1=0'#;S1=0';),append(B,[S1|C],A)), \+ ((S2=0'#;S2=0';),append(_X,[S2|_Y],B)) -> B=D; A=D.</syntaxhighlight> |
||
At the query console: |
At the query console: |
||
<pre> |
<pre> |
||
Line 1,316: | Line 1,601: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.s Strip_comments(Str$) |
||
Protected result$=Str$, l, l1, l2 |
Protected result$=Str$, l, l1, l2 |
||
l1 =FindString(Str$,"#",1) |
l1 =FindString(Str$,"#",1) |
||
Line 1,332: | Line 1,617: | ||
EndIf |
EndIf |
||
ProcedureReturn result$ |
ProcedureReturn result$ |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
Implementation |
Implementation |
||
< |
<syntaxhighlight lang="purebasic">#instring1 ="apples, pears # and bananas" |
||
#instring2 ="apples, pears ; and bananas" |
#instring2 ="apples, pears ; and bananas" |
||
PrintN(Strip_comments(#instring1)) |
PrintN(Strip_comments(#instring1)) |
||
PrintN(Strip_comments(#instring2))</ |
PrintN(Strip_comments(#instring2))</syntaxhighlight> |
||
Output:<pre> |
Output:<pre> |
||
apples, pears |
apples, pears |
||
Line 1,346: | Line 1,631: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Procedural=== |
===Procedural=== |
||
< |
<syntaxhighlight lang="python">def remove_comments(line, sep): |
||
for s in sep: |
for s in sep: |
||
i = line.find(s) |
i = line.find(s) |
||
Line 1,356: | Line 1,641: | ||
print remove_comments('apples ; pears # and bananas', ';#') |
print remove_comments('apples ; pears # and bananas', ';#') |
||
print remove_comments('apples ; pears # and bananas', '!') |
print remove_comments('apples ; pears # and bananas', '!') |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Regular expressions=== |
===Regular expressions=== |
||
You could also use a regular expression |
You could also use a regular expression |
||
< |
<syntaxhighlight lang="python">import re |
||
m = re.match(r'^([^#]*)#(.*)$', line) |
m = re.match(r'^([^#]*)#(.*)$', line) |
||
if m: # The line contains a hash / comment |
if m: # The line contains a hash / comment |
||
line = m.group(1) |
line = m.group(1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Functional=== |
===Functional=== |
||
Line 1,372: | Line 1,657: | ||
which is defined over strings as well as lists. |
which is defined over strings as well as lists. |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Comments stripped with itertools.takewhile''' |
||
from itertools import takewhile |
from itertools import takewhile |
||
Line 1,400: | Line 1,685: | ||
''' |
''' |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>apples, pears |
<pre>apples, pears |
||
apples, pears</pre> |
apples, pears</pre> |
||
=={{header|QBasic}}== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="qbasic">SUB stripComment (s$, commentMarkers$) |
|||
IF s$ = "" THEN RETURN |
|||
i = INSTR(s$, commentMarkers$) |
|||
IF i > 0 THEN |
|||
s$ = LEFT$(s$, i - 1) |
|||
s$ = LTRIM$((RTRIM$(s$))) '' removes both leading and trailing whitespace |
|||
END IF |
|||
END SUB |
|||
DIM s$(1 TO 4) |
|||
s$(1) = "apples, pears # and bananas" |
|||
s$(2) = "apples, pears ; and bananas" |
|||
s$(3) = "# this is a comment" |
|||
s$(4) = " # this is a comment with leading whitespace" |
|||
FOR i = 1 TO 4 |
|||
CALL stripComment(s$(i), "#;") |
|||
PRINT s$(i), " => Length ="; LEN(s$(i)) |
|||
NEXT i</syntaxhighlight> |
|||
{{out}} |
|||
<pre>apples, pears # and bananas => Length = 27 |
|||
apples, pears ; and bananas => Length = 27 |
|||
# this is a comment => Length = 19 |
|||
# this is a comment with leading whitespace => Length = 45</pre> |
|||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="Quackery"> [ char ; over find split drop |
|||
char # over find split drop ] is strip ( $ --> $ ) |
|||
( or, to pander to the debacle by trimming trailing whitespace: |
|||
[ char ; over find split drop |
|||
char # over find split drop |
|||
reverse trim reverse ] is strip ( $ --> $ ) |
|||
( or, to pander to the debacle by trimming leading and trailing whitespace: |
|||
[ char ; over find split drop |
|||
char # over find split drop |
|||
trim reverse trim reverse ] is strip ( $ --> $ ) |
|||
$ "apples, pears # and bananas" strip echo$ cr |
|||
$ "apples, pears ; and bananas" strip echo$ cr |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>apples, pears |
|||
apples, pears |
|||
</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
This is most cleanly accomplished using the <code>stringr</code> package. |
This is most cleanly accomplished using the <code>stringr</code> package. |
||
< |
<syntaxhighlight lang="r">strip_comments <- function(str) |
||
{ |
{ |
||
if(!require(stringr)) stop("you need to install the stringr package") |
if(!require(stringr)) stop("you need to install the stringr package") |
||
str_trim(str_split_fixed(str, "#|;", 2)[, 1]) |
str_trim(str_split_fixed(str, "#|;", 2)[, 1]) |
||
}</ |
}</syntaxhighlight> |
||
Example usage: |
Example usage: |
||
< |
<syntaxhighlight lang="r">x <-c( |
||
"apples, pears # and bananas", # the requested hash test |
"apples, pears # and bananas", # the requested hash test |
||
"apples, pears ; and bananas", # the requested semicolon test |
"apples, pears ; and bananas", # the requested semicolon test |
||
Line 1,422: | Line 1,763: | ||
" apples, pears # and bananas" # with preceding spaces |
" apples, pears # and bananas" # with preceding spaces |
||
) |
) |
||
strip_comments(x)</ |
strip_comments(x)</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang at-exp racket |
#lang at-exp racket |
||
Line 1,447: | Line 1,788: | ||
(strip-comments2 text) ; -> "apples, pears\napples, pears" |
(strip-comments2 text) ; -> "apples, pears\napples, pears" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<syntaxhighlight lang="raku" line>$*IN.slurp.subst(/ \h* <[ # ; ]> \N* /, '', :g).print</syntaxhighlight> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
Red has an embedded parse engine called Parse. |
Red has an embedded parse engine called Parse. |
||
For more info on the Parse dialect. http://www.red-lang.org/2013/11/041-introducing-parse.html. |
For more info on the Parse dialect. http://www.red-lang.org/2013/11/041-introducing-parse.html. |
||
<syntaxhighlight lang="red"> |
|||
<lang Red> |
|||
>> parse s: "apples, pears ; and bananas" [to [any space ";"] remove thru end] |
>> parse s: "apples, pears ; and bananas" [to [any space ";"] remove thru end] |
||
== true |
== true |
||
>> s |
>> s |
||
== "apples, pears" |
== "apples, pears" |
||
</syntaxhighlight> |
|||
</lang> |
|||
But you can also use simple series operations to find where something occurs, clear from that position, and trim leading and trailing spaces. |
But you can also use simple series operations to find where something occurs, clear from that position, and trim leading and trailing spaces. |
||
<syntaxhighlight lang="red"> |
|||
<lang Red> |
|||
s: "apples, pears ; and bananas" |
s: "apples, pears ; and bananas" |
||
dlms: charset "#;" |
dlms: charset "#;" |
||
Line 1,471: | Line 1,816: | ||
trim head clear find s dlms |
trim head clear find s dlms |
||
== "apples, pears" |
== "apples, pears" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,482: | Line 1,827: | ||
<br>The fourth subroutine is similar to the third version but more idiomatic. |
<br>The fourth subroutine is similar to the third version but more idiomatic. |
||
<br><br>All four subroutines trim leading ''and'' trailing blanks after stripping the "comments". |
<br><br>All four subroutines trim leading ''and'' trailing blanks after stripping the "comments". |
||
< |
<syntaxhighlight lang="rexx">/*REXX program strips a string delineated by a hash (#) or a semicolon (;). */ |
||
old1= ' apples, pears # and bananas' ; say ' old ───►'old1"◄───" |
old1= ' apples, pears # and bananas' ; say ' old ───►'old1"◄───" |
||
new1= stripCom1(old1) ; say ' 1st version new ───►'new1"◄───" |
new1= stripCom1(old1) ; say ' 1st version new ───►'new1"◄───" |
||
Line 1,522: | Line 1,867: | ||
if p\==0 then x=left(x,p-1) /*shorten the X string by one character*/ |
if p\==0 then x=left(x,p-1) /*shorten the X string by one character*/ |
||
end /*k*/ /* [↑] If p==0, then char wasn't found*/ |
end /*k*/ /* [↑] If p==0, then char wasn't found*/ |
||
return strip(x) /*return the stripped shortened string.*/</ |
return strip(x) /*return the stripped shortened string.*/</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 1,539: | Line 1,884: | ||
===version 2=== |
===version 2=== |
||
< |
<syntaxhighlight lang="rexx">Call stripd ' apples, pears # and bananas' |
||
Call stripd ' apples, pears and bananas' |
Call stripd ' apples, pears and bananas' |
||
Exit |
Exit |
||
Line 1,552: | Line 1,897: | ||
Say '>'old'<' |
Say '>'old'<' |
||
Say '>'new'<' |
Say '>'new'<' |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aList = 'apples, pears # and bananas' |
aList = 'apples, pears # and bananas' |
||
see aList + nl |
see aList + nl |
||
Line 1,569: | Line 1,914: | ||
if nr > 0 cList = substr(bList,1,nr-1) ok |
if nr > 0 cList = substr(bList,1,nr-1) ok |
||
return cList |
return cList |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|RPL}}== |
|||
In addition to the input string, a second argument defines if whitespaces must be removed (mode=1) or not (mode=0). |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ |
|||
'''IF''' DUP 1 1 SUB " " == '''THEN''' |
|||
2 OVER SIZE SUB '''TRIMF END''' |
|||
'''IF''' DUP DUP SIZE DUP SUB " " == '''THEN''' |
|||
1 OVER SIZE 1 - SUB '''TRIMF END''' |
|||
≫ ‘'''TRIMF'''’ STO |
|||
≪ → string charset |
|||
≪ 0 1 charset SIZE '''FOR''' j |
|||
'''IF''' string charset j DUP SUB POS |
|||
'''THEN''' LAST SWAP DROP charset SIZE 'j' STO '''END''' |
|||
'''NEXT''' |
|||
≫ ≫ ''''STPOS'''' STO |
|||
≪ SWAP |
|||
'''IF''' DUP "#;" '''STPOS THEN''' |
|||
1 LAST 1 - SUB '''IF''' SWAP '''THEN TRIMF END''' |
|||
'''ELSE''' SWAP DROP '''END''' |
|||
≫ ''''NOCOM'''' STO |
|||
| |
|||
'''TRIMF''' ''( "string" -- "trimmed" ) '' |
|||
if flag 1 set and 1st char of string is a space |
|||
then recursively process tail(string) |
|||
if flag 1 set and last char of string is a space |
|||
then recursively process head(string) |
|||
'''STPOS''' ''( "string" "char_set" -- position ) '' |
|||
position = 0, for each character in set |
|||
if char in string |
|||
update position, request loop exit |
|||
'''NOCOM''' ''( "string" mode -- "no_comment" )'' |
|||
if there is a comment |
|||
remove it from string, and spaces too if required |
|||
otherwise clean stack |
|||
|} |
|||
{{in}} |
|||
<pre> |
|||
" apple, pears # and bananas" 0 NOCOM |
|||
" apple, pears # and bananas" 1 NOCOM |
|||
" apple, pears ; and bananas" 1 NOCOM |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
3: " apple, pears " |
|||
2: "apple, pears" |
|||
1: "apple, pears" |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class String |
||
def strip_comment( markers = ['#',';'] ) |
def strip_comment( markers = ['#',';'] ) |
||
re = Regexp.union( markers ) # construct a regular expression which will match any of the markers |
re = Regexp.union( markers ) # construct a regular expression which will match any of the markers |
||
Line 1,590: | Line 1,996: | ||
p str.strip_comment('and') |
p str.strip_comment('and') |
||
p " \t \n ;".strip_comment |
p " \t \n ;".strip_comment |
||
p "".strip_comment</ |
p "".strip_comment</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,602: | Line 2,008: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn strip_comment<'a>(input: &'a str, markers: &[char]) -> &'a str { |
||
input |
input |
||
.find(markers) |
.find(markers) |
||
Line 1,614: | Line 2,020: | ||
println!("{:?}", strip_comment("apples, pears ; and bananas", &['#', ';'])); |
println!("{:?}", strip_comment("apples, pears ; and bananas", &['#', ';'])); |
||
println!("{:?}", strip_comment("apples, pears and bananas ", &['#', ';'])); |
println!("{:?}", strip_comment("apples, pears and bananas ", &['#', ';'])); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,623: | Line 2,029: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object StripComments { |
||
def stripComments1(s:String, markers:String =";#")=s takeWhile (!markers.contains(_)) trim |
def stripComments1(s:String, markers:String =";#")=s takeWhile (!markers.contains(_)) trim |
||
Line 1,645: | Line 2,051: | ||
print("apples, pears ; and bananas") |
print("apples, pears ; and bananas") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>'apples, pears # and bananas' => |
<pre>'apples, pears # and bananas' => |
||
Line 1,656: | Line 2,062: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Guile}} |
{{works with|Guile}} |
||
< |
<syntaxhighlight lang="scheme">(use-modules (ice-9 regex)) |
||
(define (strip-comments s) |
(define (strip-comments s) |
||
Line 1,663: | Line 2,069: | ||
(display (strip-comments "apples, pears # and bananas"))(newline) |
(display (strip-comments "apples, pears # and bananas"))(newline) |
||
(display (strip-comments "apples, pears ; and bananas"))(newline)</ |
(display (strip-comments "apples, pears ; and bananas"))(newline)</syntaxhighlight> |
||
Output:<pre>apples, pears |
Output:<pre>apples, pears |
||
apples, pears</pre> |
apples, pears</pre> |
||
=={{header|sed}}== |
=={{header|sed}}== |
||
< |
<syntaxhighlight lang="bash">#!/bin/sh |
||
# Strip comments |
# Strip comments |
||
echo "$1" | sed 's/ *[#;].*$//g' | sed 's/^ *//'</ |
echo "$1" | sed 's/ *[#;].*$//g' | sed 's/^ *//'</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func string: stripComment (in string: line) is func |
const func string: stripComment (in string: line) is func |
||
Line 1,703: | Line 2,109: | ||
writeln(stripComment(line)); |
writeln(stripComment(line)); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,715: | Line 2,121: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func strip_comment(s) { |
||
(s - %r'[#;].*').strip; |
(s - %r'[#;].*').strip; |
||
} |
} |
||
Line 1,723: | Line 2,129: | ||
" apples, pears "].each { |s| |
" apples, pears "].each { |s| |
||
say strip_comment(s).dump; |
say strip_comment(s).dump; |
||
};</ |
};</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,730: | Line 2,136: | ||
"apples, pears" |
"apples, pears" |
||
</pre> |
</pre> |
||
=={{header|SparForte}}== |
|||
As a structured script. |
|||
<syntaxhighlight lang="ada">#!/usr/local/bin/spar |
|||
pragma annotate( summary, "stripcomment" ) |
|||
@( description, "The task is to remove text that follow any of a set of" ) |
|||
@( description, "comment markers, (in these examples either a hash or a" ) |
|||
@( description, "semicolon) from a string or input line." ) |
|||
@( see_also, "http://rosettacode.org/wiki/Strip_comments_from_a_string" ) |
|||
@( author, "Ken O. Burtch" ); |
|||
pragma license( unrestricted ); |
|||
pragma restriction( no_external_commands ); |
|||
procedure stripcomment is |
|||
line : constant string := get_line; |
|||
eol : natural := 0; |
|||
ch : character; |
|||
begin |
|||
for i in 1..strings.length( line ) loop |
|||
ch := strings.element( line, i ); |
|||
exit when ch = '#' or ch = ';'; |
|||
eol := i; |
|||
end loop; |
|||
if eol > 0 then |
|||
? strings.trim( strings.slice( line, 1, eol ), trim_end.both ); |
|||
end if; |
|||
end stripcomment;</syntaxhighlight> |
|||
=={{header|Standard ML}}== |
|||
Strips comments and trailing spaces. |
|||
<syntaxhighlight lang="sml">val stripComment = |
|||
let |
|||
val notMarker = fn #"#" => false | #";" => false | _ => true |
|||
open Substring |
|||
in |
|||
string o dropr Char.isSpace o takel notMarker o full |
|||
end</syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc stripLineComments {inputString {commentChars ";#"}} { |
||
# Switch the RE engine into line-respecting mode instead of the default whole-string mode |
# Switch the RE engine into line-respecting mode instead of the default whole-string mode |
||
regsub -all -line "\[$commentChars\].*$" $inputString "" commentStripped |
regsub -all -line "\[$commentChars\].*$" $inputString "" commentStripped |
||
# Now strip the whitespace |
# Now strip the whitespace |
||
regsub -all -line {^[ \t\r]*(.*\S)?[ \t\r]*$} $commentStripped {\1} |
regsub -all -line {^[ \t\r]*(.*\S)?[ \t\r]*$} $commentStripped {\1} |
||
}</ |
}</syntaxhighlight> |
||
Demonstration: |
Demonstration: |
||
< |
<syntaxhighlight lang="tcl"># Multi-line string constant |
||
set input "apples, pears # and bananas |
set input "apples, pears # and bananas |
||
apples, pears ; and bananas" |
apples, pears ; and bananas" |
||
# Do the stripping |
# Do the stripping |
||
puts [stripLineComments $input]</ |
puts [stripLineComments $input]</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,750: | Line 2,194: | ||
</pre> |
</pre> |
||
The above code has one issue though; it's notion of a set of characters is very much that of the RE engine. That's possibly desirable, but to handle ''any'' sequence of characters as a set of separators requires a bit more cleverness. |
The above code has one issue though; it's notion of a set of characters is very much that of the RE engine. That's possibly desirable, but to handle ''any'' sequence of characters as a set of separators requires a bit more cleverness. |
||
< |
<syntaxhighlight lang="tcl">proc stripLineComments {inputString {commentChars ";#"}} { |
||
# Convert the character set into a transformation |
# Convert the character set into a transformation |
||
foreach c [split $commentChars ""] {lappend map $c "\uFFFF"}; # *very* rare character! |
foreach c [split $commentChars ""] {lappend map $c "\uFFFF"}; # *very* rare character! |
||
Line 1,757: | Line 2,201: | ||
# Now strip the whitespace |
# Now strip the whitespace |
||
regsub -all -line {^[ \t\r]*(.*\S)?[ \t\r]*$} $commentStripped {\1} |
regsub -all -line {^[ \t\r]*(.*\S)?[ \t\r]*$} $commentStripped {\1} |
||
}</ |
}</syntaxhighlight> |
||
Output in the example is the same as above. |
Output in the example is the same as above. |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript">$$ MODE TUSCRIPT |
||
strngcomment=* |
strngcomment=* |
||
DATA apples, pears # and bananas |
DATA apples, pears # and bananas |
||
Line 1,771: | Line 2,215: | ||
x=SPLIT (s,comment_char,string,comment) |
x=SPLIT (s,comment_char,string,comment) |
||
PRINT string |
PRINT string |
||
ENDLOOP</ |
ENDLOOP</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,782: | Line 2,226: | ||
{{works with|pdksh}} |
{{works with|pdksh}} |
||
Adapted from the Advanced Bash-Scripting Guide, section 10.1 [http://tldp.org/LDP/abs/html/string-manipulation.html Manipulating Strings]. |
Adapted from the Advanced Bash-Scripting Guide, section 10.1 [http://tldp.org/LDP/abs/html/string-manipulation.html Manipulating Strings]. |
||
< |
<syntaxhighlight lang="bash">bash$ a='apples, pears ; and bananas' |
||
bash$ b='apples, pears # and bananas' |
bash$ b='apples, pears # and bananas' |
||
bash$ echo ${a%%;*} |
bash$ echo ${a%%;*} |
||
Line 1,788: | Line 2,232: | ||
bash$ echo ${b%%#*} |
bash$ echo ${b%%#*} |
||
apples, pears |
apples, pears |
||
bash$</ |
bash$</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function strip_comments(s,char) |
Function strip_comments(s,char) |
||
If InStr(1,s,char) > 0 Then |
If InStr(1,s,char) > 0 Then |
||
Line 1,803: | Line 2,247: | ||
WScript.StdOut.WriteLine strip_comments("apples, pears # and bananas","#") |
WScript.StdOut.WriteLine strip_comments("apples, pears # and bananas","#") |
||
WScript.StdOut.WriteLine strip_comments("apples, pears ; and bananas",";") |
WScript.StdOut.WriteLine strip_comments("apples, pears ; and bananas",";") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|V (Vlang)}}== |
|||
<syntaxhighlight lang="v (vlang)">const comment_chars = "#;" |
|||
fn main() { |
|||
s := [ |
|||
"apples, pears # and bananas", |
|||
"apples, pears ; and bananas", |
|||
"no bananas" |
|||
] |
|||
for element in s { |
|||
println('source: $element') |
|||
println('stripped: ' + strip_comment(element)) |
|||
} |
|||
} |
|||
fn strip_comment(source string) string { |
|||
if source.index_any(comment_chars) >= 0 { |
|||
return source.substr(0, source.index_any(comment_chars)) |
|||
} |
|||
return source |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
source: apples, pears # and bananas |
|||
stripped: apples, pears |
|||
source: apples, pears ; and bananas |
|||
stripped: apples, pears |
|||
source: no bananas |
|||
stripped: no bananas |
|||
</pre> |
|||
=={{header|Wren}}== |
|||
This is based on what the post 29th March, 2011 requirements appear to be. |
|||
<syntaxhighlight lang="wren">var markers = ["#", ";"] |
|||
var stripComments = Fn.new { |s| |
|||
for (marker in markers) { |
|||
var t = s.split(marker) |
|||
if (t.count > 1) return t[0].trim() |
|||
} |
|||
return s.trim() |
|||
} |
|||
var strings = [ |
|||
" apples, pears # and bananas", |
|||
" apples, pears ; and bananas", |
|||
" apples, pears \t " |
|||
] |
|||
for (s in strings) { |
|||
var t = stripComments.call(s) |
|||
System.print("'%(s)' -> '%(t)'") |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
' apples, pears # and bananas' -> 'apples, pears' |
|||
' apples, pears ; and bananas' -> 'apples, pears' |
|||
' apples, pears ' -> 'apples, pears' |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang "XPL0">string 0; \use zero-terminated strings |
|||
func StripComment(Str); \Remove characters after marker and remove |
|||
char Str; int I; \ whitespace at beginning and end of string |
|||
[I:= 0; |
|||
loop case Str(I) of |
|||
^#, ^;, 0: quit |
|||
other I:= I+1; |
|||
while I>0 and Str(I-1)<=$20 do I:= I-1; |
|||
Str(I):= 0; |
|||
while Str(0)<=$20 do Str:= Str+1; |
|||
return Str; |
|||
]; |
|||
int Strings, I; |
|||
[Strings:= [ |
|||
" apples, pears # and bananas", |
|||
" apples, pears ; and bananas ", |
|||
" apples, pears "]; |
|||
for I:= 0 to 3-1 do |
|||
[ChOut(0, ^"); |
|||
Text(0, StripComment(Strings(I))); |
|||
ChOut(0, ^"); |
|||
CrLf(0); |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
"apples, pears" |
|||
"apples, pears" |
|||
"apples, pears" |
|||
</pre> |
|||
=={{header|XProfan}}== |
|||
Delimiter in string "#" or ";" will be ignored. Good for INI files. |
|||
<syntaxhighlight lang="xprofan"> |
|||
Proc Min |
|||
Declare int PC, i, float e, t |
|||
PC = %PCount |
|||
e = @!(1) |
|||
If PC > 1 |
|||
For i, 2, PC |
|||
t = @!(i) |
|||
e = if( (e == 0.0) and (t == 0.0), -(-e - t), if(t < e, t, e) ) |
|||
EndFor |
|||
EndIf |
|||
Return e |
|||
EndProc |
|||
Proc Odd |
|||
Parameters int n |
|||
Return TestBit(n,0) |
|||
EndProc |
|||
Proc strip_comments |
|||
Parameters string s, delim |
|||
Declare int posi[] |
|||
Declare int i, min_p, p |
|||
min_p = $7FFFFFFF |
|||
For i, 1, Len(delim) |
|||
posi[ i ] = InStr( mid$(delim,i,1), s ) |
|||
Case posi[ i ] > 0 : min_p = Min( posi[ i ], min_p ) |
|||
EndFor |
|||
posi[ 0 ] = InStr( chr$(34), s ) |
|||
// if there is a string delimiter on the left side... |
|||
If (posi[0] > 0) and (posi[0] < min_p) |
|||
// ...and counting of delimiter is odd, then the sign is part of a string |
|||
If Odd( Len( Left$(s,min_p) ) - Len( translate$( Left$(s,min_p), Chr$(34), "" )) ) |
|||
p = posi[ 0 ] + 1 |
|||
min_p = $7FFFFFFF |
|||
Repeat |
|||
// closing quote |
|||
posi[ 0 ] = InStr( chr$(34), s, p ) |
|||
'Case posi[0] > 0 : posi[0] = posi[0] + p |
|||
p = posi[ 0 ] + 1 |
|||
// find new positions after that |
|||
For i, 1, Len(delim) |
|||
posi[ i ] = InStr( mid$(delim,i,1), s, p ) |
|||
Case posi[ i ] > 0 : min_p = Min( posi[ i ], min_p ) |
|||
EndFor |
|||
posi[ 0 ] = InStr( chr$(34), s, p ) |
|||
// if there is a string delimiter on the left side... |
|||
If (posi[0] > 0) and (posi[0] < min_p) |
|||
// ...and counting of delimiter is odd, then the sign is part of a string |
|||
If Odd( Len( Left$(s,min_p) ) - Len( translate$( Left$(s,min_p), Chr$(34), "" )) ) |
|||
p = posi[ 0 ] + 1 |
|||
min_p = $7FFFFFFF |
|||
// and again.... |
|||
CONTINUE |
|||
EndIf |
|||
EndIf |
|||
BREAK |
|||
Until min_p = 0 |
|||
EndIf |
|||
EndIf |
|||
Return Trim$( Left$( s, min_p - 1 ) ) |
|||
EndProc |
|||
cls |
|||
declare string s, t |
|||
s = " apples, pears # and bananas" |
|||
t = strip_comments( s, "#;" ) |
|||
Print s + "|\n-> [" + t + "]\n" |
|||
s = " apples, pears ; and bananas" |
|||
t = strip_comments( s, "#;" ) |
|||
Print s + "|\n-> [" + t + "]\n" |
|||
s = " apples, pears \t " |
|||
t = strip_comments( s, "#;" ) |
|||
Print s + "|\n-> [" + t + "]\n" |
|||
s = " " + chr$(34) + " #oh, my god " + chr$(34) + " apples, pears # and bananas" |
|||
t = strip_comments( s, "#;" ) |
|||
Print s + "|\n-> [" + t + "]\n" |
|||
waitkey |
|||
end</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
[apples, pears] |
|||
[apples, pears] |
|||
[apples, pears] |
|||
[" #oh, my god " apples, pears] |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn strip(text,c){ // if c in text, remove it and following text |
||
if (Void!=(n:=text.find(c))) text=text[0,n]; |
if (Void!=(n:=text.find(c))) text=text[0,n]; |
||
text.strip() // remove leading and trailing white space |
text.strip() // remove leading and trailing white space |
||
Line 1,813: | Line 2,450: | ||
foreach c in (vm.arglist[1,*]){ text=strip(text,c) } |
foreach c in (vm.arglist[1,*]){ text=strip(text,c) } |
||
text |
text |
||
}</ |
}</syntaxhighlight> |
||
Or, if you want the all-in-one stripper: |
Or, if you want the all-in-one stripper: |
||
< |
<syntaxhighlight lang="zkl">fcn stripper(text,a,b,c,etc){ |
||
vm.arglist[1,*].reduce('wrap(text,c){ |
vm.arglist[1,*].reduce('wrap(text,c){ |
||
if (Void!=(n:=text.find(c))) text[0,n] else text |
if (Void!=(n:=text.find(c))) text[0,n] else text |
||
},text) |
},text) |
||
.strip() |
.strip() |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">String(">", strip(" apples, pears # and bananas","#"), "<").println(); |
||
String(">", stripper(" apples, pears ; and # bananas","#",";"), "<").println();</ |
String(">", stripper(" apples, pears ; and # bananas","#",";"), "<").println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 16:24, 10 February 2024
You are encouraged to solve this task according to the task description, using any language you may know.
The task is to remove text that follow any of a set of comment markers, (in these examples either a hash or a semicolon) from a string or input line.
Whitespace debacle: There is some confusion about whether to remove any whitespace from the input line.
As of 2 September 2011, at least 8 languages (C, C++, Java, Perl, Python, Ruby, sed, UNIX Shell) were incorrect, out of 36 total languages, because they did not trim whitespace by 29 March 2011 rules. Some other languages might be incorrect for the same reason.
Please discuss this issue at Talk:Strip comments from a string.
- From 29 March 2011, this task required that: "The comment marker and any whitespace at the beginning or ends of the resultant line should be removed. A line without comments should be trimmed of any leading or trailing whitespace before being produced as a result." The task had 28 languages, which did not all meet this new requirement.
- From 28 March 2011, this task required that: "Whitespace before the comment marker should be removed."
- From 30 October 2010, this task did not specify whether or not to remove whitespace.
The following examples will be truncated to either "apples, pears " or "apples, pears".
(This example has flipped between "apples, pears " and "apples, pears" in the past.)
apples, pears # and bananas apples, pears ; and bananas
- Metrics
- Counting
- Word frequency
- Letter frequency
- Jewels and stones
- I before E except after C
- Bioinformatics/base count
- Count occurrences of a substring
- Count how many vowels and consonants occur in a string
- Remove/replace
- XXXX redacted
- Conjugate a Latin verb
- Remove vowels from a string
- String interpolation (included)
- Strip block comments
- Strip comments from a string
- Strip a set of characters from a string
- Strip whitespace from a string -- top and tail
- Strip control codes and extended characters from a string
- Anagrams/Derangements/shuffling
- Word wheel
- ABC problem
- Sattolo cycle
- Knuth shuffle
- Ordered words
- Superpermutation minimisation
- Textonyms (using a phone text pad)
- Anagrams
- Anagrams/Deranged anagrams
- Permutations/Derangements
- Find/Search/Determine
- ABC words
- Odd words
- Word ladder
- Semordnilap
- Word search
- Wordiff (game)
- String matching
- Tea cup rim text
- Alternade words
- Changeable words
- State name puzzle
- String comparison
- Unique characters
- Unique characters in each string
- Extract file extension
- Levenshtein distance
- Palindrome detection
- Common list elements
- Longest common suffix
- Longest common prefix
- Compare a list of strings
- Longest common substring
- Find common directory path
- Words from neighbour ones
- Change e letters to i in words
- Non-continuous subsequences
- Longest common subsequence
- Longest palindromic substrings
- Longest increasing subsequence
- Words containing "the" substring
- Sum of the digits of n is substring of n
- Determine if a string is numeric
- Determine if a string is collapsible
- Determine if a string is squeezable
- Determine if a string has all unique characters
- Determine if a string has all the same characters
- Longest substrings without repeating characters
- Find words which contains all the vowels
- Find words which contains most consonants
- Find words which contains more than 3 vowels
- Find words which first and last three letters are equals
- Find words which odd letters are consonants and even letters are vowels or vice_versa
- Formatting
- Substring
- Rep-string
- Word wrap
- String case
- Align columns
- Literals/String
- Repeat a string
- Brace expansion
- Brace expansion using ranges
- Reverse a string
- Phrase reversals
- Comma quibbling
- Special characters
- String concatenation
- Substring/Top and tail
- Commatizing numbers
- Reverse words in a string
- Suffixation of decimal numbers
- Long literals, with continuations
- Numerical and alphabetical suffixes
- Abbreviations, easy
- Abbreviations, simple
- Abbreviations, automatic
- Song lyrics/poems/Mad Libs/phrases
- Mad Libs
- Magic 8-ball
- 99 Bottles of Beer
- The Name Game (a song)
- The Old lady swallowed a fly
- The Twelve Days of Christmas
- Tokenize
- Text between
- Tokenize a string
- Word break problem
- Tokenize a string with escaping
- Split a character string based on change of character
- Sequences
11l
F remove_comments(line, sep)
V? p = line.find(sep)
I p != N
R line[0.<p].rtrim(‘ ’)
R line
print(remove_comments(‘apples ; pears # and bananas’, (‘;’, ‘#’)))
print(remove_comments(‘apples ; pears # and bananas’, ‘#’))
print(remove_comments(‘apples ; pears # and bananas’, ‘!’))
- Output:
apples apples ; pears apples ; pears # and bananas
68000 Assembly
StripComments:
;prints a string but stops at the comment character
;INPUT: D7 = comment character(s) of choice
;A0 = source address of string
;up to four can be used, each takes up a different 8 bits of the register
;to omit an argument, leave its bits as zero.
.loop:
MOVE.B (A0)+,D0
CMP.B #0,D0 ;check for null terminator
beq .done
CMP.B D7,D0 ;check the first comment char
beq .done
ROR.L #8,D7
CMP.B D7,D0 ;check the second comment char
beq .done
ROR.L #8,D7
CMP.B D7,D0 ;check the third comment char
beq .done
ROR.L #8,D7
CMP.B D7,D0 ;check the fourth comment char
beq .done
ROR.L #8,D7
CMP.B #' ',D0
BNE dontCheckNext
MOVE.B (A0),D1 ;look ahead one character, if that character is a comment char or null terminator, stop here
CMP.B #0,D1
beq .done
CMP.B D7,D1
beq .done
ROR.L #8,D7
CMP.B D7,D1
beq .done
ROR.L #8,D7
CMP.B D7,D1
beq .done
ROR.L #8,D7
CMP.B D7,D1
beq .done
ROR.L #8,D7
dontCheckNext:
jsr PrintChar
bra .loop
.done:
rts
TestString:
dc.b "apples ; pears # and bananas",0
Action!
PROC Strip(CHAR ARRAY text,chars,result)
BYTE i,j,pos,found
pos=text(0)
FOR i=1 TO text(0)
DO
found=0
FOR j=1 TO chars(0)
DO
IF text(i)=chars(j) THEN
found=1 EXIT
FI
OD
IF found THEN
pos=i-1 EXIT
FI
OD
WHILE pos>0 AND text(pos)='
DO
pos==-1
OD
SCopyS(result,text,1,pos)
RETURN
PROC Test(CHAR ARRAY text,chars)
CHAR ARRAY result(255)
Strip(text,chars,result)
PrintF("""%S"", ""%S"" -> ""%S""%E%E",text,chars,result)
RETURN
PROC Main()
Test("apples, pears # and bananas","#;")
Test("apples, pears ; and bananas","#;")
Test("qwerty # asdfg ; zxcvb","#")
Test("qwerty # asdfg ; zxcvb",";")
Test(" ;this is a comment","#;")
Test("#this is a comment","#;")
Test(" ",";")
Test("","#")
RETURN
- Output:
Screenshot from Atari 8-bit computer
"apples, pears # and bananas", "#;" -> "apples, pears" "apples, pears ; and bananas", "#;" -> "apples, pears" "qwerty # asdfg ; zxcvb", "#" -> "qwerty" "qwerty # asdfg ; zxcvb", ";" -> "qwerty # asdfg" " ;this is a comment", "#;" -> "" "#this is a comment", "#;" -> "" " ", ";" -> "" "", "#" -> ""
Ada
with Ada.Text_IO;
procedure Program is
Comment_Characters : String := "#;";
begin
loop
declare
Line : String := Ada.Text_IO.Get_Line;
begin
exit when Line'Length = 0;
Outer_Loop : for I in Line'Range loop
for J in Comment_Characters'Range loop
if Comment_Characters(J) = Line(I) then
Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
exit Outer_Loop;
end if;
end loop;
end loop Outer_Loop;
end;
end loop;
end Program;
Aime
strip_comments(data b)
{
b.size(b.look(0, ";#")).bf_drop(" \t").bb_drop(" \t");
}
main(void)
{
for (, text n in list("apples, pears # and bananas", "apples, pears ; and bananas")) {
o_(strip_comments(n), "\n");
}
0;
}
ALGOL 68
#!/usr/local/bin/a68g --script #
PROC trim comment = (STRING line, CHAR marker)STRING:(
INT index := UPB line+1;
char in string(marker, index, line);
FOR i FROM index-1 BY -1 TO LWB line
WHILE line[i]=" " DO index := i OD;
line[:index-1]
);
CHAR q = """";
print((
q, trim comment("apples, pears # and bananas", "#"), q, new line,
q, trim comment("apples, pears ; and bananas", ";"), q, new line,
q, trim comment("apples, pears and bananas ", ";"), q, new line,
q, trim comment(" ", ";"), q, new line, # blank string #
q, trim comment("", ";"), q, new line # empty string #
))
CO Alternatively Algol68g has available "grep"
;STRING re marker := " *#", line := "apples, pears # and bananas";
INT index := UPB line;
grep in string(re marker, line, index, NIL);
print((q, line[:index-1], q, new line))
END CO
Output:
"apples, pears" "apples, pears" "apples, pears and bananas" "" ""
ALGOL W
Leading and trailing spaces are removed from the result, as per the March 29 2011 task version.
begin
% determines the non-comment portion of the string s, startPos and endPos are %
% returned set to the beginning and ending character positions (indexed from 0) %
% of the non-comment text in s. If there is no non-comment text in s, startPos %
% will be greater than endPos %
% note that in Algol W, strings can be at most 256 characters long %
procedure stripComments ( string(256) value s; integer result startPos, endPos ) ;
begin
integer MAX_LENGTH;
MAX_LENGTH := 256;
startPos := 0;
endPos := -1;
% find the first non-blank character in s %
while startPos < MAX_LENGTH and s( startPos // 1 ) = " " do startPos := startPos + 1;
if startPos < MAX_LENGTH then begin
% have a non-blank character in the string %
if s( startPos // 1 ) not = "#" and s( startPos // 1 ) not = ";" then begin
% the non-blank character is not a comment delimiter %
integer cPos;
cPos := endPos := startPos;
while cPos < MAX_LENGTH and s( cPos // 1 ) not = "#" and s( cPos // 1 ) not = ";" do begin
if s( cPos // 1 ) not = " " then endPos := cPos;
cPos := cPos + 1
end while_not_a_comment
end if_not_a_comment
end if_startPos_lt_MAX_LENGTH
end stripComments ;
% tests the stripComments procedure %
procedure testStripComments( string(256) value s ) ;
begin
integer startPos, endPos;
stripComments( s, startPos, endPos );
write( """" );
for cPos := startPos until endPos do writeon( s( cPos // 1 ) );
writeon( """" )
end testStripComments ;
begin % test cases - should all print "apples, pears" %
testStripComments( "apples, pears # and bananas" );
testStripComments( "apples, pears ; and bananas" );
testStripComments( "apples, pears " );
testStripComments( " apples, pears" )
end
end.
- Output:
"apples, pears" "apples, pears" "apples, pears" "apples, pears"
Applesoft BASIC
10 LET C$ = ";#"
20 S$(1)="APPLES, PEARS # AND BANANAS"
30 S$(2)="APPLES, PEARS ; AND BANANAS"
40 FOR Q = 1 TO 2
50 LET S$ = S$(Q)
60 GOSUB 100"STRIP COMMENTS
70 PRINT S$
80 NEXT Q
90 END
100 IF S$ = "" THEN RETURN
110 FOR I = 1 TO LEN(S$)
120 LET A$ = MID$(S$, I, 1)
130 FOR J = 1 TO LEN(C$)
140 LET F$ = MID$(C$, J, 1)
150 IF A$ <> F$ THEN NEXT J
160 IF A$ = F$ THEN 200
170 NEXT I
200 LET I = I - 1
210 GOSUB 260"STRIP
220 IF S$ = "" THEN RETURN
230 FOR I = I TO 0 STEP -1
240 LET A$ = MID$(S$, I, 1)
250 IF A$ = " " THEN NEXT I
260 LET S$ = MID$(S$, 1, I)
270 RETURN
Arturo
stripComments: function [str][
strip replace str {/[#;].+/} ""
]
loop ["apples, pears # and bananas", "apples, pears ; and bananas"] 'str [
print [str "->" stripComments str]
]
- Output:
apples, pears # and bananas -> apples, pears apples, pears ; and bananas -> apples, pears
AutoHotkey
Delims := "#;"
str := "apples, pears # and bananas"
str2:= "apples, pears, `; and bananas" ; needed to escape the ; since that is AHK's comment marker
msgbox % StripComments(Str,Delims)
msgbox % StripComments(Str2,Delims)
; The % forces expression mode.
StripComments(String1,Delims){
Loop, parse, delims
{
If Instr(String1,A_LoopField)
EndPosition := InStr(String1,A_LoopField) - 1
Else
EndPosition := StrLen(String1)
StringLeft, String1, String1, EndPosition
}
return String1
}
Output:
apples, pears apples, pears,
AutoIt
It was always said in discussion, the task is not really stripping comments. It's only a truncation.
Dim $Line1 = "apples, pears # and bananas"
Dim $Line2 = "apples, pears ; and bananas"
_StripAtMarker($Line1)
_StripAtMarker($Line2)
Func _StripAtMarker($_Line, $sMarker='# ;')
Local $aMarker = StringSplit($sMarker, ' ')
Local $iPos
For $i = 1 To $aMarker[0]
$iPos = StringInStr($_Line, $aMarker[$i])
If $iPos Then
ConsoleWrite($_Line & @CRLF)
ConsoleWrite( StringStripWS( StringLeft($_Line, $iPos -1), 2) & @CRLF)
EndIf
Next
EndFunc ;==>_StripAtMarker
Output:
apples, pears # and bananas apples, pears apples, pears ; and bananas apples, pears
Here is a really language-related solution to parse script lines and delete comments. A comment in line in AutoIt starts with an semicolon. But it may be possible, that a semicolon is part of a string in a parameter from an function-call/function-headline or in an assignment. That means: the comment starts with the first semicolon outside a string.
Dim $aLines[4] = _
[ _
"$a = $b + $c ; Comment line 1", _
"Dim $s1 = 'some text; tiled with semicolon', $s2 = 'another text; also tiled with semicolon' ; Comment line 2 - semicolon as part of assignment", _
"_SomeFunctionCall('string parameter with ;', $anotherParam) ; Comment line 3 - semicolon as part parameter in an function call", _
"Func _AnotherFunction($param1=';', $param2=';', $param3=';') ; Comment line 4 - semicolon as default value in parameter of a function headline" _
]
For $i = 0 To 3
ConsoleWrite('+> Line ' & $i+1 & ' full:' & @CRLF & '+>' & $aLines[$i] & @CRLF)
ConsoleWrite('!> without comment:' & @CRLF & '!>' & _LineStripComment($aLines[$i]) & @CRLF & @CRLF)
Next
Func _LineStripComment($_Line)
; == tile line by all included comment marker
Local $aPartsWithMarker = StringSplit($_Line, ';')
Local $sNoComment
; == if no comment marker: return full line
If $aPartsWithMarker[0] = 0 Then Return $_Line
; == check if string in part, if is'nt: following part(s) are comment
For $i = 1 To $aPartsWithMarker[0]
If Not StringRegExp($aPartsWithMarker[$i], "('|\x22)") Then
If $i = 1 Then
Return StringStripWS($aPartsWithMarker[$i], 2)
Else
Return StringStripWS($sNoComment & $aPartsWithMarker[$i], 2)
EndIf
Else
; == check if next leftside string delimiter has uneven count
Local $iLen = StringLen($aPartsWithMarker[$i])
Local $fDetectDelim = False, $sStringDelim, $iDelimCount, $sCurr
For $j = $iLen To 1 Step -1
$sCurr = StringMid($aPartsWithMarker[$i], $j, 1)
If Not $fDetectDelim Then
If $sCurr = "'" Or $sCurr = '"' Then
$sStringDelim = $sCurr
$iDelimCount += 1
$fDetectDelim = True
EndIf
Else
If $sCurr = $sStringDelim Then $iDelimCount += 1
EndIf
Next
If Mod($iDelimCount, 2) Then
; == uneven count: so it masks the comment marker
$sNoComment &= $aPartsWithMarker[$i] & ';'
Else
; == even count: all following is comment
Return StringStripWS($sNoComment & $aPartsWithMarker[$i], 2)
EndIf
EndIf
Next
EndFunc ;==>_LineStripComment
Output:
+> Line 1 full: +>$a = $b + $c ; Comment line 1 >> without comment: >>$a = $b + $c +> Line 2 full: +>Dim $s1 = 'some text; tiled with semicolon', $s2 = 'another text; also tiled with semicolon' ; Comment line 2 - semicolon as part of assignment >> without comment: >>Dim $s1 = 'some text; tiled with semicolon', $s2 = 'another text; also tiled with semicolon' +> Line 3 full: +>_SomeFunctionCall('string parameter with ;', $anotherParam) ; Comment line 3 - semicolon as part parameter in an function call >> without comment: >>_SomeFunctionCall('string parameter with ;', $anotherParam) +> Line 4 full: +>Func _AnotherFunction($param1=';', $param2=';', $param3=';') ; Comment line 4 - semicolon as default value in parameter of a function headline >> without comment: >>Func _AnotherFunction($param1=';', $param2=';', $param3=';')
AWK
#!/usr/local/bin/awk -f
{
sub("[ \t]*[#;].*$","",$0);
print;
}
BASIC
ANSI BASIC
100 DECLARE EXTERNAL FUNCTION FNstripcomment$
110 LET marker$="#;"
120 PRINT """";FNstripcomment$("apples, pears # and bananas", marker$);""""
130 PRINT """";FNstripcomment$("apples, pears ; and bananas", marker$);""""
140 PRINT """";FNstripcomment$(" apples, pears ", marker$);""""
150 END
160 !
170 EXTERNAL FUNCTION FNstripcomment$(text$, delim$)
180 FOR I=1 TO LEN(delim$)
190 LET D = POS(text$, delim$(I:I))
200 IF D>0 THEN LET text$ = text$(1:D-1)
210 NEXT I
220 LET FNstripcomment$=RTRIM$(text$)
230 END FUNCTION
Output:
"apples, pears" "apples, pears" " apples, pears"
BASIC256
arraybase 1
dim s$(4)
s$[1] = "apples, pears # and bananas"
s$[2] = "apples, pears ; and bananas"
s$[3] = "# this is a comment"
s$[4] = " # this is a comment with leading whitespace"
for i = 1 to 4
call stripComment(s$[i], "#;")
print s$[i], " => Length = "; length(s$[i])
next i
end
subroutine stripComment(s$, commentMarkers$)
if s$ = "" then return
i = instr(s$, commentMarkers$)
if i > 0 then
s$ = left$(s$, i - 1)
s$ = trim(s$) # removes both leading and trailing whitespace
end if
end subroutine
- Output:
apples, pears # and bananas => Length = 27 apples, pears ; and bananas => Length = 27 # this is a comment => Length = 19 # this is a comment with leading whitespace => Length = 45
BQN
StripW
only removes spaces. StripC
uses that in conjunction with a filter that removes comments.
StripW←((∨`∧∨`⌾⌽)' '⊸≠)⊸/
StripC←StripW (¬·∨`∊⟜"#;")⊸/
•Show StripC " apples, pears # and bananas "
•Show StripC " apples, pears ; and bananas"
"apples, pears"
"apples, pears"
Bracmat
( " apples, pears # and bananas
oranges, mangos ; and a durian"
: ?text
& :?newText
& ( non-blank
= %@:~(" "|\t|\r|\n)
)
& ( cleanUp
=
. @(!arg:?arg ("#"|";") ?)
& @(rev$!arg:? (!non-blank ?:?arg))
& @(rev$!arg:? (!non-blank ?:?arg))
& !arg {You could write & "[" !arg "]" to prove that the blanks are stripped.}
)
& whl
' ( @(!text:?line (\r|\n) ?text)
& !newText \n cleanUp$!line:?newText
)
& !newText \n cleanUp$!text:?newText
& out$(str$!newText)
);
Output:
apples, pears oranges, mangos
C
#include<stdio.h>
int main()
{
char ch, str[100];
int i;
do{
printf("\nEnter the string :");
fgets(str,100,stdin);
for(i=0;str[i]!=00;i++)
{
if(str[i]=='#'||str[i]==';')
{
str[i]=00;
break;
}
}
printf("\nThe modified string is : %s",str);
printf("\nDo you want to repeat (y/n): ");
scanf("%c",&ch);
fflush(stdin);
}while(ch=='y'||ch=='Y');
return 0;
}
Output:
Enter the string :apples, pears # and bananas The modified string is : apples, pears Do you want to repeat (y/n): y Enter the string :apples, pears ; and bananas The modified string is : apples, pears Do you want to repeat (y/n): n
C#
using System.Text.RegularExpressions;
string RemoveComments(string str, string delimiter)
{
//regular expression to find a character (delimiter) and
// replace it and everything following it with an empty string.
//.Trim() will remove all beginning and ending white space.
return Regex.Replace(str, delimiter + ".+", string.Empty).Trim();
}
Sample output:
Console.WriteLine(RemoveComments("apples, pears # and bananas", "#")); Console.WriteLine(RemoveComments("apples, pears ; and bananas", ";")); apples, pears apples, pears
C++
#include <iostream>
#include <string>
std::string strip_white(const std::string& input)
{
size_t b = input.find_first_not_of(' ');
if (b == std::string::npos) b = 0;
return input.substr(b, input.find_last_not_of(' ') + 1 - b);
}
std::string strip_comments(const std::string& input, const std::string& delimiters)
{
return strip_white(input.substr(0, input.find_first_of(delimiters)));
}
int main( ) {
std::string input;
std::string delimiters("#;");
while ( getline(std::cin, input) && !input.empty() ) {
std::cout << strip_comments(input, delimiters) << std::endl ;
}
return 0;
}
Sample output:
apples, pears # and bananas apples, pears apples, pears ; and bananas apples, pears
Clojure
> (apply str (take-while #(not (#{\# \;} %)) "apples # comment"))
"apples "
COBOL
identification division.
program-id. StripComments.
data division.
working-storage section.
01 line-text pic x(64).
procedure division.
main.
move "apples, pears # and bananas" to line-text
perform show-striped-text
move "apples, pears ; and bananas" to line-text
perform show-striped-text
stop run
.
show-striped-text.
unstring line-text delimited by "#" or ";" into line-text
display quote, function trim(line-text), quote
.
- Output:
"apples, pears" "apples, pears"
Common Lisp
(defun strip-comments (s cs)
"Truncate s at the first occurrence of a character in cs."
(defun comment-char-p (c)
(some #'(lambda (x) (char= x c)) cs))
(let ((pos (position-if #'comment-char-p s)))
(subseq s 0 pos)))
- Output:
Use the function in combination with STRING-TRIM to fulfill the task requirements.
> (string-trim '(#\Space #\Tab) (strip-comments "apples, pears # and bananas" "#;")) "apples, pears" > (string-trim '(#\Space #\Tab) (strip-comments "apples, pears ; and bananas" "#;")) "apples, pears" > (string-trim '(#\Space #\Tab) (strip-comments " apples, pears and bananas " "#;")) "apples, pears and bananas"
D
import std.stdio, std.regex;
string remove1LineComment(in string s, in string pat=";#") {
const re = "([^" ~ pat ~ "]*)([" ~ pat ~ `])[^\n\r]*([\n\r]|$)`;
return s.replace(regex(re, "gm"), "$1$3");
}
void main() {
const s = "apples, pears # and bananas
apples, pears ; and bananas ";
writeln(s, "\n====>\n", s.remove1LineComment());
}
- Output:
apples, pears # and bananas apples, pears ; and bananas ====> apples, pears apples, pears
Delphi
program StripComments;
{$APPTYPE CONSOLE}
uses
SysUtils;
function DoStripComments(const InString: string; const CommentMarker: Char): string;
begin
Result := Trim(Copy(InString,1,Pos(CommentMarker,InString)-1));
end;
begin
Writeln('apples, pears # and bananas --> ' + DoStripComments('apples, pears # and bananas','#'));
Writeln('');
Writeln('apples, pears ; and bananas --> ' + DoStripComments('apples, pears ; and bananas',';'));
Readln;
end.
DWScript
function StripComments(s : String) : String;
begin
var p := FindDelimiter('#;', s);
if p>0 then
Result := Trim(Copy(s, 1, p-1))
else Result := Trim(s);
end;
PrintLn(StripComments('apples, pears # and bananas'));
PrintLn(StripComments('apples, pears ; and bananas'));
EasyLang
func$ strip s$ .
i = 1
repeat
c$ = substr s$ i 1
until c$ = "#" or c$ = ";" or c$ = ""
if c$ = " " and sp = 0
sp = i
elif c$ <> " "
sp = 0
.
i += 1
.
if sp = 0
sp = i
.
return substr s$ 1 (sp - 1)
.
print strip "Regular string" & "."
print strip "With a hash# a comment" & "."
print strip "With a hash # a comment" & "."
print strip "With a semicolon ; a comment" & "."
print strip "No comment " & "."
Erlang
-module( strip_comments_from_string ).
-export( [task/0] ).
task() ->
io:fwrite( "~s~n", [keep_until_comment("apples, pears and bananas")] ),
io:fwrite( "~s~n", [keep_until_comment("apples, pears # and bananas")] ),
io:fwrite( "~s~n", [keep_until_comment("apples, pears ; and bananas")] ).
keep_until_comment( String ) -> lists:takewhile( fun not_comment/1, String ).
not_comment( $# ) -> false;
not_comment( $; ) -> false;
not_comment( _ ) -> true.
- Output:
17> strip_comments_from_string:task(). apples, pears and bananas apples, pears apples, pears
F#
let stripComments s =
s
|> Seq.takeWhile (fun c -> c <> '#' && c <> ';')
|> Seq.map System.Char.ToString
|> Seq.fold (+) ""
Factor
USE: sequences.extras
: strip-comments ( str -- str' )
[ "#;" member? not ] take-while "" like ;
Fantom
Using a regular expression:
class Main
{
static Str removeComment (Str str)
{
regex := Regex <|(;|#)|>
matcher := regex.matcher (str)
if (matcher.find)
return str[0..<matcher.start]
else
return str
}
public static Void main ()
{
echo (removeComment ("String with comment here"))
echo (removeComment ("String with comment # here"))
echo (removeComment ("String with comment ; here"))
}
}
Forth
Modern Forth advocates the use of stack strings. Stack strings are manipulated as an address and a length on the Forth DATA stack. As such they do not require memory copying for many forms of string functions making them fast. Stack string functions typically return a stack string allowing concatenation of string functions.
NOTES:
1. SKIP and SCAN are not ANS Forth but are common in most Forth systems as either library functions or resident.
2. -TRAILING is a resident function in most Forth systems. Shown for clarity.
Tested with GForth on Windows
\ Rosetta Code Strip Comment
: LASTCHAR ( addr len -- addr len c) 2DUP + C@ ;
: COMMENT? ( char -- ? ) S" #;" ROT SCAN NIP ; \ test char for "#;"
: -LEADING ( addr len -- addr' len') BL SKIP ; \ remove leading space characters
: -COMMENT ( addr len -- addr len') \ removes # or ; comments
1-
BEGIN
LASTCHAR COMMENT? 0=
WHILE \ while not a comment char...
1- \ reduce length by 1
REPEAT
1- ; \ remove 1 more (the comment char)
: -TRAILING ( adr len -- addr len') \ remove trailing spaces
1-
BEGIN
LASTCHAR BL =
WHILE \ while lastchar = blank
1- \ reduce length by 1
REPEAT
1+ ;
: COMMENT-STRIP ( addr len -- addr 'len) -LEADING -COMMENT -TRAILING ;
Tested at the Forth console
S" apples, pears # and bananas" COMMENT-STRIP TYPE apples, pears ok S" apples, pears ; and bananas" COMMENT-STRIP TYPE apples, pears ok
Fortran
!****************************************************
module string_routines
!****************************************************
implicit none
private
public :: strip_comments
contains
!****************************************************
function strip_comments(str,c) result(str2)
implicit none
character(len=*),intent(in) :: str
character(len=1),intent(in) :: c !comment character
character(len=len(str)) :: str2
integer :: i
i = index(str,c)
if (i>0) then
str2 = str(1:i-1)
else
str2 = str
end if
end function strip_comments
!****************************************************
end module string_routines
!****************************************************
!****************************************************
program main
!****************************************************
! Example use of strip_comments function
!****************************************************
use string_routines, only: strip_comments
implicit none
write(*,*) strip_comments('apples, pears # and bananas', '#')
write(*,*) strip_comments('apples, pears ; and bananas', ';')
!****************************************************
end program main
!****************************************************
output:
apples, pears apples, pears
FreeBASIC
' FB 1.05.0 Win64
Sub stripComment(s As String, commentMarkers As String)
If s = "" Then Return
Dim i As Integer = Instr(s, Any commentMarkers)
If i > 0 Then
s = Left(s, i - 1)
s = Trim(s) '' removes both leading and trailing whitespace
End If
End Sub
Dim s(1 To 4) As String = _
{ _
"apples, pears # and bananas", _
"apples, pears ; and bananas", _
"# this is a comment", _
" # this is a comment with leading whitespace" _
}
For i As Integer = 1 To 4
stripComment(s(i), "#;")
Print s(i), " => Length ="; Len(s(i))
Next
Print
Print "Press any key to quit"
Sleep
- Output:
apples, pears => Length = 13 apples, pears => Length = 13 => Length = 0 => Length = 0
FutureBasic
include "NSLog.incl"
local fn StripCommentsFromString( string as CFStringRef ) as CFStringRef
CFRange range = fn StringRangeOfCharacterFromSet( string, fn CharacterSetWithCharactersInString( @"#;" ) )
if ( range.location != NSNotFound )
string = fn StringSubstringToIndex( string, range.location )
string = fn StringByTrimmingCharactersInSet( string, fn CharacterSetWhitespaceSet )
end if
end fn = string
NSLog(@"%@",fn StripCommentsFromString(@"apples, pears # and bananas"))
NSLog(@"%@",fn StripCommentsFromString(@"apples, pears ; and bananas"))
HandleEvents
Go
package main
import (
"fmt"
"strings"
"unicode"
)
const commentChars = "#;"
func stripComment(source string) string {
if cut := strings.IndexAny(source, commentChars); cut >= 0 {
return strings.TrimRightFunc(source[:cut], unicode.IsSpace)
}
return source
}
func main() {
for _, s := range []string{
"apples, pears # and bananas",
"apples, pears ; and bananas",
"no bananas",
} {
fmt.Printf("source: %q\n", s)
fmt.Printf("stripped: %q\n", stripComment(s))
}
}
Output:
source: "apples, pears # and bananas" stripped: "apples, pears" source: "apples, pears ; and bananas" stripped: "apples, pears" source: "no bananas" stripped: "no bananas"
Groovy
def stripComments = { it.replaceAll(/\s*[#;].*$/, '') }
Testing:
assert 'apples, pears' == stripComments('apples, pears # and bananas')
assert 'apples, pears' == stripComments('apples, pears ; and bananas')
Haskell
ms = ";#"
main = getContents >>=
mapM_ (putStrLn . takeWhile (`notElem` ms)) . lines
Icon and Unicon
Output:
apples, pears and bananas apples, pears apples, pears
Inform 7
Home is a room.
When play begins:
strip comments from "apples, pears # and bananas";
strip comments from "apples, pears ; and bananas";
end the story.
To strip comments from (T - indexed text):
say "[T] -> ";
replace the regular expression "<#;>.*$" in T with "";
say "[T][line break]".
Since square brackets have a special meaning in strings, Inform's regular expression syntax uses angle brackets for character grouping.
J
Solution 1 (mask & filter):
strip=: dltb@(#~ *./\@:-.@e.&';#')
Solution 2 (index & cut):
strip=: dltb@({.~ <./@i.&';#')
Example:
dquote strip ' apples, pears # and bananas' NB. quote result to show stripped whitespace
"apples, pears"
strip ' apples, pears ; and bananas'
apples, pears
Java
import java.io.*;
public class StripLineComments{
public static void main( String[] args ){
if( args.length < 1 ){
System.out.println("Usage: java StripLineComments StringToProcess");
}
else{
String inputFile = args[0];
String input = "";
try{
BufferedReader reader = new BufferedReader( new FileReader( inputFile ) );
String line = "";
while((line = reader.readLine()) != null){
System.out.println( line.split("[#;]")[0] );
}
}
catch( Exception e ){
e.printStackTrace();
}
}
}
}
JavaScript
ES5
function stripComments(s) {
var re1 = /^\s+|\s+$/g; // Strip leading and trailing spaces
var re2 = /\s*[#;].+$/g; // Strip everything after # or ; to the end of the line, including preceding spaces
return s.replace(re1,'').replace(re2,'');
}
var s1 = 'apples, pears # and bananas';
var s2 = 'apples, pears ; and bananas';
alert(stripComments(s1) + '\n' + stripComments(s2));
A more efficient version that caches the regular expressions in a closure:
var stripComments = (function () {
var re1 = /^\s+|\s+$/g;
var re2 = /\s*[#;].+$/g;
return function (s) {
return s.replace(re1,'').replace(re2,'');
};
}());
A difference with the two versions is that in the first, all declarations are processed before code is executed so the function declaration can be after the code that calls it. However in the second example, the expression creating the function must be executed before the function is available, so it must be before the code that calls it.
ES6
(() => {
'use strict';
const main = () => {
const src = `apples, pears # and bananas
apples, pears ; and bananas`;
return unlines(
map(preComment(chars(';#')),
lines(src)
)
);
};
// preComment :: [Char] -> String -> String
const preComment = cs => s =>
strip(
takeWhile(
curry(flip(notElem))(cs),
s
)
);
// GENERIC FUNCTIONS ------------------------------
// chars :: String -> [Char]
const chars = s => s.split('');
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f => a => b => f(a, b);
// flip :: (a -> b -> c) -> b -> a -> c
const flip = f => (a, b) => f.apply(null, [b, a]);
// lines :: String -> [String]
const lines = s => s.split(/[\r\n]/);
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// notElem :: Eq a => a -> [a] -> Bool
const notElem = (x, xs) => -1 === xs.indexOf(x);
// strip :: String -> String
const strip = s => s.trim();
// takeWhile :: (a -> Bool) -> [a] -> [a]
// takeWhile :: (Char -> Bool) -> String -> String
const takeWhile = (p, xs) => {
let i = 0;
const lng = xs.length;
while ((i < lng) && p(xs[i]))(i = i + 1);
return xs.slice(0, i);
};
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// MAIN ---
return main();
})();
- Output:
apples, pears apples, pears
jq
If your version of jq has regex support, the task can be accomplished with the following one-liner:
sub("[#;].*";"") | sub("^\\s+";"") | sub("\\s+$";"")
Otherwise, we can define strip_comment as a jq filter, as follows. For clarity, the helper functions are presented as top-level functions.
# define whitespace here as a tab, space, newline, return or form-feed character:
def is_whitespace: . as $in | " \n\r\f\t" | index($in);
def ltrim:
if .[0:1] | is_whitespace then (.[1:]|ltrim) else . end;
def rtrim:
if .[length-1:] | is_whitespace then .[0:length-1]|rtrim else . end;
def trim: ltrim | rtrim;
def strip_comment:
index("#") as $i1 | index(";") as $i2
| (if $i1 then if $i2 then [$i1, $i2] | min
else $i1
end
else $i2
end ) as $ix
| if $ix then .[0:$ix] else . end
| trim;
Example:
" abc ; def # ghi" | strip_comment
- Output:
"abc"
Julia
striplinecomment is designed to be flexible and robust. By default # and ; are considered comment defining characters, but any characters can be used by passing them as the string cchars. All such characters are escaped in the regular expression used to eliminate comments to allow characters special to the Regex language (e.g. ^, $, [) to be used as a comment character.
using Printf
function striplinecomment(a::String, cchars::String="#;")
b = strip(a)
0 < length(cchars) || return b
for c in cchars
r = Regex(@sprintf "\\%c.*" c)
b = replace(b, r => "")
end
strip(b)
end
tests = ("apples, pears # and bananas",
"apples, pears ; and bananas",
" apples, pears & bananas ",
" # ")
for t in tests
s = striplinecomment(t)
println("Testing \"", t, "\":")
println(" \"", s, "\"")
end
- Output:
Testing "apples, pears # and bananas": "apples, pears" Testing "apples, pears ; and bananas": "apples, pears" Testing " apples, pears & bananas ": "apples, pears & bananas" Testing " # ": ""
Kotlin
For the avoidance of doubt and in line with what the current description of this task appears to be, the following program is designed to remove comments from a string which in Kotlin are:
1. // this is a comment (to the end of the line)
2. /* this is a comment */
3. /* outer comment /* nested comment */ more outer comment */
It then removes whitespace from the beginning and end of the resulting string:
// version 1.0.6
val r = Regex("""(/\*.*\*/|//.*$)""")
fun stripComments(s: String) = s.replace(r, "").trim()
fun main(args: Array<String>) {
val strings = arrayOf(
"apples, pears // and bananas",
" apples, pears /* and bananas */",
"/* oranges */ apples // pears and bananas ",
" \toranges /*apples/*, pears*/*/and bananas"
)
for (string in strings) println(stripComments(string))
}
- Output:
apples, pears apples, pears apples oranges and bananas
Liberty BASIC
string1$ = "apples, pears # and bananas"
string2$ = "pears;, " + chr$(34) + "apples ; " + chr$(34) + " an;d bananas"
commentMarker$ = "; #"
Print parse$(string2$, commentMarker$)
End
Function parse$(string$, commentMarker$)
For i = 1 To Len(string$)
charIn$ = Mid$(string$, i, 1)
If charIn$ = Chr$(34) Then
inQuotes = Not(inQuotes)
End If
If Instr(commentMarker$, charIn$) And (inQuotes = 0) Then Exit For
next i
parse$ = Left$(string$, (i - 1))
End Function
Lua
comment_symbols = ";#"
s1 = "apples, pears # and bananas"
s2 = "apples, pears ; and bananas"
print ( string.match( s1, "[^"..comment_symbols.."]+" ) )
print ( string.match( s2, "[^"..comment_symbols.."]+" ) )
Maple
> use StringTools in map( Trim@Take, [ "\t\t apples, pears \t# and bananas", " apples, pears ; and bananas \t" ], "#;" ) end;
["apples, pears", "apples, pears"]
Mathematica/Wolfram Language
a = "apples, pears # and bananas
apples, pears ; and bananas";
b = StringReplace[a, RegularExpression["[ ]+[#;].+[\n]"] -> "\n"];
StringReplace[b, RegularExpression["[ ]+[#;].+$"] -> ""] // FullForm
- Output:
"apples, pears\napples, pears"
MATLAB / Octave
function line = stripcomment(line)
e = min([find(line=='#',1),find(line==';',1)]);
if ~isempty(e)
e = e-1;
while isspace(line(e)) e = e - 1; end;
line = line(1:e);
end;
end;
Output:
>> stripcomment('apples, pears # and bananas\n') ans = apples, pears >> stripcomment('apples, pears ; and bananas\n') ans = apples, pears
MiniScript
strip = function(test)
comment = test.indexOf("#")
if comment == null then comment = test.indexOf(";")
if comment then test = test[:comment]
while test[-1] == " "
test = test - " "
end while
return test
end function
print strip("This is a hash test # a comment") + "."
print strip("This is a semicolon test ; a comment") + "."
print strip("This is a no comment test ") + "."
- Output:
This is a hash test. This is a semicolon test. This is a no comment test.
Nim
import strutils
proc removeComments(line: string; sep: char): string =
line.split(sep)[0].strip(leading = false)
const
Str1 = "apples, pears # and bananas"
Str2 = "apples, pears ; and bananas"
echo "Original: “$#”" % Str1
echo "Stripped: “$#”" % Str1.removeComments('#')
echo "Original: “$#”" % Str2
echo "Stripped: “$#”" % Str2.removeComments(';')
- Output:
Original: “apples, pears # and bananas” Stripped: “apples, pears” Original: “apples, pears ; and bananas” Stripped: “apples, pears”
Objeck
use System.IO.File;
class StripComments {
function : Main(args : String[]) ~ Nil {
reader : FileReader;
if(args->Size() = 1) {
reader := FileReader->New(args[0]);
line := reader->ReadString();
while(line <> Nil) {
index := line->FindLast(';');
if(index < 0) {
index := line->FindLast('#');
};
if(index > -1) {
line->SubString(index)->PrintLine();
};
line := reader->ReadString();
};
};
leaving {
if(reader <> Nil) {
reader->Close();
};
};
}
}
OCaml
let strip_comments str =
let len = String.length str in
let rec aux print i =
if i >= len then () else
match str.[i] with
| '#' | ';' ->
aux false (succ i)
| '\n' ->
print_char '\n';
aux true (succ i)
| c ->
if print then print_char c;
aux print (succ i)
in
aux true 0
let () =
strip_comments "apples, pears # and bananas\n";
strip_comments "apples, pears ; and bananas\n";
;;
or with an imperative style:
let strip_comments =
let print = ref true in
String.iter (function
| ';' | '#' -> print := false
| '\n' -> print_char '\n'; print := true
| c -> if !print then print_char c)
Oforth
: stripComments(s, markers)
| firstMarker |
markers map(#[ s indexOf ]) reduce(#min) ->firstMarker
s firstMarker ifNotNull: [ left(firstMarker 1 - ) ] strip ;
- Output:
>stripComments("apples, pears # and bananas", "#") println apples, pears >stripComments("apples, pears ; and bananas # and oranges", "#;") println apples, pears
Pascal
See Delphi
Perl
while (<>)
{
s/[#;].*$//s; # remove comment
s/^\s+//; # remove leading whitespace
s/\s+$//; # remove trailing whitespace
print
}
Phix
Added a couple of things that can go wrong with something nowhere near sufficiently smart or for that matter language-specific enough.
A line comment inside a block comment (eg " /* left -- right */") could also be very dodgy, and perhaps vice versa.
with javascript_semantics function strip_comments(string s, sequence comments={"#",";"}) for i=1 to length(comments) do integer k = match(comments[i],s) if k then s = s[1..k-1] s = trim_tail(s) end if end for return s end function ?strip_comments("apples, pears # and bananas") ?strip_comments("apples, pears ; and bananas") ?strip_comments("apples, pears and bananas ") ?strip_comments(" WS_CAPTION = #00C00000, -- = WS_BORDER+WS_DLGFRAME") ?strip_comments(" WS_CAPTION = #00C00000, -- = WS_BORDER+WS_DLGFRAME",{"--"}) ?strip_comments(" title = \"--Title--\"",{"--"})
- Output:
"apples, pears" "apples, pears" "apples, pears and bananas " " WS_CAPTION =" " WS_CAPTION = #00C00000," " title = \""
PicoLisp
(for Str '("apples, pears # and bananas" "apples, pears ; and bananas")
(prinl (car (split (chop Str) "#" ";"))) )
Output:
apples, pears apples, pears
PL/I
k = search(text, '#;');
if k = 0 then put skip list (text);
else put skip list (substr(text, 1, k-1));
Prolog
This version is implemented as a state automata to strip multiple lines of comments.
stripcomment(A,B) :- stripcomment(A,B,a).
stripcomment([A|AL],[A|BL],a) :- \+ A=0';, \+ A=0'# , \+ A=10, \+ A=13 , stripcomment(AL,BL,a).
stripcomment([A|AL], BL ,a) :- ( A=0';; A=0'#), \+ A=10, \+ A=13 , stripcomment(AL,BL,b).
stripcomment([A|AL], BL ,b) :- \+ A=10, \+ A=13 , stripcomment(AL,BL,b).
stripcomment([A|AL],[A|BL],_M):- ( A=10; A=13), stripcomment(AL,BL,a).
stripcomment([],[],_M).
start :-
In = "apples, pears ; and bananas
apples, pears # and bananas",
stripcomment(In,Out),
format("~s~n",[Out]).
Output:
?- start. apples, pears apples, pears
This version uses prolog's pattern matching with two append/3 to strip 1 line.
strip_1comment(A,D) :- ((S1=0'#;S1=0';),append(B,[S1|C],A)), \+ ((S2=0'#;S2=0';),append(_X,[S2|_Y],B)) -> B=D; A=D.
At the query console:
?- strip_1comment("apples, pears ; and bananas",O1),format("~s~n",[O1]). apples, pears O1 = [97, 112, 112, 108, 101, 115, 44, 32, 112|...] . ?- strip_1comment("apples, pears # and bananas",O1),format("~s~n",[O1]). apples, pears O1 = [97, 112, 112, 108, 101, 115, 44, 32, 112|...] .
PureBasic
Procedure.s Strip_comments(Str$)
Protected result$=Str$, l, l1, l2
l1 =FindString(Str$,"#",1)
l2 =FindString(Str$,";",1)
;
; See if any comment sign was found, prioritizing '#'
If l1
l=l1
ElseIf l2
l=l2
EndIf
l-1
If l>0
result$=Left(Str$,l)
EndIf
ProcedureReturn result$
EndProcedure
Implementation
#instring1 ="apples, pears # and bananas"
#instring2 ="apples, pears ; and bananas"
PrintN(Strip_comments(#instring1))
PrintN(Strip_comments(#instring2))
Output:
apples, pears apples, pears
Python
Procedural
def remove_comments(line, sep):
for s in sep:
i = line.find(s)
if i >= 0:
line = line[:i]
return line.strip()
# test
print remove_comments('apples ; pears # and bananas', ';#')
print remove_comments('apples ; pears # and bananas', '!')
Regular expressions
You could also use a regular expression
import re
m = re.match(r'^([^#]*)#(.*)$', line)
if m: # The line contains a hash / comment
line = m.group(1)
Functional
We can specify the line prefixes to retain in terms of itertools.takewhile, which is defined over strings as well as lists.
'''Comments stripped with itertools.takewhile'''
from itertools import takewhile
# stripComments :: [Char] -> String -> String
def stripComments(cs):
'''The lines of the input text, with any
comments (defined as starting with one
of the characters in cs) stripped out.
'''
def go(cs):
return lambda s: ''.join(
takewhile(lambda c: c not in cs, s)
).strip()
return lambda txt: '\n'.join(map(
go(cs),
txt.splitlines()
))
if __name__ == '__main__':
print(
stripComments(';#')(
'''apples, pears # and bananas
apples, pears ; and bananas
'''
)
)
- Output:
apples, pears apples, pears
QBasic
SUB stripComment (s$, commentMarkers$)
IF s$ = "" THEN RETURN
i = INSTR(s$, commentMarkers$)
IF i > 0 THEN
s$ = LEFT$(s$, i - 1)
s$ = LTRIM$((RTRIM$(s$))) '' removes both leading and trailing whitespace
END IF
END SUB
DIM s$(1 TO 4)
s$(1) = "apples, pears # and bananas"
s$(2) = "apples, pears ; and bananas"
s$(3) = "# this is a comment"
s$(4) = " # this is a comment with leading whitespace"
FOR i = 1 TO 4
CALL stripComment(s$(i), "#;")
PRINT s$(i), " => Length ="; LEN(s$(i))
NEXT i
- Output:
apples, pears # and bananas => Length = 27 apples, pears ; and bananas => Length = 27 # this is a comment => Length = 19 # this is a comment with leading whitespace => Length = 45
Quackery
[ char ; over find split drop
char # over find split drop ] is strip ( $ --> $ )
( or, to pander to the debacle by trimming trailing whitespace:
[ char ; over find split drop
char # over find split drop
reverse trim reverse ] is strip ( $ --> $ )
( or, to pander to the debacle by trimming leading and trailing whitespace:
[ char ; over find split drop
char # over find split drop
trim reverse trim reverse ] is strip ( $ --> $ )
$ "apples, pears # and bananas" strip echo$ cr
$ "apples, pears ; and bananas" strip echo$ cr
- Output:
apples, pears apples, pears
R
This is most cleanly accomplished using the stringr
package.
strip_comments <- function(str)
{
if(!require(stringr)) stop("you need to install the stringr package")
str_trim(str_split_fixed(str, "#|;", 2)[, 1])
}
Example usage:
x <-c(
"apples, pears # and bananas", # the requested hash test
"apples, pears ; and bananas", # the requested semicolon test
"apples, pears and bananas", # without a comment
" apples, pears # and bananas" # with preceding spaces
)
strip_comments(x)
Racket
#lang at-exp racket
(define comment-start-rx "[;#]")
(define text
@~a{apples, pears # and bananas
apples, pears ; and bananas
})
(define (strip-comments text [rx comment-start-rx])
(string-join
(for/list ([line (string-split text "\n")])
(string-trim line (pregexp (~a "\\s*" rx ".*")) #:left? #f))
"\n"))
;; Alternatively, do it in a single regexp operation
(define (strip-comments2 text [rx comment-start-rx])
(regexp-replace* (pregexp (~a "(?m:\\s*" rx ".*)")) text ""))
(strip-comments2 text) ; -> "apples, pears\napples, pears"
Raku
(formerly Perl 6)
$*IN.slurp.subst(/ \h* <[ # ; ]> \N* /, '', :g).print
Red
Red has an embedded parse engine called Parse. For more info on the Parse dialect. http://www.red-lang.org/2013/11/041-introducing-parse.html.
>> parse s: "apples, pears ; and bananas" [to [any space ";"] remove thru end]
== true
>> s
== "apples, pears"
But you can also use simple series operations to find where something occurs, clear from that position, and trim leading and trailing spaces.
s: "apples, pears ; and bananas"
dlms: charset "#;"
trim head clear find s dlms
== "apples, pears"
s: "apples, pears # and bananas"
trim head clear find s dlms
== "apples, pears"
REXX
version 1
The first REXX subroutine takes advantage of the fact that there are only two single-character delimiters:
- # (hash or pound sign),
- ; (a semicolon).
The second and third subroutines take a general approach to the (number of) delimiters,
the third subroutine is more straightforward and reads better.
The fourth subroutine is similar to the third version but more idiomatic.
All four subroutines trim leading and trailing blanks after stripping the "comments".
/*REXX program strips a string delineated by a hash (#) or a semicolon (;). */
old1= ' apples, pears # and bananas' ; say ' old ───►'old1"◄───"
new1= stripCom1(old1) ; say ' 1st version new ───►'new1"◄───"
new2= stripCom2(old1) ; say ' 2nd version new ───►'new2"◄───"
new3= stripCom3(old1) ; say ' 3rd version new ───►'new3"◄───"
new4= stripCom4(old1) ; say ' 4th version new ───►'new4"◄───"
say copies('▒', 62)
old2= ' apples, pears ; and bananas' ; say ' old ───►'old2"◄───"
new1= stripCom1(old2) ; say ' 1st version new ───►'new1"◄───"
new2= stripCom2(old2) ; say ' 2nd version new ───►'new2"◄───"
new3= stripCom3(old2) ; say ' 3rd version new ───►'new3"◄───"
new4= stripCom4(old2) ; say ' 4th version new ───►'new4"◄───"
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
stripCom1: procedure; parse arg x /*obtain the argument (the X string).*/
x=translate(x, '#', ";") /*translate semicolons to a hash (#). */
parse var x x '#' /*parse the X string, ending in hash. */
return strip(x) /*return the stripped shortened string.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
stripCom2: procedure; parse arg x /*obtain the argument (the X string).*/
d= ';#' /*this is the delimiter list to be used*/
d1=left(d,1) /*get the first character in delimiter.*/
x=translate(x,copies(d1,length(d)),d) /*translates delimiters ──► 1st delim.*/
parse var x x (d1) /*parse the string, ending in a hash. */
return strip(x) /*return the stripped shortened string.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
stripCom3: procedure; parse arg x /*obtain the argument (the X string).*/
d= ';#' /*this is the delimiter list to be used*/
do j=1 for length(d) /*process each of the delimiters singly*/
_=substr(d,j,1) /*use only one delimiter at a time. */
parse var x x (_) /*parse the X string for each delim. */
end /*j*/ /* [↑] (_) means stop parsing at _ */
return strip(x) /*return the stripped shortened string.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
stripCom4: procedure; parse arg x /*obtain the argument (the X string).*/
d= ';#' /*this is the delimiter list to be used*/
do k=1 for length(d) /*process each of the delimiters singly*/
p=pos(substr(d,k,1), x) /*see if a delimiter is in the X string*/
if p\==0 then x=left(x,p-1) /*shorten the X string by one character*/
end /*k*/ /* [↑] If p==0, then char wasn't found*/
return strip(x) /*return the stripped shortened string.*/
output
old ───► apples, pears # and bananas◄─── 1st version new ───►apples, pears◄─── 2nd version new ───►apples, pears◄─── 3rd version new ───►apples, pears◄─── 4th version new ───►apples, pears◄─── ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ old ───► apples, pears ; and bananas◄─── 1st version new ───►apples, pears◄─── 2nd version new ───►apples, pears◄─── 3rd version new ───►apples, pears◄─── 4th version new ───►apples, pears◄───
version 2
Call stripd ' apples, pears # and bananas'
Call stripd ' apples, pears and bananas'
Exit
stripd:
Parse Arg old
dlist='#;' /* delimiter list */
p=verify(old,dlist,'M') /* find position of delimiter */
If p>0 Then /* delimiter found */
new=strip(left(old,p-1))
Else
new=strip(old)
Say '>'old'<'
Say '>'new'<'
Return
Ring
aList = 'apples, pears # and bananas'
see aList + nl
see stripComment(aList) + nl
aList = 'apples, pears // and bananas'
see aList + nl
see stripComment(aList) + nl
func stripComment bList
nr = substr(bList,"#")
if nr > 0 cList = substr(bList,1,nr-1) ok
nr = substr(bList,"//")
if nr > 0 cList = substr(bList,1,nr-1) ok
return cList
RPL
In addition to the input string, a second argument defines if whitespaces must be removed (mode=1) or not (mode=0).
RPL code | Comment |
---|---|
≪ IF DUP 1 1 SUB " " == THEN 2 OVER SIZE SUB TRIMF END IF DUP DUP SIZE DUP SUB " " == THEN 1 OVER SIZE 1 - SUB TRIMF END ≫ ‘TRIMF’ STO ≪ → string charset ≪ 0 1 charset SIZE FOR j IF string charset j DUP SUB POS THEN LAST SWAP DROP charset SIZE 'j' STO END NEXT ≫ ≫ 'STPOS' STO ≪ SWAP IF DUP "#;" STPOS THEN 1 LAST 1 - SUB IF SWAP THEN TRIMF END ELSE SWAP DROP END ≫ 'NOCOM' STO |
TRIMF ( "string" -- "trimmed" ) if flag 1 set and 1st char of string is a space then recursively process tail(string) if flag 1 set and last char of string is a space then recursively process head(string) STPOS ( "string" "char_set" -- position ) position = 0, for each character in set if char in string update position, request loop exit NOCOM ( "string" mode -- "no_comment" ) if there is a comment remove it from string, and spaces too if required otherwise clean stack |
- Input:
" apple, pears # and bananas" 0 NOCOM " apple, pears # and bananas" 1 NOCOM " apple, pears ; and bananas" 1 NOCOM
- Output:
3: " apple, pears " 2: "apple, pears" 1: "apple, pears"
Ruby
class String
def strip_comment( markers = ['#',';'] )
re = Regexp.union( markers ) # construct a regular expression which will match any of the markers
if index = (self =~ re)
self[0, index].rstrip # slice the string where the regular expression matches, and return it.
else
rstrip
end
end
end
p 'apples, pears # and bananas'.strip_comment
str = 'apples, pears ; and bananas'
p str.strip_comment
str = 'apples, pears and bananas '
p str.strip_comment
p str.strip_comment('and')
p " \t \n ;".strip_comment
p "".strip_comment
- Output:
"apples, pears" "apples, pears" "apples, pears and bananas" "apples, pears" "" ""
Rust
fn strip_comment<'a>(input: &'a str, markers: &[char]) -> &'a str {
input
.find(markers)
.map(|idx| &input[..idx])
.unwrap_or(input)
.trim()
}
fn main() {
println!("{:?}", strip_comment("apples, pears # and bananas", &['#', ';']));
println!("{:?}", strip_comment("apples, pears ; and bananas", &['#', ';']));
println!("{:?}", strip_comment("apples, pears and bananas ", &['#', ';']));
}
- Output:
"apples, pears" "apples, pears" "apples, pears and bananas"
Scala
object StripComments {
def stripComments1(s:String, markers:String =";#")=s takeWhile (!markers.contains(_)) trim
// using regex and pattern matching
def stripComments2(s:String, markers:String =";#")={
val R=("(.*?)[" + markers + "].*").r
(s match {
case R(line) => line
case _ => s
}) trim
}
def print(s:String)={
println("'"+s+"' =>")
println(" '"+stripComments1(s)+"'")
println(" '"+stripComments2(s)+"'")
}
def main(args: Array[String]): Unit = {
print("apples, pears # and bananas")
print("apples, pears ; and bananas")
}
}
Output:
'apples, pears # and bananas' => 'apples, pears' 'apples, pears' 'apples, pears ; and bananas' => 'apples, pears' 'apples, pears'
Scheme
(use-modules (ice-9 regex))
(define (strip-comments s)
(regexp-substitute #f
(string-match "[ \t\r\n\v\f]*[#;].*" s) 'pre "" 'post))
(display (strip-comments "apples, pears # and bananas"))(newline)
(display (strip-comments "apples, pears ; and bananas"))(newline)
Output:
apples, pears apples, pears
sed
#!/bin/sh
# Strip comments
echo "$1" | sed 's/ *[#;].*$//g' | sed 's/^ *//'
Seed7
$ include "seed7_05.s7i";
const func string: stripComment (in string: line) is func
result
var string: lineWithoutComment is "";
local
var integer: lineEnd is 0;
var integer: pos is 0;
begin
lineEnd := length(line);
for pos range 1 to length(line) do
if line[pos] in {'#', ';'} then
lineEnd := pred(pos);
pos := length(line);
end if;
end for;
lineWithoutComment := line[.. lineEnd];
end func;
const proc: main is func
local
var string: stri is "apples, pears # and bananas\n\
\apples, pears ; and bananas";
var string: line is ""
begin
writeln(stri);
writeln("====>");
for line range split(stri, '\n') do
writeln(stripComment(line));
end for;
end func;
Output:
apples, pears # and bananas apples, pears ; and bananas ====> apples, pears apples, pears
Sidef
func strip_comment(s) {
(s - %r'[#;].*').strip;
}
[" apples, pears # and bananas",
" apples, pears ; and bananas",
" apples, pears "].each { |s|
say strip_comment(s).dump;
};
- Output:
"apples, pears" "apples, pears" "apples, pears"
SparForte
As a structured script.
#!/usr/local/bin/spar
pragma annotate( summary, "stripcomment" )
@( description, "The task is to remove text that follow any of a set of" )
@( description, "comment markers, (in these examples either a hash or a" )
@( description, "semicolon) from a string or input line." )
@( see_also, "http://rosettacode.org/wiki/Strip_comments_from_a_string" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
pragma restriction( no_external_commands );
procedure stripcomment is
line : constant string := get_line;
eol : natural := 0;
ch : character;
begin
for i in 1..strings.length( line ) loop
ch := strings.element( line, i );
exit when ch = '#' or ch = ';';
eol := i;
end loop;
if eol > 0 then
? strings.trim( strings.slice( line, 1, eol ), trim_end.both );
end if;
end stripcomment;
Standard ML
Strips comments and trailing spaces.
val stripComment =
let
val notMarker = fn #"#" => false | #";" => false | _ => true
open Substring
in
string o dropr Char.isSpace o takel notMarker o full
end
Tcl
proc stripLineComments {inputString {commentChars ";#"}} {
# Switch the RE engine into line-respecting mode instead of the default whole-string mode
regsub -all -line "\[$commentChars\].*$" $inputString "" commentStripped
# Now strip the whitespace
regsub -all -line {^[ \t\r]*(.*\S)?[ \t\r]*$} $commentStripped {\1}
}
Demonstration:
# Multi-line string constant
set input "apples, pears # and bananas
apples, pears ; and bananas"
# Do the stripping
puts [stripLineComments $input]
Output:
apples, pears apples, pears
The above code has one issue though; it's notion of a set of characters is very much that of the RE engine. That's possibly desirable, but to handle any sequence of characters as a set of separators requires a bit more cleverness.
proc stripLineComments {inputString {commentChars ";#"}} {
# Convert the character set into a transformation
foreach c [split $commentChars ""] {lappend map $c "\uFFFF"}; # *very* rare character!
# Apply transformation and then use a simpler constant RE to strip
regsub -all -line {\uFFFF.*$} [string map $map $inputString] "" commentStripped
# Now strip the whitespace
regsub -all -line {^[ \t\r]*(.*\S)?[ \t\r]*$} $commentStripped {\1}
}
Output in the example is the same as above.
TUSCRIPT
$$ MODE TUSCRIPT
strngcomment=*
DATA apples, pears # and bananas
DATA apples, pears ; and bananas
BUILD S_TABLE comment_char="|#|;|"
LOOP s=strngcomment
x=SPLIT (s,comment_char,string,comment)
PRINT string
ENDLOOP
Output:
apples, pears apples, pears
UNIX Shell
Adapted from the Advanced Bash-Scripting Guide, section 10.1 Manipulating Strings.
bash$ a='apples, pears ; and bananas'
bash$ b='apples, pears # and bananas'
bash$ echo ${a%%;*}
apples, pears
bash$ echo ${b%%#*}
apples, pears
bash$
VBScript
Function strip_comments(s,char)
If InStr(1,s,char) > 0 Then
arr = Split(s,char)
strip_comments = RTrim(arr(0))
Else
strip_comments = s
End If
End Function
WScript.StdOut.WriteLine strip_comments("apples, pears # and bananas","#")
WScript.StdOut.WriteLine strip_comments("apples, pears ; and bananas",";")
V (Vlang)
const comment_chars = "#;"
fn main() {
s := [
"apples, pears # and bananas",
"apples, pears ; and bananas",
"no bananas"
]
for element in s {
println('source: $element')
println('stripped: ' + strip_comment(element))
}
}
fn strip_comment(source string) string {
if source.index_any(comment_chars) >= 0 {
return source.substr(0, source.index_any(comment_chars))
}
return source
}
- Output:
source: apples, pears # and bananas stripped: apples, pears source: apples, pears ; and bananas stripped: apples, pears source: no bananas stripped: no bananas
Wren
This is based on what the post 29th March, 2011 requirements appear to be.
var markers = ["#", ";"]
var stripComments = Fn.new { |s|
for (marker in markers) {
var t = s.split(marker)
if (t.count > 1) return t[0].trim()
}
return s.trim()
}
var strings = [
" apples, pears # and bananas",
" apples, pears ; and bananas",
" apples, pears \t "
]
for (s in strings) {
var t = stripComments.call(s)
System.print("'%(s)' -> '%(t)'")
}
- Output:
' apples, pears # and bananas' -> 'apples, pears' ' apples, pears ; and bananas' -> 'apples, pears' ' apples, pears ' -> 'apples, pears'
XPL0
string 0; \use zero-terminated strings
func StripComment(Str); \Remove characters after marker and remove
char Str; int I; \ whitespace at beginning and end of string
[I:= 0;
loop case Str(I) of
^#, ^;, 0: quit
other I:= I+1;
while I>0 and Str(I-1)<=$20 do I:= I-1;
Str(I):= 0;
while Str(0)<=$20 do Str:= Str+1;
return Str;
];
int Strings, I;
[Strings:= [
" apples, pears # and bananas",
" apples, pears ; and bananas ",
" apples, pears "];
for I:= 0 to 3-1 do
[ChOut(0, ^");
Text(0, StripComment(Strings(I)));
ChOut(0, ^");
CrLf(0);
];
]
- Output:
"apples, pears" "apples, pears" "apples, pears"
XProfan
Delimiter in string "#" or ";" will be ignored. Good for INI files.
Proc Min
Declare int PC, i, float e, t
PC = %PCount
e = @!(1)
If PC > 1
For i, 2, PC
t = @!(i)
e = if( (e == 0.0) and (t == 0.0), -(-e - t), if(t < e, t, e) )
EndFor
EndIf
Return e
EndProc
Proc Odd
Parameters int n
Return TestBit(n,0)
EndProc
Proc strip_comments
Parameters string s, delim
Declare int posi[]
Declare int i, min_p, p
min_p = $7FFFFFFF
For i, 1, Len(delim)
posi[ i ] = InStr( mid$(delim,i,1), s )
Case posi[ i ] > 0 : min_p = Min( posi[ i ], min_p )
EndFor
posi[ 0 ] = InStr( chr$(34), s )
// if there is a string delimiter on the left side...
If (posi[0] > 0) and (posi[0] < min_p)
// ...and counting of delimiter is odd, then the sign is part of a string
If Odd( Len( Left$(s,min_p) ) - Len( translate$( Left$(s,min_p), Chr$(34), "" )) )
p = posi[ 0 ] + 1
min_p = $7FFFFFFF
Repeat
// closing quote
posi[ 0 ] = InStr( chr$(34), s, p )
'Case posi[0] > 0 : posi[0] = posi[0] + p
p = posi[ 0 ] + 1
// find new positions after that
For i, 1, Len(delim)
posi[ i ] = InStr( mid$(delim,i,1), s, p )
Case posi[ i ] > 0 : min_p = Min( posi[ i ], min_p )
EndFor
posi[ 0 ] = InStr( chr$(34), s, p )
// if there is a string delimiter on the left side...
If (posi[0] > 0) and (posi[0] < min_p)
// ...and counting of delimiter is odd, then the sign is part of a string
If Odd( Len( Left$(s,min_p) ) - Len( translate$( Left$(s,min_p), Chr$(34), "" )) )
p = posi[ 0 ] + 1
min_p = $7FFFFFFF
// and again....
CONTINUE
EndIf
EndIf
BREAK
Until min_p = 0
EndIf
EndIf
Return Trim$( Left$( s, min_p - 1 ) )
EndProc
cls
declare string s, t
s = " apples, pears # and bananas"
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
s = " apples, pears ; and bananas"
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
s = " apples, pears \t "
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
s = " " + chr$(34) + " #oh, my god " + chr$(34) + " apples, pears # and bananas"
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
waitkey
end
Output:
[apples, pears] [apples, pears] [apples, pears] [" #oh, my god " apples, pears]
zkl
fcn strip(text,c){ // if c in text, remove it and following text
if (Void!=(n:=text.find(c))) text=text[0,n];
text.strip() // remove leading and trailing white space
}
fcn stripper(text,a,b,c,etc){ // strip a,b,c,etc from text
foreach c in (vm.arglist[1,*]){ text=strip(text,c) }
text
}
Or, if you want the all-in-one stripper:
fcn stripper(text,a,b,c,etc){
vm.arglist[1,*].reduce('wrap(text,c){
if (Void!=(n:=text.find(c))) text[0,n] else text
},text)
.strip()
}
String(">", strip(" apples, pears # and bananas","#"), "<").println();
String(">", stripper(" apples, pears ; and # bananas","#",";"), "<").println();
- Output:
>apples, pears< >apples, pears<
- Clarify task
- Programming Tasks
- Basic language learning
- String manipulation
- 11l
- 68000 Assembly
- Action!
- Ada
- Aime
- ALGOL 68
- ALGOL W
- Applesoft BASIC
- Arturo
- AutoHotkey
- AutoIt
- AWK
- BASIC
- ANSI BASIC
- BASIC256
- BQN
- Bracmat
- C
- C sharp
- C++
- Clojure
- COBOL
- Common Lisp
- D
- Delphi
- DWScript
- EasyLang
- Erlang
- F Sharp
- Factor
- Fantom
- Forth
- Fortran
- FreeBASIC
- FutureBasic
- Go
- Groovy
- Haskell
- Icon
- Unicon
- Inform 7
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Liberty BASIC
- Lua
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- MiniScript
- Nim
- Objeck
- OCaml
- Oforth
- Pascal
- Perl
- Phix
- PicoLisp
- PL/I
- Prolog
- PureBasic
- Python
- QBasic
- Quackery
- R
- Racket
- Raku
- Red
- REXX
- Ring
- RPL
- Ruby
- Rust
- Scala
- Scheme
- Sed
- Seed7
- Sidef
- SparForte
- Standard ML
- Tcl
- TUSCRIPT
- UNIX Shell
- VBScript
- V (Vlang)
- Wren
- XPL0
- XProfan
- Zkl
- Bc/Omit
- Openscad/Omit
- PARI/GP/Omit