{

How to print dollar symbol in string dart?| Flutter By Example


print dollar symbol in string  in Dart

You can append any string with another string using the plus operator or interpolation syntax.

Similarly, You can add a dollar symbol to a string in multiple ways.

How to print the dollar symbol in Dart and Flutter?

There are multiple ways The string contains normal text or raw text which is interpreted with interpolation syntax.

interpolation syntax uses the $ symbol to print the variable value. For example, if you interpolate syntax, You can use $price to print the variable value. if we append $ to an interpolation variable, It throws an error Error: A ‘$’ has a special meaning inside a string, and must be followed by an identifier or an expression in curly braces ({}).

The below code throws an error.

void main() {
  int price = 95;

  print("Current price is $$price");
}

One way is you need to escape the $ symbol as \$

void main() {
  int price = 95;

  print("Current price is \$$price");
}

Another way using the raw string that starts with r

void main() {
  int price = 95;

  print(r"Current price is $price");
}

Conclusion

Learned Since raw strings have a special meaning for the $ dollar symbol. To print a dollar symbol, We need to escape it.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart