package edu; //三种能力:1、打字快的能力。2、逻辑思维能力3、调试能力。 public class 字符串操作练习 { static int aa=12; static void aaa() {} public static void main(String[] args) { //1、长度 //2、字符串索引位置的字符 //3、提取子串 //4、拆分 //5、合并 //6、转换成字符数组 //7、字符数组转换成字符串 //8、把其他数据类型转换成字符串 //9、替换 //10、正则表达式 //11、根据字符找索引位置 //12、去除字符串前后空格 //13、把字符串改成大写 //14、把字符串改成小写 //15、把字符串改成字节数组表示 //16、根据字节数组生成字符串 String s="njkchkje bfv cjd";//字符串类对象 System.out.println(s.length());//1 System.out.println(s.charAt(4));//2 System.out.println(s.substring(4, 10));//包括索引为4 的字符,不包括索引为10 的字符 String[] aaa=s.split(" ");//按空格将字符串分组,并保存成字符串型数组 for(int i=0;i<aaa.length;i++) { System.out.println(aaa[i]); } System.out.println("************************");//4 System.out.println(s.concat("Hello!"));//5 char[] tt= s.toCharArray(); /*将字符变成字符数组,字符串(字符串类对象)只是调用了将字符变成字符数组的方法, 用一个字符将保存的字符数组一行一行输出来*/ for(int i=0;i<tt.length;i++) { System.out.println(tt[i]); } System.out.println("***************************"); String ss=new String(tt);//7.将字符数组转换成字符串:通过构造函数 System.out.println(ss);//输出字符串 System.out.println("***************************"); String string="We are happy!"; System.out.println(string.replaceAll(" ", " "));//将空格处都替换成 ;replace()方法前面是替换目标,后面是要替换的值 //下面的方法是将整型实型等其他类型转换成字符串 System.out.println(String.valueOf(20));//字符串有静态方法(当加载这个类的时候,静态的东西就已经分配了不用去new这个对象,它就已经在内存里面。) //静态的东西可以直接调,但是我们推荐用类名调,静态的的东西可以用类名去访问(加一个限定访问起来可以更清晰,便于我们知道这个方法是从哪个类里面定义的。) 字符串操作练习.aa=25;//加类名调用静态成员 字符串操作练习.aaa(); System.out.println(String.valueOf(true));//将true转换成字符串。 System.out.println(string.indexOf('r',3));//11、从第三个字符开始匹配字符为d的字符串的索引位置 String ttt=" Laurence is a good man "; System.out.println(ttt.trim());//12 System.out.println(ttt.toUpperCase());//13 System.out.println(ttt.toLowerCase());//14 byte [] a=ttt.getBytes();//将字符串变成字节数组 System.out.println(new String(a));//将字节数组转换成字符串 } }
