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

Idiom #167 Trim prefix

Create string t consisting of string s with its prefix p removed (if s starts with p).

uses StrUtils;
if AnsiStartsStr(p, s) then 
  t := copy(s, length(p)+1, length(s)-length(p))
else
  s := t;
#include <string.h>
size_t l = strlen(p);
const char * t = strncmp(s, p, l) ? s : s + l;
var t = s.TrimStart(p);
import std.string;
string t = s.chompPrefix(p);
  if (index(s,p) == 1) then
     t = s(len(p)+1:)
  else
     t = s
  end if
import "strings"
t := strings.TrimPrefix(s, p)
import qualified Data.Maybe as Maybe
import qualified Data.List as List
t = Maybe.fromMaybe s $ List.stripPrefix p s
if (s.indexOf(p) === 0) {
    t = s.substring(p.length);
}
$t = strpos($s, $p) === 0 ? substr($s, strlen($p)) : $s;
if (0 == index $s, $p) {
    my $t = substr $s, length $p;
}
t = s.lstrip(p)
t = s[s.startswith(p) and len(p):]
t = s.sub(/\A#{p}/, "")
t = s.delete_prefix(p)
let t = s.trim_left_matches(p);

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

Idiom created by

programming-idioms.org

Related idioms