• Java String,char,int类型的相互转换


    1. int转string(三种方法)

    1. public static void main(String[] args) {
    2. //int -> string
    3. int testInt = 10;
    4. String testString1 = testInt + "";
    5. System.out.println("string1: " +testString1);
    6. String testString2 = String.valueOf(testInt);
    7. System.out.println("string2: " +testString2);
    8. String testString3 = Integer.toString(testInt);
    9. System.out.println("string3: " +testString3);
    10. }

    输出结果:

    string1:  10
    string2:  10
    string3:  10

    2.0~9的int数字转为char(两种方法)

    1. public static void main(String[] args) {
    2. //int -> char
    3. int testInt = 4;
    4. char testChar1 = (char) (testInt + '0');
    5. System.out.println(testChar1);
    6. //代表10进制
    7. int radix = 10;
    8. char testChar2 = Character.forDigit(testInt, radix);
    9. System.out.println(testChar2);
    10. }

    输出结果:

    4
    4

     

    3.string转int的方法(两种方法)

    1. public static void main(String[] args) {
    2. //string -> int
    3. String testString = "520";
    4. int testInt1 = Integer.parseInt(testString);
    5. System.out.println("int1: " +testInt1);
    6. int testInt2 = Integer.valueOf(testString);
    7. System.out.println("int2: " +testInt2);
    8. }

    输出结果为:

    int1:  520
    int2:  520

    4.string转char的方法

    1. public static void main(String[] args) {
    2. // string -> char
    3. String testString = "abcdefg";
    4. //获取某个下标的字符元素
    5. char ch = testString.charAt(0);
    6. System.out.println("ch: " + ch);
    7. //string转 char array
    8. char chArray[] = testString.toCharArray();
    9. System.out.println("ch: " + chArray[0]);
    10. }

    输出结果为:

    ch:   a
    ch:   a

    5. char转int (两种方法)

    1. public static void main(String[] args) {
    2. // char转 int
    3. char ch = '9';
    4. int testInt1 = Character.getNumericValue(ch);
    5. int testInt2 = (int)ch - (int)'0';
    6. System.out.println("int: "+ testInt1);
    7. System.out.println("int: "+ testInt2);
    8. }

    输出结果:

    int:    9
    int:    9

    6.char转string方法(四种方法)

    1. public static void main(String[] args) {
    2. // char转 string
    3. char ch = 's';
    4. String testString1 = String.valueOf(ch);
    5. System.out.println("string: "+ testString1);
    6. String testString2 = ch + "";
    7. System.out.println("string: "+ testString2);
    8. String testString3 = Character.toString(ch);
    9. System.out.println("string: "+ testString3);
    10. String testString4= String.valueOf(new char[]{'j', 'a', 'v', 'a'});
    11. System.out.println("string: "+ testString4);
    12. }

    输出结果为:

    string:    s
    string:    s
    string:    s
    string:    java

  • 相关阅读:
    HPLC电力载波灯控的节能照明 智慧照明方案
    蓝桥杯每日一题2023.9.16
    springboot系列(二十一):基于AOP实现自定义注解且记录接口日志|超级超级详细,建议收藏
    带你了解MySQL数据库(六)
    【数组】-找出有序数组中(有负有正)绝对值最小的数
    Ruoyi若依前后端一体项目整合cas单点登录
    mysql的基础操作语句
    【自动化测试】Appium环境搭建与配置-详细步骤,一篇带你打通...
    mobileFX_WebKitX_CEF3_ActiveX_x86_v6.0.0.15802
    【Opencv】-----倾斜图片转正
  • 原文地址:https://blog.csdn.net/sinat_38079265/article/details/125466042