RosettaCodeData/Task/Strip-comments-from-a-string/Julia/strip-comments-from-a-string.julia
2023-07-01 13:44:08 -04:00

22 lines
493 B
Text

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