java怎么拦截某个对象
218
2022-09-20
String类的常用方法
import java.util.Scanner;public class TestStringAll { public static void main(String[] args) { //使用Integer类中的构造方法来构造对象,该类没有无参的构造方法 // int 转为 Integer Integer it1 = new Integer(123); //自动调用toString()方法,得到字符串类型的十进制整型 System.out.println("it1 = " + it1); //123 //String 转为 Integer Integer it2 = new Integer("456"); System.out.println("it2 = " + it2); //456 System.out.println("-------------------------"); //实现int类型和Integer类型之间的相关转换 //int类型 转为 String类型 Integer it3 = Integer.valueOf(789); System.out.println("it3 = " + it3); //789 //将String类型 转为 int类型 int res = it3.intValue(); System.out.println("res = " + res); //789 System.out.println("-------------------------"); //将String类型 转为 Int类型 res = Integer.parseInt("12345"); //要求字符串中的每个字符都是十进制整数,否则产生数字格式异常 //res = Integer.parseInt("12345a"); System.out.println("res = " + res);//12345 int类型 //将-1转换成字符串形式的二进制 System.out.println(Integer.toBinaryString(-1)); //11111111111111111111111111111111 //将-1转换为字符串形式的十六进制 System.out.println(Integer.toHexString(-1)); //ffffffff System.out.println("-------------------------"); //自动装箱和自带拆箱的机制 Integer it4 = 100; //Int类转 转为 Integer 发生自动装箱,自动调用valueOf()方法 res = it4; //Integer类型 转为 int类型,自动调用intValue()方法 System.out.println("-------------------------"); Integer it5 = 127; Integer it6 = 127; Integer it7 = new Integer(127); Integer it8 = new Integer(127); System.out.println(it5.equals(it6)); //true 比较地址 System.out.println(it5 == it6); //true 比较地址 System.out.println(it7.equals(it8)); //true 内容相同 System.out.println(it7 == it8); //false 地址不同 System.out.println("-------------------------"); Integer it9 = 128; Integer it10 = 128; Integer it11 = new Integer(128); Integer it12 = new Integer(128); System.out.println(it9.equals(it10)); //true 内容相同 System.out.println(it9 == it10); //false 地址不同 System.out.println(it11.equals(it12)); //true 内容相同 System.out.println(it11 == it12); //false 地址不同 System.out.println("-------------------------"); // 使用无参的构造方法来构造对象 String s1 = new String(); //返回当前字符串的长度 int len = s1.length(); System.out.println("len = " + len); //0 System.out.println("-------------------------"); //使用有参的构造方法来构造对象 String s2 = new String("hello"); //获取当前字符串的长度 len = s2.length(); System.out.println("len = " + len); //5 //使用 charAt()方法来获取里面的单个字符 //char cv = s2.charAt(-1); 字符串下标越界异常 //输入字符串下标返回对应字符 char cv = s2.charAt(0); System.out.println("cv = " + cv); //h cv = s2.charAt(4); // System.out.println("cv = " + cv); //o System.out.println("-------------------------"); // 打印字符串中的所有字符 for(int i = 0; i < s2.length(); i++){ System.out.println(s2.charAt(i)); } System.out.println("-------------------------"); // 分别使用两种方式将字符串"12345"转换为整数并打印 //方法一: 借用Integer类 String s3 = new String("12345"); int s3res = Integer.parseInt(s3); System.out.println("转换后的整数是:" + s3res); //12345 //方法二: 取出单个字符利用ASCII进行拆分 s3res = 0; for(int i = 0; i < s3.length(); i++){ s3res = s3res*10 + (s3.charAt(i)-'0'); } System.out.println("最终转换的结果是: " + s3res); //12345 System.out.println("-------------------------"); String cptS1 = new String("abc"); //调用compareTo()方法实现字符串大小的比较 /* int compareTo(String anotherString) * - 用于比较调用对象和参数对象的字符串大小关系并返回。 * - 使用当前字符串中第一个字符起的每个字符减去参数字符串中对应位置的字符 * 若第一个字符不相等则可以代表字符串大小关系,若相等则使用下一个字符继续比较 * - 若存在的字符都相同时,则大小取决于长度。 * * int compareToIgnoreCase(String str) * - 用于比较调用对象和参数对象的字符串大小关系并返回。 * - 不考虑大小写,也就是'A'和'a'在该方法中认为是相等的关系。 * */ System.out.println(cptS1.compareTo("bcd")); //负数 -1 'a'-'b' System.out.println(cptS1.compareTo("abe")); //负数 -2 'c'-'e' System.out.println(cptS1.compareTo("abcdef"));//负数 -3 3 - 6 System.out.println("-------------------------"); //对应位置的字符串做减法,若所有字符相同则用长度做减法 System.out.println(cptS1.compareTo("aaa")); //正数 32 'b' - 'a' System.out.println(cptS1.compareTo("aba")); //正数 32 'c' - 'a' System.out.println(cptS1.compareTo("")); //正数 3 3 - 0 System.out.println("-------------------------"); //字符串大小不同时,比较规则 ASCII 'a' 97 'A' 65 '0' 48 空格 32 '\n' 10 System.out.println(cptS1.compareTo("Abc")); //正数 32 System.out.println(cptS1.compareTo("abC")); //正数 32 System.out.println(cptS1.compareToIgnoreCase("Abc")); // 0 System.out.println(cptS1.compareToIgnoreCase("abC")); //0 System.out.println("-------------------------"); /* boolean contains(CharSequence s) - 用于判断调用对象中是否包含参数指定的内容。 * - 其中参数是接口类型的引用,因此实参的传递有两种方式: * a.创建实现类的对象作为实参传递,而String类就是该接口的实现类; * b.使用匿名内部类来创建接口类型的对象作为实参传递; * */ String CtsS1 = new String(" let me give you some color to see see!"); //判断当前字符串中是否包含参数指定的内容 注意大小写区别 System.out.println(CtsS1.contains(new String("Some"))); //false System.out.println(CtsS1.contains(new String("some"))); //true System.out.println("-------------------------"); /* boolean startsWith(String prefix) * - 用于判断当前字符串中是否以参数指定的内容为开头; * boolean endsWith(String suffix) * - 用于判断当前字符串中是否以参数指定的内容为结尾; * */ //判断当前字符串是否以...开头 以及 以...结尾 System.out.println(CtsS1.startsWith("let")); //false 因为let前面还有空格 System.out.println(CtsS1.endsWith("see!")); //true System.out.println("-------------------------"); /* String toUpperCase() * - 用于将当前字符串中所有字符串转换为大写并返回 * String toLowerCase() * - 用于将当前字符串中所有的字符转换为小写并返回; * */ //将当前字符串中的所有字符转换为大写 以及 小写 //当进行字符串大小写转换时,会创建新的字符串,原来的字符串内容保持不变 String tupS2 = CtsS1.toUpperCase(); // LET ME GIVE YOU SOME COLOR TO SEE SEE! System.out.println("tupS2 = " + tupS2); // let me give you some color to see see! System.out.println("CtsS1 = " + CtsS1); String tlwS3 = tupS2.toLowerCase(); //let me give you some color to see see! System.out.println("tlwS3 = " + tlwS3); // LET ME GIVE YOU SOME COLOR TO SEE SEE! System.out.println("tupS2 = " + tupS2); System.out.println("-------------------------"); /* String trim() - 用于去除当前字符串中两端的空白字符并返回; */ //去除字符串两端的空白字符 String tiS4 = CtsS1.trim(); //let me give you some color to see see! System.out.println("tiS4 = " + tiS4); // let me give you some color to see see! System.out.println("CtsS1 = " + CtsS1); System.out.println("-------------------------"); /* boolean equals(Object anObject) - 用于比较字符串内容是否相等并返回; boolean equalsIgnoreCase(String anotherString) - 用于比较字符串内容是否相等并返回,不考虑大小写,如:'A'和'a'是相等。 */ /* 提示用户输入用户名和密码来模拟登陆的过程,当用户输入"admin"和"123456"时, * 则提示登录成功,否则提示登录失败,当用户输入3次之后仍然失败,则提示"账户已冻" * + "结,明天再试"要求不考虑大小写。 *//* Scanner sc = new Scanner(System.in); for(int i = 3; i > 0; i--){ //1.提示用户分别输入用户名和密码信息 System.out.println("请输入用户名信息"); String userName = sc.nextLine(); System.out.println("请输入密码信息:"); String passWord = sc.nextLine(); //2.判断用户输入的用户名和密码是否正确,并给出对应的提示 //当字符串字面值和字符串变量比较是否相等时,推荐使用字符串字面值调用 //避免空指针异常的发生 if("admin".equalsIgnoreCase(userName) && "123456".equalsIgnoreCase(passWord)){ System.out.println("登录成功 !"); break; }else{ System.out.println("用户名和密码错误,登录失败 ,您还有" + (i-1) + "次机会 !"); } //当用户使用完毕最后一次机会后,则冻结账户 if(i == 1){ System.out.println("账户冻结,明天再试"); } }*/ System.out.println("-------------------------"); /* * byte[] getBytes() - 用于将当前字符串内容转换为byte数组并返回。 * char[] toCharArray() - 用于将当前字符串内容转换为char数组并返回。 * */ //1.准备一个字符串对象 String tcaS1 = new String("GoodMoring!"); //2.将该字符串对象转换为char数组并打印 char[] arr = tcaS1.toCharArray(); for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } System.out.println("-------------------------"); //3.将该字符串对象转换为byte数组并打印 byte[] data = tcaS1.getBytes(); for(int i = 0; i < data.length; i++){ System.out.println(data[i]); //ASCII码 71 111 111... } System.out.println("-------------------------"); //4.使用静态方法valueOF将字符数组转换为String类型 String vlfS2 = String.valueOf(arr); System.out.println("vlfS2 = " + vlfS2); // GoodMoring! //5.使用构造方法将字符数组转换为String类型 String vlfS3 = new String(arr); System.out.println("vlfS3 = " + vlfS3); // GoodMoring! System.out.println("-------------------------"); /* * int indexOf(int ch) - 用于返回当前字符串中参数ch指定的字符第一次出现的下标。 int indexOf(int ch, int fromIndex) - 用于从fromIndex位置开始查找ch指定的字符。 - 上述方法若查找失败,则返回-1. int indexOf(String str) - 用于查找参数str指定的字符串并返回下标。 int indexOf(String str, int fromIndex) - 用于从fromIndex位置开始查找。 int lastIndexOf(int ch) - 用于返回参数ch指定的字符最后一次出现的下标。 int lastIndexOf(int ch, int fromIndex) - 用于从fromIndex位置开始查找ch指定字符出现的下标,反向搜索的第一次。 int lastIndexOf(String str) - 用于返回str指定字符串最后一次出现的下标。 int lastIndexOf(String str, int fromIndex) - 用于从fromIndex位置开始反向搜索的第一次。*/ //1.构造字符串对象 String idfS1 = new String("Good Good Study, Day Day Up!"); //2.查找字符串中的内容 //查找单个字符在该字符串中的索引位置并返回 int pos = idfS1.indexOf('g'); System.out.println("pos = " + pos); //-1 pos = idfS1.indexOf("G"); System.out.println("pos = " + pos); // 0 pos = idfS1.indexOf("G",0); System.out.println("pos = " + pos); // 0 pos = idfS1.indexOf("G",1); System.out.println("pos = " + pos); //5 System.out.println("-------------------------"); //查找单个字符在该字符串中的索引位置并返回 //Good Good Study, Day Day Up! pos = idfS1.indexOf("day"); System.out.println("pos = " + pos); //-1 pos = idfS1.indexOf("Day"); System.out.println("pos = " + pos); //17 pos = idfS1.indexOf("Day",17); System.out.println("pos = " + pos); //17 pos = idfS1.indexOf("Day",18); System.out.println("pos = " + pos); //21 //当下标不存在时没有发生异常,而是返回-1 pos = idfS1.indexOf("Day",28); System.out.println("pos = " + pos); //-1 System.out.println("-------------------------"); //编程查找s1中"Day"出现的所有位置并打印出来 //1.查找idfS1中"Day"第一次出现的索引位置 pos = idfS1.indexOf("Day"); //2.判断本次查找是否成功,若成功则打印下标在进行下一次查找 while(pos != -1){ System.out.println(pos); //查找"Day"字符串下一个出现的索引位置 pos = s1.indexOf("Day",pos+"Day".length()); } System.out.println("-------------------------"); //编程查找idfS1中"Day"出现的所有位置并打印出来 //Good Good Study, Day Day Up! pos = 0; while((pos = idfS1.indexOf("Day",pos)) != -1){ System.out.println(pos); pos += "Day".length(); //17 21 } System.out.println("-------------------------"); //Good Good Study, Day Day Up! /* int lastIndexOf(int ch) - 用于返回参数ch指定的字符最后一次出现的下标。 int lastIndexOf(int ch, int fromIndex) - 用于从fromIndex位置开始查找ch指定字符出现的下标,反向搜索的第一次。 int lastIndexOf(String str) - 用于返回str指定字符串最后一次出现的下标。 int lastIndexOf(String str, int fromIndex) - 用于从fromIndex位置开始反向搜索的第一次。*/ //使用lastIndexOf()方法查找该字符最后一次出现的索引位置 pos = idfS1.lastIndexOf("D"); System.out.println("pos = " + pos); //21 pos = idfS1.lastIndexOf("D",20); System.out.println("pos = " + pos); //17 pos = idfS1.lastIndexOf("D",16); System.out.println("pos = " + pos); //-1 System.out.println("-------------------------"); //Good Good Study, Day Day Up! //使用lastIndexOf()方法查找该字符串最后一次出现的索引位置 pos = idfS1.lastIndexOf("Good"); System.out.println("pos = " + pos); //5 pos = idfS1.lastIndexOf("Good",5); System.out.println("pos = " + pos); //5 pos = idfS1.lastIndexOf("Good",4); System.out.println("pos = " + pos); //0 pos = idfS1.lastIndexOf("Good",-1); System.out.println("pos = " + pos); //-1 System.out.println("-------------------------"); /* String substring(int beginIndex) - 用于获取当前字符串中从beginIndex位置开始的子字符串并返回。 String substring(int beginIndex, int endIndex) - 用于获取当前字符串中从beginIndex位置开始到endIndex结尾的子字符串并返回。*/ //获取当前字符串中指定下标位置的子字符串 //Good Good Study, Day Day Up! String idfS2 = idfS1.substring(3); System.out.println("idfS2 = " + idfS2); //d Good Study, Day Day Up! System.out.println("idfS1 = " + idfS1); //Good Good Study, Day Day Up! //包括4 不包括10 String idfS3 = idfS1.substring(4,10); System.out.println("idfS3 = " + idfS3); //Good System.out.println("idfS1 = " + idfS1); //Good Good Study, Day Day Up!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~