java中的数据格式转换

1整型数据——》字符型

1)如果是按ASCII码形式进行转换的,且转化结果只是单个字符,

int d = 97; 
char e = (char)d;

2)如果是按ASCII码形式进行转换的,且转化结果是多个字符字符串,前提应将整型数据保存在byte型的数组中,即

byte[] b ={97, 98, 99};
System.out.println(new String(b));

3)如果是按整型数据本身进行转化,即123——》"123",

String num = Integer.toString(int n);

2浮点型——>字符串

String num = Float.toString(Float n);

3数组——》字符串

1)整型字符数组转换为字符串,

//int型数组转字符串  使用StringBuffer
int ints = {1,2,3,4,5,6};
StringBuffer str = new StringBuffer();
for (int i = 0; i <ints.length ; i++) {
str = str.append(ints[i]);
}
String str1 = str.toString();

作者:Bye白夜
链接:https://www.imooc.com/article/43299
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作

自己想了一下也可以只使用String实现转化,

int[] arr = {1, 2, 3, 4, 5};
String str = new String();
for(int i = 0; i < arr.length; i++){
	str = str + arr[i];
}
System.out.println(str);

2)字符串数组转换为字符串,

//字符数组转字符串
//使用String.copyValueOf()方法
 
char[] ch = new char[10];
 
String string =String.copyValueOf(ch);

作者:Bye白夜
链接:https://www.imooc.com/article/43299
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作

4字符串——》数组

String  str= "abc";
 
//转换为char[]  注意取char[]中元素应该用  单引号 '' 表示char
char[] ch = str.toCharArray();

作者:Bye白夜
链接:https://www.imooc.com/article/43299
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作

也可以使用逐个替换的方法


char[] cha= new char[str.length()];
for(int i =0;i<str.length();i++) {
     cha[i] = str.charAt(i);
}

5字符型——》整型

单个字符转换为整型

char c = 'a';
int a = (int)c;

多个的字符串转化为一个整型数字

int/Integer num  =  Integer.parseInt(String str);

多个字符串转化为多个整型数字(整型数组),可以使用字符串str——》字符数组stra(参照4),字符数组stra——》整型数组(参考本节上述方法)。

6包装类对象——》基本数字类型

Integer a = new Integer(3);
//int b = a;
//int b = a.intValue();

7基本数据类型——》包装类对象

Integer a = new Integer(3);
Integer a = Integer.valueOf(30);

8字符串——》包装类对象

Integer a = new Integer("124");
Integer a = Integer.parseInt("123");

9包装类对象——》字符串

Integer f = new Integer(123);
String str = f.toString();
//String str = f+"";

10StringBuffer——》String

String str= sb.toString();

11String——》StringBuffer

StringBuffer sb = new StringBuffer(str);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值