Reputation: 738
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.
Upvotes: 2
Views: 598
Reputation: 7819
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
Upvotes: 3