字符串的遍历:
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); } }