Logo

Programming-Idioms

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

Idiom #167 Trim prefix

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

var t = s.startsWith(p) ? s.replaceFirst(p,"") : s;
var t = s.startsWith(p) ? s.substring(p.length, s.length) : s;
#include <string.h>
size_t l = strlen(p);
const char * t = strncmp(s, p, l) ? s : s + l;
#include <string>
std::string t = s.starts_with(p) ? s.substr(p.size()) : s;
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
let t = s.startsWith(p) ? s.substring(p.length) : s;
int i = s.indexOf(p), n = p.length();
t = i == 0 ? s.substring(n) : s;
String t = s.replaceFirst("^" + p, "");
import static java.util.regex.Pattern.quote;
String t = s.replaceFirst('^' + quote(p), "");
local t = (s:sub(0, #p) == p) and s:sub(#p+1) or s
$t = strpos($s, $p) === 0 ? substr($s, strlen($p)) : $s;
uses StrUtils;
if AnsiStartsStr(p, s) then 
  t := copy(s, length(p)+1, length(s)-length(p))
else
  s := t;
if (0 == index $s, $p) {
    my $t = substr $s, length $p;
}
t = s[s.startswith(p) and len(p):]
t = s.removeprefix(p)
t = s.delete_prefix(p)
t = s.sub(/\A#{p}/, "")
let t = s.strip_prefix(p).unwrap_or(s);
let t = if s.starts_with(p) { &s[p.len()..] } else { s };
let t = s.trim_start_matches(p);

New implementation...