Anonymous user
Strip comments from a string: Difference between revisions
→{{header|Julia}}
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 1,015:
<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.
<lang Julia>
using Printf
function striplinecomment{T<:String,U<:String}(a::T, cchars::U="#;")▼
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 & bananas ",
" # "
for t in tests
|