在Flutter中修剪字符串/文本

时间:2019-09-17 16:08:34

标签: regex flutter dart

嗨,我试图修剪链接中的链接

当前我正在研究正则表达式,但我认为这不可能

这是完整的链接:

http://sales.local/api/v1/payments/454/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH

我想要做的是像这样修剪链接:

http://sales.local/api/v1/payments/454

请最好地实践一下,以减少颤动中的字符串/文本。谢谢!

3 个答案:

答案 0 :(得分:0)

尝试使用substring()

  String link = 'http://sales.local/api/v1/payments/454/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH';
  String delimiter = '/ticket';
  int lastIndex = link.indexOf(delimiter);
  String trimmed = link.substring(0,lastIndex);
  //print(trimmed);

答案 1 :(得分:0)

    input string print for Flutter: 

       String str2 = "-hello Friend- "; 
    print(str2.trim()); 



    Output Print : -hello Friend- 

NOte: Here last space remove from string.
    1.Right Method: 

    var str1 = 'Dart';
    var str2 = str1.trim();
    identical(str1, str2);

    2.Wrong Method
    '\tTest String is Fun\n'.trim(); // 'Test String is Fun'

答案 2 :(得分:0)

main(List<String> args) {
  String str =
      'http://sales.local/api/v2/paymentsss/45444/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH';

  RegExp exp = new RegExp(r"((http|https)://sales.local/api/v\d+/\w.*?/\d*)");
  String matches = exp.stringMatch(str);
  print(matches); // http://sales.local/api/v2/paymentsss/45444
}