java中String类型转换方法

时间:2023-01-04 15:33:51

integer to String :

int i = 42;
String str = Integer.toString(i);
or
String str = "" + i

double to String :
String str = Double.toString(i);

long to String :
String str = Long.toString(l);
float to String :
String str = Float.toString(f);
String to integer :
str = "25";
int i = Integer.valueOf(str).intValue();
or
int i = Integer.parseInt(str);
String to double :
double d = Double.valueOf(str).doubleValue();

String to long :
long l = Long.valueOf(str).longValue();
or
long l = Long.parseLong(str);

String to float :
float f = Float.valueOf(str).floatValue();
decimal to binary :
int i = 42;
String binstr = Integer.toBinaryString(i);
decimal to hexadecimal :
int i = 42;
String hexstr = Integer.toString(i, 16);
or
String hexstr = Integer.toHexString(i);
hexadecimal (String) to integer :
int i = Integer.valueOf("B8DA3", 16).intValue();
or
int i = Integer.parseInt("B8DA3", 16);
ASCII code to i = 64;
String aChar = new Character((char)i).toString();
integer to ASCII code c = 'A';
int i = (int) c; // i will have the value 65 decimal
To extract Ascii codes from a test = "ABCD";
for ( int i = 0; i < test.length(); ++i ) {
char c = test.charAt( i );
int i = (int) c;
System.out.println(i);
}

integer to boolean :
b = (i != 0);
boolean to =
note :
To catch illegal number conversion, try using the try/catch mechanism.
try{
i = Integer.parseInt(aString);
}
catch(NumberFormatException e)
{
}

java中String类型转换方法的更多相关文章

  1. Java中String类型细节

    Java中String类型细节 一 . String两种初始化方式 1 . String str1= “abc”;//String类特有的创建字符对象的方式,更高效 在字符串缓冲区中检测”abc”是否 ...

  2. Java中String类型详解

    这篇博客是我一直想总结的,这两天一直比较忙,先上传下照片吧,过后有时间再弄成正常的. 本文主要是对Java中String类型的总结,包括其在JVM中是怎么存储的...

  3. 【转载】 Java中String类型的两种创建方式

    本文转载自 https://www.cnblogs.com/fguozhu/articles/2661055.html Java中String是一个特殊的包装类数据有两种创建形式: String s ...

  4. 关于JAVA中String类型的最大长度

    前些天看到一道面试题,题目很容易理解:String的长度限制是多少? 针对这个题目,浏览了网友的回答,大概得到了3个层次的答案. 最浅的层次: 近似计算机内存大小的长度.这是作为一个程序员最浅显的回答 ...

  5. Java中String类型的数据比较

    在Java中如果想比较两个字符串是否相等,可以使用string1==string2 或string1.equal(string2)来比较. 但是,第一种方法过于局限.例如, String string ...

  6. Java中String类型的不可变性和驻留池

    一 基本概念 可变类和不可变类(Mutable and Immutable Objects)的初步定义: 可变类:当获得这个类的一个实例引用时,可以改变这个实例的内容. 不可变类:不可变类的实例一但创 ...

  7. java中String类型的相关知识

    String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...

  8. java中String类型

    string对象常用方法 string对象比较方法: string类获取包含子串的方法: 字符串和数字的转换: String类 String对象是不可改变的,字符串一旦创建,内容不能再改变. 构造字符 ...

  9. Java中String类型的部分用法

    1.如何将字符串转换为整型数值? int i = Integer.parseInt("20"); 2.如何用“==”还是equals比较两个字符串? “==”是用来比较俩引用是不是 ...

随机推荐

  1. Cocosd-x的坐标系

    OpenGL 坐标系 :   原点在屏幕左下角,x 轴向右,y 轴向上. UI坐标体系       :   原点在屏幕左上角,x 轴向右,y 轴向下. 屏幕坐标系:    UI 世界坐标系:  也叫绝 ...

  2. PureLayout和Masonry比较

    一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...

  3. iOS-TextField知多少

    iOS-TextField知多少 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRect ...

  4. POJ 3336 Count the string (KMP&plus;DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

  5. HDOJ 1058 Humble Numbers&lpar;打表过&rpar;

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  6. SQL Server 系统时间

    getdate()函数:取得系统当前的日期和时间.返回值为datetime类型的. 用法:getdate() 例子: select getdate() as dte,dateadd(day,-1,ge ...

  7. 基于Android的上课助手的概况及第一周冲刺详情

    基于Android平台的上课助手 一.       功能简介 课表查询 课程提醒 空闲教室的查询 二.       开发环境 Android 三.       开发成员 组长:*岩 成员:王亚蕊.孙 ...

  8. xxe漏洞的学习与利用总结

    前言 对于xxe漏洞的认识一直都不是很清楚,而在我为期不长的挖洞生涯中也没有遇到过,所以就想着总结一下,撰写此文以作为记录,加深自己对xxe漏洞的认识. xml基础知识 要了解xxe漏洞,那么一定得先 ...

  9. 如何用CSS和jQuery实现一个侧滑导航菜单

    为了建立导航菜单,让我们先看看html结构:<!DOCTYPE html><html lang="en"><head> <meta cha ...

  10. Java学习笔记(四)——好记性不如烂键盘(答答租车)

    根据所学知识,编写一个控制台版的租车系统. 功能: 1. 展示所有可租车辆 2. 选择车型.租车辆 3. 展示租车清单,包含:总金额.总载货量及其车型.总载人量及其车型 代码参考imooc中Java课 ...