Logo

Programming-Idioms

Find substring t consisting in characters i (included) to j (excluded) of string s.
Character indices start at 0 unless specified otherwise.
Make sure that multibyte characters are properly handled.
Implementation
Erlang

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Erlang implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
String t = s.substring(i,j);
t = s[i..j-1] 
t = s[i:j]
var t = s.substring(i, j);
my $chunk = substr("now is the time", $i, $j);
let t = s.substring(i, j);
$t = mb_substr($s, $i, $j-$i, 'UTF-8');
#include <stdlib.h>
#include <string.h>
char *t=malloc((j-i+1)*sizeof(char));
strncpy(t,s+i,j-i);
auto t = s[i .. j];
t := copy(s,i+1,j-i);
extern crate unicode_segmentation;
use unicode_segmentation::UnicodeSegmentation;
let t = s.graphemes(true).skip(i).take(j - i).collect::<String>();
t = drop i (take j s)
t = String.slice(s, i..j-1)
t := string([]rune(s)[i:j])
#include <string>
auto t = s.substr(i, j-i);
var t = s.Substring(i, j - i);
T : String := S (I .. J - 1);
uses Sysutils;
t := s.Substring(i,j-i);
(setf u (subseq s i j))
character(len=:), allocatable :: t

  t = s(i:j-1)
(def t (subs s i j))
Dim t As String = s.Substring(i,j)
let t = s.slice(i, j);
def t = s[i..<j]
@import Foundation;
t=[s substringWithRange:NSMakeRange(i,j-i)]
use substring::Substring;
let t = s.substring(i, j);
(define t (substring s i j))
val t = s.substring(i, j)
t := s copyFrom: i to: j - 1.
local t = s:sub(i, j - 1)
val t = s.substring(i, j)