字符串、String等转换
(1) String 转换为字符串
例:
String s = "abcde";
char[] a = s.toCharArray();
(2) 字符串转换为String
char[] a;
a = {a,b,c,d};
String s;
s = new String(a);
(3) int类型转换为String
例:
int n = 0;
String s = String.valueOf(n)
(4)int -> String
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
(5)String -> int
s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
以下是答案:
第一种方法:s=i+""; //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象
第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
JAVA数据类型转换
这是一个例子,说的是JAVA中数据数型的转换.供大家学习
- public class TypeChange {
- public TypeChange() {
- }
- //change the string type to the int type
- public static int stringToInt(String intstr)
- {
- Integer integer;
- integer = Integer.valueOf(intstr);
- return integer.intValue();
- }
- //change int type to the string type
- public static String intToString(int value)
- {
- Integer integer = new Integer(value);
- return integer.toString();
- }
- //change the string type to the float type
- public static float stringToFloat(String floatstr)
- {
- Float floatee;
- floatee = Float.valueOf(floatstr);
- return floatee.floatValue();
- }
- //change the float type to the string type
- public static String floatToString(float value)
- {
- Float floatee = new Float(value);
- return floatee.toString();
- }
- //change the string type to the sqlDate type
- public static java.sql.Date stringToDate(String dateStr)
- {
- return java.sql.Date.valueOf(dateStr);
- }
- //change the sqlDate type to the string type
- public static String dateToString(java.sql.Date datee)
- {
- return datee.toString();
- }
- public static void main(String[] args)
- {
- java.sql.Date day ;
- day = TypeChange.stringToDate("2003-11-3");
- String strday = TypeChange.dateToString(day);
- System.out.println(strday);
- }
- }
字符串、String等转换的更多相关文章
- HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...
- [转] HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- java算法:统计数字-将数字转换成字符串,然后使用字符串String.valueOf()方法进行判断
题目: 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 样例 1: 输入: k = 1, n = 1 输出: 1 解释: 在 [0, 1] 中,我们发现 1 出现了 ...
- java字符数组char[]和字符串String之间的转换
java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...
- c/c++日期时间处理与字符串string转换
转自:https://www.cnblogs.com/renjiashuo/p/6913668.html 在c/c++实际问题的编程中,我们经常会用到日期与时间的格式,在算法运行中,通常将时间转化为i ...
- C++中数字与字符串之间的转换 scanf string总结(复习必读)
1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...
- Swift:字符串(String)分割之Substring优雅转换
认识Substring类型 这是一个全新的类型,看类名像是String的子类,但是大家千万别被误导了,Substring并不是String的子类,这是两个不同的类型,但是它们都继承了StringPro ...
- Java基础——基本类型和包装类、基本类型和字符串之间的转换
基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更 ...
随机推荐
- Windows Vista for Developers——第四部分:用户帐号控制(User Account Control,UAC)
作者:Kenny Kerr 翻译:Dflying Chen 原文:http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-f ...
- CSS:CSS cursor 属性
ylbtech-CSS:CSS cursor 属性 1.返回顶部 1. 实例 一些不同的光标: span.crosshair {cursor:crosshair;} span.help {cursor ...
- bzoj1941
KD-tree **了这道题 这个估价函数好鬼畜,把min打成max... 关于min的估价函数非常鬼畜,具体我也不知道为什么. #include<bits/stdc++.h> using ...
- 关于group_concat函数拼接字符超长的问题
昨天测试的人火急火燎的找我,跟我说数据不对!说明情况后我去查看,原来是数据上有个子查询出来的字段没有完全展示 问题很明显,就是数据被截断了.下面贴上我写的查询 wyids_是正确的显示,通过它子查询出 ...
- MongoDB -- 安装(win 10)
1. 下载安装包: mongodb-win32-x86_64-2008plus-ssl-4.0.10-signed.msi https://www.mongodb.com/download-cente ...
- Unity3D 开发ios时困扰多时游戏开始画面图片的分辨率
- java集合框架之HashCode
参考http://how2j.cn/k/collection/collection-hashcode/371.html List查找的低效率 假设在List中存放着无重复名称,没有顺序的2000000 ...
- 5-1条件运算符 & 5-2
三目运算符 新建类: ConditionDemo 用三目运算符: package com.imooc.operator; public class ConditionDemo { public sta ...
- LeetCode: 500 Keyboard Row (easy)
题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- html上传多图并预览
涉及知识:base64处理图片,ajax,js,thinkphp 效果图: 代码实现: html: <!DOCTYPE html> <html> <head> &l ...