标签:9.png 提取 cas 查看类 表达 mic style 字符串转换 ack
创建字符串的常见4种方式:
代码举例:
package demo01; public class Demo01String { public static void main(String[] args) { //创建字符串方式一: 无参构造 String str1 = new String(); System.out.println("我的第一个字符串" + str1); //创建字符串方式二:通过字符数组构造 char chars[] = {‘a‘, ‘b‘, ‘c‘}; String str2 = new String(chars); System.out.println("我的第二个字符串" + str2); // 创建字符串方式三:通过字节数组构造 byte bytes[] = {97, 98, 99}; String str3 = new String(bytes); System.out.println("我的第三个字符串" + str3); //创建字符串方式四:直接创建 System.out.println("我的第四个字符串" +"abc"); } }
执行结果:
程序当中直接写上的双引号字符串,就在常量池当中,常量池中的对象共享。通过构造方法创建的字符串,不共享对象。
判断功能的方法
代码举例
package demo01; public class Demo02String { public static void main(String[] args) { // 创建字符串对象 String s1 = "hello"; String s2 = "hello"; String s3 = "HELLO"; // boolean equals(Object obj):比较字符串的内容是否相同 System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); } }
执行结果:
注意事项:
代码举例:
package demo01; public class Demo03String { public static void main(String[] args) { //创建字符串对象 String s = "helloworld123"; // int length():获取字符串的长度,其实也就是字符个数 System.out.println(s.length()); System.out.println("‐‐‐‐‐‐‐‐"); // String concat (String str):将将指定的字符串连接到该字符串的末尾. String s2 = s.concat("werhello aww"); System.out.println(s2); // char charAt(int index):获取指定索引处的字符 System.out.println(s.charAt(3)); System.out.println(s.charAt(1)); System.out.println("‐‐‐‐‐‐‐‐"); // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1 System.out.println(s.indexOf("l")); System.out.println(s.indexOf("owo")); System.out.println(s.indexOf("ak")); System.out.println("‐‐‐‐‐‐‐‐"); } }
执行结果:
字符串的截取方法:
代码举例:
package demo02; public class Demo01String { public static void main(String[] args) { String s = "12356789abc"; // String substring(int start):从start开始截取字符串到字符串结尾 System.out.println(s.substring(0)); System.out.println(s.substring(5)); System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start,int end):从start到end截取字符串。含start,不含end。 System.out.println(s.substring(0, s.length())); System.out.println(s.substring(3, 8)); } }
执行结果:
代码举例:
package demo02; public class Demo02String { public static void main(String[] args) { //创建字符串对象 String s = "abc"; // char[] toCharArray():把字符串转换为字符数组 char[] chs = s.toCharArray(); for (int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); // byte[] getBytes ():把字符串转换为字节数组 byte[] bytes = s.getBytes(); for (int x = 0; x < bytes.length; x++) { System.out.println(bytes[x]); } System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); // 替换字母it为大写IT String str = "123456abc123"; String replace = str.replace("123", "abc"); System.out.println(replace); System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); } }
执行结果:
注意事项:此方法的参数是正则表达式,需要符合正则表达式规则
package demo02; public class Demo03String { public static void main(String[] args) { //创建字符串对象 String s = "aa,bb,cc"; // 调用切割方法 String[] strArray = s.split(","); // 遍历数组 for (int x = 0; x < strArray.length; x++) { System.out.print(strArray[x] + " "); } } }
执行结果:
标签:9.png 提取 cas 查看类 表达 mic style 字符串转换 ack
原文地址:https://www.cnblogs.com/wurengen/p/10849615.html