Finding strings which contain a given substring
Two additional methods (both faster than Select[strings, StringContainsQ["101"]]
):
strings = Map[StringJoin, Tuples[{"0", "1"}, 6]];
Pick[strings, StringContainsQ["101"] @ strings]
{"000101", "001010", "001011", "001101", "010100", "010101", "010110", "010111", "011010", "011011", "011101", "100101", "101000", "101001", "101010", "101011", "101100", "101101", "101110", "101111", "110100", "110101", "110110", "110111", "111010", "111011", "111101"}
A slower alternative:
Pick[strings, StringMatchQ["*101*"]@strings]
same result
Two different methods:
strings = Map[StringJoin, Tuples[{"0", "1"}, 6]];
Flatten @ StringCases[
strings,
StartOfString ~~ ___ ~~ "101" ~~ ___ ~~ EndOfString
]
Select[strings, StringContainsQ["101"]]
The Select
variant is slightly faster for large datasets.