Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #46 Extract beginning of string (prefix)

Create the string t consisting of the 5 first characters of the string s.
Make sure that multibyte characters are properly handled.

T : String := S (1 .. 5);
(def t (apply str (take 5 s)))
IDENTIFICATION DIVISION.
PROGRAM-ID. prefix 5.
PROCEDURE DIVISION.
    MOVE s(1:5) TO t 	
STOP RUN.
auto t = s.substr(0, 5);
string t = s.Substring(0, 5);
string t = s[0..5];
var t = s.substring(0, 5);
t = String.slice(s, 0, 5)
[A, B, C, D, E | _] = S,
T = [A, B, C, D, E].
T = string:slice(S, 0, 5).
 character(len=5) :: t
 t = s(1:5)
t := string([]rune(s)[:5])
def t = s.take(5)
def t = s[0..<5]
import qualified Data.Text as T
t :: T.Text
t = T.take 5 s
t :: String
t = take 5 s
let t = s.substring(0,5);
String t = s.substring(0,5);
val t = s.take(5)
(setf *t* (subseq s 0 5))
t = s:sub(1,5)
@import Foundation;
NSString *t=[s substringToIndex:5];
$t = mb_substr($s, 0, 5, 'UTF-8');
_t := Copy(_s, 1, 5);
my $t = substr($s,0,5);
t = s[:5]
t = s[0, 5]
t = s.slice(0, 5)
t = s.slice(0...5)
let t = s.char_indices().nth(5).map_or(s, |(i, _)| &s[..i]);
let t = s.chars().take(5).collect::<String>();
val t = s.take(5)
s first: 5

New implementation...