Substring/Top and tail: Difference between revisions

Content added Content deleted
Line 1,083: Line 1,083:


=={{header|Java}}==
=={{header|Java}}==
I solve this problem two ways. First I use substring which is relatively fast for small strings, since it simply grabs the characters within a set of given bounds. The second uses regular expressions, which have a higher overhead for such short strings.
I solve this problem two ways. First I use substring which is relatively fast for small strings, since it simply grabs the characters within a set of given bounds. The second uses regular expressions, which have a higher overhead for such short strings, but work correctly with all Unicode code points, not just those in the Basic Multilingual Plane.


<syntaxhighlight lang="java">public class RM_chars {
<syntaxhighlight lang="java">public class RM_chars {
Line 1,108: Line 1,108:
room</pre>
room</pre>


Nearly all current solutions for this task fail to work correctly: the task says "The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16." The above program, as most on this page, works only with Unicode code points in the Basic Multilingual Plane. The code below, on the other hand, works correctly with all Unicode characters.
Nearly all current solutions for this task fail to work correctly: the task says "The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16." The code below works correctly with all Unicode characters, without using regular expressions as the above program does.


<syntaxhighlight lang="java">public class SubstringTopAndTail {
<syntaxhighlight lang="java">public class SubstringTopAndTail {