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
 character(len=5) :: t
 t = s(1:5)
T : String := S (1 .. 5);
#include <stdlib.h>
#include <string.h>
char *t=(char *)calloc(6,sizeof(char));
strncpy(t,s,5);
(take 5 "abcdefjhi")
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)
T = string:sub_string(S, 1, 5),
T = string:substr(S, 1, 5).
[A, B, C, D, E | _] = S,
T = [A, B, C, D, E].
t := string([]rune(s)[:5])
import qualified Data.Text as T
t :: T.Text
t = T.take 5 s
t :: String
t = take 5 s
var 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(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();
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