Strip comments from a string: Difference between revisions

Strip comments from a string in QBASIC
No edit summary
(Strip comments from a string in QBASIC)
Line 1,631:
<pre>apples, pears
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|R}}==
2,169

edits