如何格式化插入字符串 [英] How to format an interploated String
本文介绍了如何格式化插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要格式字符串,例如秒前发送%d
,哈利喜欢%s,我认为%1 $ s喜欢%2 $ s 。这些可以在Android中实现,但是我不怎么在Flutter的Dart中实现。
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");
也可以使用 $ {...} <嵌入更复杂的表达式/ code>
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.
例如参见
- < a href = https://www.dartdocs.org/documentation/intl/latest/intl/NumberFormat-class.html rel = noreferrer> https://www.dartdocs.org/documentation/intl/latest/ intl / NumberFormat-class.html
- dart的货币格式
- https://www.dartdocs.org/documentation/intl/latest/intl/NumberFormat-class.html
- Currency format in dart
这篇关于如何格式化插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文