字符串的案例

本文通过实例展示了字符串转换功能,包括String的getBytes(), toCharArray(), String.valueOf()等方法,以及字符串大小写转换、拼接和替换操作。还介绍了如何根据需求定制字符串格式,如首字母大写和数组元素拼接成指定格式的字符串。
摘要由CSDN通过智能技术生成

字符串的案例

String 的转换功能
package cn.itcast05;
/*
 * String的转换功能:
 * byte[] getBytes():把字符串转换成字节数组
 * char[] toCharArray():把字符串转换成字符数组
 * static String valueOf(char[] chs):把字符数组转成字符串
 * static String valueOf(int i):把int类型的数据转成字符串
 * 	注意:String类的valueOf方法可以把任意类型的数据转成字符串
 * String toLowerCase():把字符串转成小写
 * String toUpperCase():把字符串转成大写
 * String concat(String str):把字符串拼接。
 * 
 */
public class StringDemo {
	public static void main(String[] args) {
		//定义一个字符串对象
		String s = "JAVASE";
		
		// byte[] getBytes():把字符串转换为字节数组。
		byte[] bys = s.getBytes();
		for (int x =0;x<bys.length;x++) {
			System.out.println(bys[x]);
			
		}
		System.out.println("----------------");
	
	//char[] toCharArray():把字符串转换成字符数组
		char[] chs = s.toCharArray();
		for(int x = 0 ; x < chs.length;x++) {
		System.out.println(chs[x]);
	
	}
	System.out.println("------------");
	
	//static String valueOf(char[] chs):把字符数组转成字符串
	int i =100;
	String  sss = String.valueOf(i);
	System.out.println(sss);
	System.out.println("--------------");
	 //String toLowerCase():把字符串转成小写
	System.out.println("toLowerCase:"+s.toLowerCase());
	System.out.println("s:"+s);
	
	//String toUpperCase():把字符串转成大写
	System.out.println("toUpperCase:"+s.toUpperCase());
	System.out.println("toUpperCase:"+s);
	//String concat(String str):把字符串拼接。
	String s1 = "hello";
	String s2 ="world";
	String s3 =s1+s2;
	String s4 = s1.concat(s2);
	System.out.println("s3:"+s3);
	System.out.println("s4:"+s4);
    }
	}

把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

package cn.itcast05;
/*
 * 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
 * 
 * 举例:
 * helloWORLD
 * 结果:
 * Helloworld
 * 分析:
 * A:先获取第一个字符
 * B:获取除了第一个字符以外的字母
 * C:把A转成大写
 * D:把B转成小写
 * E:C拼接D
 
 */
public class StringTest {
public static void main(String[] args) {
//定义一个字符串
	
	String s ="helloWORLD";
	//先获取第一个字符
	String s1 = s.substring(0,1);
	//获取除了第一个字符以外的字母
	String s2 = s.substring(1);
	//把A转成大写
	String s3 = s1.toUpperCase();
	//把B转成小写
	String s4 = s2.toLowerCase();
	//C拼接D
	String s5 = s3.concat(s4);

	System.out.println(s5);
	//进行优化
	//链式编程
	String result = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
	System.out.println(result);	
}	
}

String的其他功能

package cn.itcast06;
/*
 * String的其他功能:
 * 替换功能:
 * String replace(char old,char new)
 * String replace(String old,String new)
 * 去除字符串两空格:
 * String trim()
 * 按字典顺序比较两个字符串:
 * int compareTo(String str)
 * int compareToIgnoreCase(String str) 
 */

public class StringDemo {
public static void main(String[] args) {
	//替换功能
	String s1 = "helloworld";
	String s2 = s1.replace('1', 'k');
	String s3 = s1.replace("owo","ak47");
	System.out.println("s1:"+s1);
	System.out.println("s2:"+s2);
	System.out.println("s3:"+s3);
	System.out.println("--------------");
	//去除字符串中两空格
	String s4 = "  hello  world   ";
	String s5 = s4.trim();
	System.out.println("s4:"+s4+"----");
	System.out.println("s5:"+s5+"----");
	
	//按字典顺序比较两个字符串
	String s6 ="hello";
	String s7 = "hello";
	String s8 ="abc";
	String s9 = "xyz";
	System.out.println(s6.compareTo(s7));
	System.out.println(s6.compareTo(s8));
	System.out.println(s6.compareTo(s9));	
}	
}

把数组中数据按照指定格式拼接成一个字符串

package cn.itcast06;
/*
 * 需求:把数组中数据按照指定格式拼接成一个字符串
 * 举例:
 * int[] arr = {1,2,3};
 * 输出结果:
 * "[1,2,3]"
 * 分析:
 * A:定义一个字符串对象,只不过内容为空
 * B:先把字符串拼接一个"["
 * C:遍历int数组,得到每一个元素
 * D:先判断该元素是否为最后一个
 * 				是:就直接拼接元素和"]"
 * 				不是:就拼接元素和逗号以及空格
 * E:输出拼接后的字符串
 * 
 */
public class StringTest {
public static void main(String[] args) {
	//前提是数组已经存在
	int[] arr = {1,2,3};
	
	//定义一个字符串对象,只不过内容为空
	String s = "";
	//先把字符串拼接一个"["
	s += "[";
	//遍历int数组,得到每一个元素
	for(int x =0;x<arr.length; x++) {
		//该元素是否为最后一个
		if(x==arr.length-1) {
		//直接拼接元素和"]"
			s +=arr[x];
			s +="]";
			
	}else {
		//就拼接元素和逗号以及空格
		s +=arr[x];
		s +=",";	
	}
	}
		//输出拼接后的字符串
	System.out.println("最终的字符串是:"+s);
}
}

把数组中数据按照指定格式拼接成一个字符串

package cn.itcast06;
/*
 * 需求:把数组中数据按照指定格式拼接成一个字符串
 * 举例:
 * int[] arr = {1,2,3};
 * 输出结果:
 * "[1,2,3]"
 * 分析:
 * A:定义一个字符串对象,只不过内容为空
 * B:先把字符串拼接一个"["
 * C:遍历int数组,得到每一个元素
 * D:先判断该元素是否为最后一个
 * 				是:就直接拼接元素和"]"
 * 				不是:就拼接元素和逗号以及空格
 * E:输出拼接后的字符串
 *     把代码用功能实现。
 */
public class StringTest02 {
public static void main(String[] args) {
	//前提是数组已经存在
	int[] arr = {1,2,3};
	//写一个功能,实现结果	
	String result = arrayToString(arr);
	System.out.println("最终结果是:"+result);
}
/*
 * 两个明确:
 * 返回值类型:String
 * 参数列表:int[] arr
 * 
 * 
 */
public static  String  arrayToString(int[] arr) {
		//定义一个字符串对象
		String s = "";
		//先把字符串拼接一个"["
		s += "[";
		//遍历int数组,得到每一个元素
		for(int x =0;x<arr.length; x++) {
			//该元素是否为最后一个
			if(x==arr.length-1) {
			//直接拼接元素和"]"
				s +=arr[x];
				s +="]";
				
		}else {
			//就拼接元素和逗号以及空格
			s +=arr[x];
			s +=",";
		}
		}
			//输出拼接后的字符串
		System.out.println("最终的字符串是:"+s);
	 return  s;
	}
}

学习他人视频-------学习笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值