Strip comments from a string: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 212: Line 212:
apples, pears,
apples, pears,
</pre>
</pre>



=={{header|AutoIt}}==
=={{header|AutoIt}}==
Line 419: Line 418:
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#}}==

<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();
}
</lang>
Sample output:
<pre>
Console.WriteLine(RemoveComments("apples, pears # and bananas", "#"));
Console.WriteLine(RemoveComments("apples, pears ; and bananas", ";"));
apples, pears
apples, pears
</pre>
</pre>


Line 450: Line 470:
apples, pears
apples, pears
apples, pears ; and bananas
apples, pears ; and bananas
apples, pears
</pre>

=={{header|C sharp|C#}}==

<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();
}
</lang>
Sample output:
<pre>
Console.WriteLine(RemoveComments("apples, pears # and bananas", "#"));
Console.WriteLine(RemoveComments("apples, pears ; and bananas", ";"));
apples, pears
apples, pears
apples, pears
</pre>
</pre>
Line 885: Line 884:
}
}
}</lang>
}</lang>



=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 1,262: Line 1,260:
print
print
}</lang>
}</lang>

=={{header|Perl 6}}==
<lang perl6>$*IN.slurp.subst(/ \h* <[ # ; ]> \N* /, '', :g).print</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,475: Line 1,470:
(strip-comments2 text) ; -> "apples, pears\napples, pears"
(strip-comments2 text) ; -> "apples, pears\napples, pears"
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>$*IN.slurp.subst(/ \h* <[ # ; ]> \N* /, '', :g).print</lang>


=={{header|Red}}==
=={{header|Red}}==