Java String类型学习

1.基本数据类型转 String 类型:
只需要在被转换变量后加 “+ “” "
2.String 类型 转 基本数据类型:
通过基本类型的包装类调用 parseXX 方法即可。
3.int 是 Integer(整数) 的缩写。
4.所有转换方法:
String转int: Integer.parseInt();
String转double: Double.parseDouble();
String转float: Float.parseFloat();
String转long: Long.parseLong();
String转byte: Byte.parseByte();
String转Boolean: Boolean.parseBoolean(); //只有字符串類型的"false” 和 “true” 才能转化为布尔型
String转short: Short.parseShort();
5.String 转 char: 例↓
String s5 = “123”;
char s7 = s5.charAt(0); 通过 字符串变量.charAt(0) 提取字符串变量中位于0号位置上的字符
char s8 = s5.charAt(1); 通过 字符串变量.charAt(1) 提取字符串变量中位于1号位置上的字符
System.out.println(s7); 结果为 “123” 中的 “1”
System.out.println(s8); 结果为 “123” 中的 “2”
System.out.println(s5.charAt(3)); 结果为 “123” 中的 “3”

public class 字符串{
	public static void main(String[] args) {
		int n1 = 100;
		float f1 = 1.1f;
		double d1 = 4.5;
		boolean b1 = true;

		//基本数据类型转 String 类型。
		String s1 = n1 + "";
		String s2 = f1 + "";
		String s3 = d1 + "";
		String s4 = b1 + "";
		System.out.println(s1);		
		System.out.println(s2);		
		System.out.println(s3);		
		System.out.println(s4);	

		//String 类型转 基本数据类型。
		String s5 = "123";
		String s6 = "true";
		int num1 = Integer.parseInt(s5);
		double num2 = Double.parseDouble(s5);
		float num3 = Float.parseFloat(s5);
		long num4 = Long.parseLong(s5);
		byte num5 = Byte.parseByte(s5);
		boolean num6 = Boolean.parseBoolean(s5);
		boolean num7 = Boolean.parseBoolean(s6);
		char s7 = s5.charAt(1);
		System.out.println(num1);
		System.out.println(num2);
		System.out.println(num3);
		System.out.println(num4);
		System.out.println(num5);
		System.out.println(num6);
		System.out.println(num7);
		System.out.println(s7);

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值