Logo

Programming-Idioms

  • JS
  • Kotlin

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?
val b = s.startsWith(prefix)
var b = (s.lastIndexOf(prefix, 0) === 0);

Note the second parameter to lastIndexOf. This is not, however, the most readable possible code.
var b = s.startsWith(prefix);

ECMAScript 6 and above.
with Ada.Strings.Fixed;
B := Ada.Strings.Fixed.Index (S, Prefix) = S'First;

New implementation...