java字符串之基本数据类型与字符串的转化
2007-11-20 10:11
399 查看
一、关键技术点
基本类型转化成字符串有三种方法
1、使用String的valueOf方法
2、将基本数据类型封装为对象,再调用对象的toString方法
3、用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串。
字符串转换成基本类型有两个方法
1、调用基本类型封装类的parseXxx静态方法。
2、用字符串构造基本类型的封装对象,再调用封装对象的xxxValue方法。
二、演示实例
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package book.oo.String;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 基本类型与字符串的相互转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class BasicTypeToString ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 整数转换成字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param n 待转换的整数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回转换后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String int2str(int n) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//3种转换方法实现转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第一种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Integer(n).toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第二种,注意""中间没有空格
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return "" + n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第三种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return String.valueOf(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 字符串转换成整数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param str 待转换的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回转换后的整数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int str2int(String str) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//2种方法实现转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第一种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Integer(str).intValue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第二种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Integer.parseInt(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String double2str(double d) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Double(d).toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return "" + d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return String.valueOf(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double str2double(String str) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Double(str).doubleValue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Double.parseDouble(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//其他基本类型与字符串相互转化这里不一一列出,方法都类似
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BasicTypeToString test = new BasicTypeToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n = 156;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str1 = test.int2str(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.int2str(n) = " + str1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.str2int(str) = " + test.str2int(str1));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double d = 12.345;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str2 = test.double2str(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.double2str(d) = " + str2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.str2double(str) = " + test.str2double(str2));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序输出:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.int2str(n) = 156
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.str2int(str) = 156
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.double2str(d) = 12.345
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.str2double(str) = 12.345
基本类型转化成字符串有三种方法
1、使用String的valueOf方法
2、将基本数据类型封装为对象,再调用对象的toString方法
3、用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串。
字符串转换成基本类型有两个方法
1、调用基本类型封装类的parseXxx静态方法。
2、用字符串构造基本类型的封装对象,再调用封装对象的xxxValue方法。
二、演示实例
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package book.oo.String;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 基本类型与字符串的相互转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class BasicTypeToString ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 整数转换成字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param n 待转换的整数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回转换后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String int2str(int n) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//3种转换方法实现转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第一种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Integer(n).toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第二种,注意""中间没有空格
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return "" + n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第三种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return String.valueOf(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 字符串转换成整数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param str 待转换的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回转换后的整数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public int str2int(String str) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//2种方法实现转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第一种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Integer(str).intValue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第二种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Integer.parseInt(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String double2str(double d) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Double(d).toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return "" + d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return String.valueOf(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double str2double(String str) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//return new Double(str).doubleValue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Double.parseDouble(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//其他基本类型与字符串相互转化这里不一一列出,方法都类似
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BasicTypeToString test = new BasicTypeToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n = 156;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str1 = test.int2str(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.int2str(n) = " + str1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.str2int(str) = " + test.str2int(str1));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double d = 12.345;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str2 = test.double2str(d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.double2str(d) = " + str2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("test.str2double(str) = " + test.str2double(str2));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序输出:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.int2str(n) = 156
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.str2int(str) = 156
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.double2str(d) = 12.345
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
test.str2double(str) = 12.345
相关文章推荐
- Java基础小知识——基本数据类型和字符串以及字节数组的转换
- Java基本数据类型转化总结
- java中基本数据类型的转化和强制类型转换浅谈
- Java基本数据类型的数据转化为字节数据的方法
- Java的基本数据类型与转化
- java基本数据类型与字符串的转换
- java 八种基本数据类型之与对应的封装类之间的相互转化
- Java 基本数据类型与其包装数据类型以及字符串(String)之间的转换问题
- [bxd学习java基本点]1.基本数据类型与字符串型转换与其他类型转成字符串
- JAVA面向对象-----值交换(基本数据类型 数组类型 对象的值 字符串的)
- 《黑马程序员》java笔记->基本数据类型包装类及自动拆箱装箱原理,数组排序练习字符串切割
- java 字符串转为基本数据类型
- Java基础小知识——基本数据类型和字符串以及字节数组的转换
- 从零开始学Java 第4章 基本数据类型与字符串
- java基本数据类型与字符串之间的转换(基本数据类型、对象封装类、自动装箱、自动拆箱)
- java中基本数据类型与字符串的转换
- JAVA面向对象-----值交换(基本数据类型 数组类型 对象的值 字符串的)
- Java学习总结--字符串String类和基本的数据类型有什么区别
- Java中 基本数据类型与封装类、字符串的相互转换 方法汇总