由String 转换成基本数据型态(char、char[]、boolean等)

要将 String 转换成基本数据型态转,大多需要使用基本数据型态的包装类别 ,具体有以下几种:

(1)byte: Byte.parseByte(String s) : 将 s 转换成 byte

(2)Byte.parseByte(Strings, int radix) : 以 radix 为基底 将 s 转换为 byte ,比如说 Byte.parseByte("11",16) 会得到 17

(3)double: Double.parseDouble(String s) : 将 s 转换成 double

(4)float: Float.parseFloat(String s) : 将 s 转换成 float

(5)int: Integer.parseInt(String s) : 将 s 转换成 int

(6)long: Long.parseLong(String s)

用法示例代码如下:

public class Demo {
    public static void main(String[] args) {
        String str;

        //(1)byte : Byte.parseByte(String s) : 将 s 转换成 byte
        // byte存值范围: -128 - 127,
        // 因此当str的形式不在byte的取值范围内容时(如:128、a等内容),
        // 程序将抛出NumberFormatException异常
        str = "-127";
        Byte resultB = Byte.parseByte(str);
        System.out.println(resultB);

        //(2)Byte.parseByte(String s, int radix) : 以 radix 为基底 将 s 转换为 byte ,比如说 Byte.parseByte("11", 16) 会得到 17
        // byte存值范围: -128 - 127,
        // 因此当str的形式中含有不是radix进制的数码(如十进制的数码有0-9)或转换后的值超出byte的范围(如:radix为10时,str中含有a或者为“129”),
        // 程序将抛出NumberFormatException异常。
        str = "11";
        resultB = Byte.parseByte(str, 16);
        System.out.println(resultB);

        //(3)double : Double.parseDouble(String s) : 将 s 转换成 double
        // str中内容不是Double型数据时(如:含有字符a),抛出异常
        str = "1.23";
        double resultD = Double.parseDouble(str);
        System.out.println(resultD);

        //(4)float : Float.parseFloat(String s) : 将 s 转换成 float
        // str中内容不是float型数据时(如:含有字符a),抛出异常
        str = "4.56";
        float resultF = Float.parseFloat(str);
        System.out.println(resultF);

        //(5)int : Integer.parseInt(String s) : 将 s 转换成 int
        // str中内容不是int型数据时(如:含有字符a),抛出异常
        str = "789";
        int resulti = Integer.parseInt(str);
        System.out.println(resulti);

        //(6)long : Long.parseLong(String s)
        // str中内容不是long型数据时(如:含有字符a),抛出异常
        str = "999";
        long resultl = Integer.parseInt(str);
        System.out.println(resultl);
    }
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中提供了以下方法可以进行类型之间的转换: 1. 将基本数据类型转换为字符串: ```java byte b = 1; String strByte = Byte.toString(b); // byte 转换为字符串 short s = 2; String strShort = Short.toString(s); // short 转换为字符串 int i = 3; String strInt = Integer.toString(i); // int 转换为字符串 long l = 4L; String strLong = Long.toString(l); // long 转换为字符串 float f = 5.0f; String strFloat = Float.toString(f); // float 转换为字符串 double d = 6.0; String strDouble = Double.toString(d); // double 转换为字符串 char c = 'a'; String strChar = Character.toString(c); // char 转换为字符串 boolean bool = true; String strBool = Boolean.toString(bool); // boolean 转换为字符串 ``` 2. 将字符串转换为基本数据类型: ```java String str = "123"; byte b = Byte.parseByte(str); // 字符串转换为 byte short s = Short.parseShort(str); // 字符串转换为 short int i = Integer.parseInt(str); // 字符串转换为 int long l = Long.parseLong(str); // 字符串转换为 long float f = Float.parseFloat(str); // 字符串转换为 float double d = Double.parseDouble(str); // 字符串转换为 double char c = str.charAt(0); // 字符串转换为 char boolean bool = Boolean.parseBoolean(str); // 字符串转换为 boolean ``` 需要注意的是,在进行字符串转换成基本数据类型的时候,如果字符串的格式不符合相应数据类型的规范,会抛出NumberFormatException异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值