【问题标题】:Dart convert int variable to stringDart 将 int 变量转换为字符串
【发布时间】:2019-07-24 01:12:16
【问题描述】:

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

var int counter = 0;

转换成字符串变量

var String $counter = "0";

我搜索了,但我只找到了类似的东西

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

不适合

var myInt = int.parse(counter);

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    使用toString 和/或toRadixString

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

    或者,如评论中所述

      String anotherValue = 'the value is $intValue';
    

    【讨论】:

    • 也可以使用字符串插值来转换,简洁一点:String stringValue = '$intValue';
    【解决方案2】:

    // 字符串转整数

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

    // 整数转字符串

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:
        int i=10;
        String st = i.toString(); // Converted to String
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-27
          • 2021-10-06
          • 1970-01-01
          • 1970-01-01
          • 2019-05-21
          • 1970-01-01
          • 1970-01-01
          • 2021-12-10
          相关资源
          最近更新 更多