Dart将整型变量转换为字符串

80

我正在尝试转换一个整型变量。

var int counter = 0;

转换成字符串变量

var String $counter = "0";

我进行了搜索,但只找到了类似于

var myInt = int.parse('12345');

无法使用

var myInt = int.parse(counter);
3个回答

121

使用 toString 和/或 toRadixString

  int intValue = 1;
  String stringValue = intValue.toString();
  String hexValue = intValue.toRadixString(16);

或者,就像评论中所说的那样

  String anotherValue = 'the value is $intValue';

2
您还可以使用字符串插值来进行转换,这样会更加简洁:String stringValue = '$intValue'; - greyaurora

26

// 字符串转整型

String s = "45";
int i = int.parse(s);

// 将int转换为字符串

int j = 45;
String t = "$j";

// 如果后者看起来很奇怪,请查阅https://dart.dev上的字符串插值。


13

你可以在int类中使用.toString()函数。

int age = 23;
String tempAge = age.toString();

那么您可以简单地将整数转换为字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接