Logo

Programming-Idioms

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

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?
bool b = s.StartsWith(prefix);
with Ada.Strings.Fixed;
B := Ada.Strings.Fixed.Index (S, Prefix) = S'First;
#include <stdbool.h>
#include <string.h>
bool b = !strncmp(s, prefix, sizeof(prefix)-1);
(require '[clojure.string :refer [starts-with?]])
(def b (starts-with? s prefix))
IDENTIFICATION DIVISION.
PROGRAM-ID. prefix.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BOOLEAN-B     PIC X.
   88 b-TRUE       VALUE "T".
   88 b-FALSE      VALUE "F".
PROCEDURE DIVISION.
    IF s(1:3) = prefix(1:3)
       SET b-TRUE  TO TRUE
    ELSE
       SET b-FALSE TO TRUE
    END-IF 	 	
STOP RUN.
#include <string>
std::string prefix = "something";
bool b = s.compare(0, prefix.size(), prefix) == 0;
#include <string>
bool b = s.starts_with(prefix);
import std.algorithm.searching;
b = s.startsWith(prefix);
var b = s.startsWith(prefix);
String.starts_with?(s,prefix)
prefix([], _) -> true;
prefix([Ch | Rest1], [Ch | Rest2]) ->
        prefix(Rest1, Rest2);
prefix(_, _) -> false.

prefix("abc", "abcdef").
B = string:find(S, Prefix) =:= S.
  logical :: b
  b = index (string, prefix) == 1
import "strings"
b := strings.HasPrefix(s, prefix)
import Data.List
b = prefix `isPrefixOf` s
var b = (s.lastIndexOf(prefix, 0) === 0);
var b = s.startsWith(prefix);
boolean b = s.startsWith(prefix);
val b = s.startsWith(prefix)
(defun check-prefix (s prefix)
 (cond ((> (length prefix) (length s)) Nil)
       ( T ( equal prefix (subseq s 0 (length prefix ))))))

(setf b (check-prefix s prefix))
b = s:find(prefix, 1, true) == 1
b = string.sub(s, 1, #prefix) == prefix
function startswith(text, prefix)
    return text:find(prefix, 1, true) == 1
end

b = startswith(s, prefix)
@import Foundation;
BOOL b=[s hasPrefix:prefix];
$b = (bool) preg_match('#^http#', $s);
b := pos(prefix, s) = 1;
$b = $prefix eq substr($s,0,length($prefix));
b = s.startswith(prefix)
b = s.start_with?(prefix)
let b = s.starts_with(prefix);
val b = s.startsWith(prefix)
b := s beginsWith: prefix
Dim b As Boolean = s.StartsWith(prefix)

New implementation...