Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format an interploated String

Tags:

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.

like image 825
H3c Avatar asked Jun 26 '18 09:06

H3c


People also ask

How do you use string interpolation?

As the colon (":") has special meaning in an interpolation expression item, to use a conditional operator in an interpolation expression, enclose that expression in parentheses. string name = "Horace"; int age = 34; Console. WriteLine($"He asked, \"Is your name {name}?\ ", but didn't wait for a reply :-{{"); Console.

Which is correct syntax for string interpolation?

Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.

What is interpolation string?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

How do you add in string interpolation?

An interpolated verbatim string starts with the $ character followed by the @ character. Starting with C# 8.0, you can use the $ and @ tokens in any order: both $@"..." and @$"..." are valid interpolated verbatim strings. To include a brace, "{" or "}", in a result string, use two braces, "{{" or "}}".


1 Answers

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

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

See for example:

  • https://www.dartdocs.org/documentation/intl/latest/intl/NumberFormat-class.html
  • Currency format in dart
like image 79
Günter Zöchbauer Avatar answered Sep 29 '22 18:09

Günter Zöchbauer