Logo

Programming-Idioms

  • Pascal
  • Rust
  • Java
  • Fortran
  • D

Idiom #62 Find substring position

Set i to the first position of string y inside string x, if exists.

Specify if i should be regarded as a character index or as a byte index.

Explain the behavior when y is not contained in x.

import std.algorithm;
auto i = x.countUntil(y);

countUntil returns -1 if y is not in x.
i := Pos(y, x);

i will be 0 (zero) if y is not a substring of x. the Pos() function is case sensitive.
let i = x.find(y);

i is an Option<usize>.
Finds the byte index of y in x (not the character index).
int i = x.indexOf(y);

i will be -1 if the substring was not found in the string.
I=index(x,y)
#include <string.h>
int i=(int)(x-strstr(x,y));

New implementation...