【问题标题】:strstr function equivalent in DartDart 中等效的 strstr 函数
【发布时间】:2020-05-09 13:58:42
【问题描述】:

在 Dart 中是否有等效的 strstr() 函数? 我在 PHP 中使用了strstr(),我想在 Dart 中使用它。

谢谢。

【问题讨论】:

    标签: string flutter dart flutter-web


    【解决方案1】:

    这是 PHP's strstr 的 Dart 等效项:

    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);
    }
    

    输出:

    strstr('name@example.com', '@');               // example.com
    strstr('name@example.com', '@', before: true); // name
    
    strstr('path/to/smthng', '/');                 // to/smthng
    strstr('path/to/smthng', '/', before: true);   // path
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-30
      • 2013-05-18
      • 2013-05-30
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      相关资源
      最近更新 更多