Java_08Java常用API---④String类的转换功能

一、String类的转换功能

char[] toCharArray():把字符串转换为字符数组
String toLowerCase():把字符串转换为小写字符串
String toUpperCase():把字符串转换为大写字符串

字符串的遍历:

	1、length()加上charAt()
	2、把字符串转为字符串数组,然后遍历数组

代码块

public class StringDemo{
	public static void main(String[] args ){
		//创建字符串对象
		String s = "abde";

		//chae[] toCharArray():把字符串转换为字符数组
		char[] chs= s.toCharArray();
		for(int x=0;x<chs.length;x++){
			System.out.println(chs[x]);
		}
		//String toLowerCase();把字符串转换为小写字符串
			System.out.println("Helloword".toLowerCase());
		//String toUpperCase():把字符串转换为大写字符串
		System.out.println("Helloword".toUpperCase());
	}
}

输出结果:

a
b
c
d
e
helloword
HELLOWORD

二、把字符串首字符大写,其他字母小写

分析:

1、键盘录入一个字符串
2、截取首字母
3、截取除了首字母以外的字符串
4、步骤2 转大写+ 步骤3 转小写	
5、输出即可

代码块

public class StringTest{
	public static void main(String[] args ){
		//键盘录入一个字符串
		Scanner sc = new Scanner(Systrm.in);
		System.out.println("请输入一个字符串");
		String s = sc.nextLine();

			//截取首字母
			String s1 = s.substring(0,1);
			
			//截取除了首字母以外的字符串
			String s2 = s.substring(1);

			//步骤2 转大写+ 步骤3 转小写
			String s3 = s1.toUpperCase()+s2.toLowerCase();
			System.out.println("s3:"+s3);
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值