Strip comments from a string: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 6 users not shown)
Line 494:
}</syntaxhighlight>
 
=={{header|ANSI BASIC}}==
==={{header|ANSI BASIC}}===
<syntaxhighlight lang="ansibasic">100 DECLARE EXTERNAL FUNCTION FNstripcomment$
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">100 DECLARE EXTERNAL FUNCTION FNstripcomment$
110 LET marker$="#;"
120 PRINT """";FNstripcomment$("apples, pears # and bananas", marker$);""""
Line 515 ⟶ 517:
" apples, pears"</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">arraybase 1
Line 777 ⟶ 779:
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}}==
Line 1,689 ⟶ 1,718:
# 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}}==
Line 1,859 ⟶ 1,915:
return cList
</syntaxhighlight>
 
=={{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}}==
Line 2,019 ⟶ 2,136:
"apples, pears"
</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}}==
Line 2,138 ⟶ 2,283:
=={{header|Wren}}==
This is based on what the post 29th March, 2011 requirements appear to be.
<syntaxhighlight lang="ecmascriptwren">var markers = ["#", ";"]
 
var stripComments = Fn.new { |s|
Line 2,164 ⟶ 2,309:
' 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>
 
9,485

edits