Logo

Programming-Idioms

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

Idiom #167 Trim prefix

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

t = s.sub(/\A#{p}/, "")

Like Strings, Regular Expressions support interpolation. \A means : beginning of string
t = s.delete_prefix(p)

available since Ruby 2.5.0
#include <string.h>
size_t l = strlen(p);
const char * t = strncmp(s, p, l) ? s : s + l;

strlen computes the prefix length and strncmp returns zero if the first l characters match

New implementation...