15-05-常用对象API(String类-常见功能-转换)

package cn.itcast.string.demo;

public class StringConstructDemo3 {

	public static void main(String[] args) 
	{
		/*
		 * 2 转换
		 * 2.1 将字符串变成字符串数组(字符串的切割)
		 * 		String[] split(String regex)
		 * 
		 * 2.2 将字符串变成字符数组
		 * 		char[] toCharArray()
		 * 
		 * 2.3 将字符串转成字节数组
		 * 		byte[] getBytes()
		 * 
		 * 2.4 将字符串中的字母转换大小写(仅限字母,若在常量池里的字符串,是不能改变值的,所以必须返回一个新字符串)
		 * 		String toUpperCase()//所有字母转成大写
		 * 		String toLowerCase()//所有字母转成小写
		 * 
		 * 2.5 将字符串中的内容进行替换
		 * 		String replace(char oldChar,char newChar)//将字符串中oldChar替换为newChar
		 * 		String replace(String s1,String s2)//将字符串s1替换为s2字符串
		 * 
		 * 2.6 将字符串两端的空格去除
		 * 		String trim()
		 * 
		 * 2.7 将字符串进行连接
		 * 		String concat(String str)//将指定字符串连接到此字符串的结尾
		 */
		stringMethodDemo_2();
	}

	private static void stringMethodDemo_2() 
	{
		String s = "张三,李四,王五";//怎么取出人名?可以先indexof找出逗号位置,之后从开头切到逗号,取出,但是麻烦
		String[] arr = s.split(",");//只要有逗号,都切一刀
		for (int i = 0; i < arr.length; i++) 
		{
			System.out.println(arr[i]);
		}
		
		char[] chs = s.toCharArray();
		for (int i = 0; i < chs.length; i++) 
		{
			System.out.println(chs[i]);
		}
		
		s="ab你";
		byte[] b = s.getBytes();
		for (int i = 0; i < b.length; i++) 
		{
			System.out.println(b[i]);
		}
		
		System.out.println("Abc".toUpperCase());
		
		System.out.println("java".replace('a', 'o'));
		System.out.println("java".replace('q', 'o'));//没找到q,那么替换失败,故输出的还是原串
		
		System.out.println("-"+"   a    bc"+"-");
		System.out.println("-"+"   a    bc".trim()+"-");
		
		System.out.println("abc".concat("kk"));
		System.out.println("abc"+"kk");
		System.out.println(""+4+1);
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值