Programming-Idioms

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

Idiom #62 Find substring position

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

import std.algorithm;
auto i = x.countUntil(y);
#include <string.h>
int i=(int)(x-strstr(x,y));
#include <string>
int i = x.find(y);
var i = x.IndexOf(y);
var i = x.indexOf(y);
I=index(x,y)
import "strings"
i := strings.Index(x, y)
import Data.List
Just i <- (y `isPrefixOf`) `findIndex` (tails x)
let i = x.indexOf(y);
int i = x.indexOf(y);
i = x:find(y)
$i = mb_strpos($x, $y, 0, 'UTF-8');
i := Pos(y, x);
$i = index($x, $y);
i = x.find(y)
i = x.index(y)
let i = x.find(y).unwrap();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms