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

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Illustration
T : String := S (1 .. 5);
#include <stdlib.h>
#include <string.h>
char *t=malloc(6*sizeof(char));
strncpy(t,s,5);
(take 5 "abcdefjhi")
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:sub_string(S, 1, 5),
T = string:substr(S, 1, 5).
t := string([]rune(s)[:5])
t :: String
t = take 5 s
import qualified Data.Text as T
t :: T.Text
t = T.take 5 s
var t = s.substring(0,5);
String t = s.substring(0,5);
t = s:sub(5)
$t = mb_substr($s, 0, 5, 'UTF-8');
_t := Copy(_s, 1, 5);
$t = substr($s, 0, 5);
t = s[:5]
t = s[0, 5]
# OR
t = s.slice(0, 5)
t = s.slice(0...5)
let t=s[..5].to_string();
1
val s = "somestring"
val t = s.take(5)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms