Substring/Top and tail: Difference between revisions

m (→‎{{header|Euphoria}}: blank line removed)
Line 94:
}: }. 'brooms' NB. drop first and last items
room</lang>
 
=={{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.
 
<lang Java>public class RM_chars {
public static void main( String[] args ){
System.out.println( "knight".substring( 1 ) );
System.out.println( "socks".substring( 0, 4 ) );
System.out.println( "brooms".substring( 1, 5 ) );
// first, do this by selecting a specific substring
// to exclude the first and last characters
System.out.println( "knight".replaceAll( "^.", "" ) );
System.out.println( "socks".replaceAll( ".$", "" ) );
System.out.println( "brooms".replaceAll( "^.|.$", "" ) );
// then do this using a regular expressions
}
}</lang>
 
Results:
<pre>night
sock
room
night
sock
room</pre>
 
=={{header|JavaScript}}==
Anonymous user