java中String类简单的字符串提取,转换,分解字符串数组,检索,获取字符串的子串,查找,字符串连接,去除字符串空格,字符串与基本类型互换


package hanjia;
//String类:
	public class hanjia{
	public static void main(String args[]) {
		String s1=new String("abcde12345 java is a langth");
		
		//获取一个字符串指定位置的字符:char charAt(int index):index值是从0到长度-1;因为字符串从零开始数起
		System.out.println("s1.charAt(5):"+s1.charAt(5));
		
		//将字符串转换为字符数组
		//void getChars(int 源字符串开头,int 源字符串自定义结尾,char []数组1,int 数组1的开始位置)
		char s2[]=new char[10];
		s1.getChars(2,6,s2,0); //错误的写法:s1.getChars(1,7,char s2[],0)
		System.out.print("拷贝后得到的字符串为s2:");
		System.out.println(s2); //不要这样写,System.out.println("拷贝后得到的字符串为s2:"+s2);容易出现乱码
		s1.toCharArray();
		System.out.println("byte toCharArray():将字符串对象中的字符转换为一个字符数组:"+s1);
		
		String s3="abcde12345 java is a langth";
		s3.getBytes();
		System.out.println("getBytes()将字符串转换为字节数组:"+s3);
		
		String []str="abc:pan:childens:快乐水".split(":");//相当于str1="abc:pan:childens:快乐水";str=str1.split(":")
		System.out.println("String split(String regex)将字符串按正则表达式分解字符串数组:"+str[1]);
		
		String str1="substring".substring(2); //从指定位置到最后的子串
		String str2="substring".substring(2,6);//从开头下标为2到6.注意下标从0开始而且数头不数尾
		System.out.println("获取字符串的子串str1:"+str1);
		System.out.println("获取字符串的子串str2:"+str2);
		
		String str3="abcde family in traditional java is a langth";
		System.out.println("从头部开始查找第一次出现a的下标:"+str3.indexOf('a'));
		System.out.println("从指定的位置查找a的位置返回下标:"+str3.indexOf('a','4'));
		System.out.println("从字符串中得到单词返回第一个下标:"+str3.indexOf("is"));
		System.out.println("当查找没有该单词或字母时返回-1"+str3.indexOf("p2p"));
		//从尾部开始查找与上面四个类似:int lastIndexOf(int ch),int lastIndexOf(int ch,int fromIndex)
		//int lastIndexOf(String str),int lastIndexOf(String str,int fromIndex)
		System.out.println("从尾部开始查找第一个a的下标,下标是从开头数起:"+str3.lastIndexOf('a'));
		
		//字符串的修改
		//字符串连接
		String str4="abc";
		String str5="123";
		System.out.println("字符串连接:"+str4.concat(str5));
		System.out.println("类似+"+"aa".concat("bb"));
		//字符串的替换
		System.out.println("字符串的替换,把b替换为3:"+str4.replace('b', '3'));
		//去除字符串中开头与结尾处的空格(不会去除中间空格)
		String str6="   abC  De  ";
		System.out.println("去除空格trim():"+str6.trim());
		//把所有字母转换为大写
		System.out.println(str4.toUpperCase());
		//把所有字母转换为小写
		System.out.println(str6.toLowerCase());
		
		//字符串与基本类型的互换
		//把基本类型转换为字符串:static Sting valuesOf(基本数据类型)
		String s4=String.valueOf(123);
		String s5=String.valueOf(true);
		String s6=String.valueOf(6.66);
		System.out.println(s4);
		System.out.println(s5);
		System.out.println(s6);
		//把字符串转换换成基本类型,需要用到基本类型的包装类,包装类parseXXX(字符串)
		//String toString():返回此对象的字符串(主要用于非字符串对象的输出) 
		byte bt=Byte.parseByte("123");
		int n=Integer.parseInt("123456");
		double d=Double.parseDouble("123.456");
		System.out.println(bt);
		System.out.println(n);
		System.out.println(d);
	}
}

在这里插入图片描述

个人理解及资料参考,若有不足或者需要扩展请指点。
若有雷同,纯属巧合。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值