如何格式化插入字符串

编程入门 行业动态 更新时间:2024-10-23 23:23:26
本文介绍了如何格式化插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要格式字符串,例如秒前发送%d ,哈利喜欢%s,我认为%1 $ s喜欢%2 $ s

I need format string like "Send %d seconds ago", "Harry like %s", "I think %1$s like %2$s". These can be implemented in Android, but i don't how to implement in Dart of Flutter.

推荐答案

Dart支持字符串插值

Dart supports string interpolation

var seconds = 5; print("Send $seconds seconds ago"); var harryLikes = 'Silvia'; var otherName = 'Erik'; var otherLikes = 'Chess'; print("Harry like $harryLikes"); print("I think $otherName like $otherLikes");

也可以使用 $ {...}

Also more complex expressions can be embedded with ${...}

print('Calc 3 + 5 = ${3 + 5}');

数字类型和 intl 软件包提供格式化数字和日期的更多方法。

The number types and the intl package provide more methods to format numbers and dates.

例如参见

  • www.dartdocs/documentation/intl/latest/ intl / NumberFormat-class.html
  • dart的货币格式
  • www.dartdocs/documentation/intl/latest/intl/NumberFormat-class.html
  • Currency format in dart

更多推荐

如何格式化插入字符串

本文发布于:2023-11-16 17:05:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!