Logo

Programming-Idioms

  • Go
  • C++
  • Fortran
  • Python
  • Clojure

Idiom #96 Check string prefix

Set the boolean b to true if string s starts with prefix prefix, false otherwise.

Are the first characters of s equal to this prefix?
(require '[clojure.string :refer [starts-with?]])
(def b (starts-with? s prefix))

Uses java.lang.String.startsWith()
import "strings"
b := strings.HasPrefix(s, prefix)
#include <string>
bool b = s.starts_with(prefix);

C++20
#include <string>
std::string prefix = "something";
bool b = s.compare(0, prefix.size(), prefix) == 0;

empty prefix is true
  logical :: b
  b = index (string, prefix) == 1
b = s.startswith(prefix)
with Ada.Strings.Fixed;
B := Ada.Strings.Fixed.Index (S, Prefix) = S'First;

New implementation...