strstr function equivalent in Dart

329

Here is a Dart equivalent for PHP's strstr :

String strstr(String myString, String pattern, {bool before = false}) {
  var index = myString.indexOf(pattern);
  if (index < 0) return null;
  if (before) return myString.substring(0, index);
  return myString.substring(index + pattern.length);
}

Output :

strstr('[email protected]', '@');               // example.com
strstr('[email protected]', '@', before: true); // name

strstr('path/to/smthng', '/');                 // to/smthng
strstr('path/to/smthng', '/', before: true);   // path
Share:
329
Mathe Eliel
Author by

Mathe Eliel

Updated on December 17, 2022

Comments

  • Mathe Eliel
    Mathe Eliel over 1 year

    Is there a strstr() function equivalent in Dart? I have used strstr() in PHP and I would like to use it in Dart.

    Thank you.