Java第二阶段---16字符串---第一节 String
1.特性介绍
String 类位于 java.lang 包中,无需引入,直接使用即可。
String 类是由 final 修饰的,表示String 类是一个最终类,不能够被继承。
String 类构建的对象不可再被更改
示例
package com.cyx.string;public class Example1 {public static void main(String[] args) {//当使用一个字面量给字符串赋值时,首先会去字符串常量池中检测是否存在这个字面量。如果存在//则直接使用这个字面量的地址赋值即可。如果不存在,则需要再字符串常量池中创建这个字面量,然后//再将地址赋值过去即可。String s = "超用心";s += "在线教育";//这里的字符串拼接动作发生在堆内存上System.out.println(s);}}
2.常用构造方法
public String(String original); public String(char value[]); public String(char value[], int offset, int count); public String(byte bytes[]); public String(byte bytes[], int offset, int length); public String(byte bytes[], Charset charset);
示例
package com.cyx.string;import java.nio.charset.Charset;public class Example2 {public static void main(String[] args) {String s ="超用心在线教育";System.out.println(s);//如果有上面的代码的话 下面代码就只创建一个对象 因为常量池有已有对象了//这里会创建两个对象:一个是字面量会在常量池中创建一个对象,//另一个是new String("")构造方法创建出来的对象String s1 = new String("超用心在线教育");System.out.println(s1);char[] values = {'a','d','m','i','n'};String s2 = new String(values);System.out.println(s2);//在使用这个构造方法时必须要考虑到数组下标越界的可能性String s3 = new String(values,1,3);//offset代表跳过 count代表跳过之后数几个System.out.println(s3);//字节可以存储整数,字符也可以使用整数表示,这个整数就是ASCII码对于的整数值byte[] bytes = {97,98,99,100,101,102};String s4 = new String(bytes);System.out.println(s4);String s5 = new String(bytes,2,3);System.out.println(s5);Charset charset = Charset.forName("UTF-8");//构建UTF-8字符集String s6 = new String(bytes,charset);//代表 bytes 使用的是UTF-8字符集System.out.println(s6);}}
3. 常用方法
获取长度
public int length(); //获取字符串的长度
字符串比较
public boolean equals(Object anObject);//比较两个字符串是否相同
public boolean equalsIgnoreCase(String anotherString);//忽略大小比较两个字符串是 否相同
字符串大小写转换
public String toLowerCase();//转换为小写
public String toUpperCase();//转换为大写
示例
package com.cyx.string;public class Example3 {public static void main(String[] args) { String s1 = "超用心在线教育";int length = s1.length();//获取字符串的长度System.out.println(length);String s2 = "abc";String s3 = "abc";String s4 = "ABC";System.out.println(s2 == s3);//字符串之间进行比较时,首先会查看两个字符串的长度是否一致,如果一致,再看其中的每一个字符是否相同System.out.println(s2.equals(s3));System.out.println(s2.equals(s4));System.out.println(s2.equalsIgnoreCase(s4));String s5 = s2.toUpperCase();System.out.println(s5);String s6 = s4.toLowerCase();System.out.println(s6);}}
获取字符在字符串中的下标
public int indexOf(int ch); //获取指定字符在字符串中第一次出现的下标
public int lastIndexOf(int ch);//获取指定字符在字符串中最后一次出现的下标
获取字符串在字符串中的下标
public int indexOf(String str);//获取指定字符串在字符串中第一次出现的下标
public int lastIndexOf(String str);//获取指定字符串在字符串中最后一次出现的下标
获取字符串中的指定下标的字符
public char charAt(int index);
示例
package com.cyx.string;public class Example4 {public static void main(String[] args) {String s = "kliey@aliyun.com";int number = 'a';System.out.println(number);//'@' => char => int//求指定字符串中第一次出现的下标位置int index1 = s.indexOf('@');//相互兼容的数据类型之间可以发生自动类型转换System.out.println(index1);int index2 = s.lastIndexOf('@');System.out.println(index2);int index3 = s.indexOf('.');//相互兼容的数据类型之间可以发生自动类型转换int index4 = s.lastIndexOf('.');boolean case1 = (index1 == index2);//保证只有一个@boolean case2 = (index3 == index4);//保证只有一个.boolean case3 = (index3 - index2 > 1);//@必须在.的前面boolean case4 = (index1 > 0 && index3 < s.length()-1);//@不能在最开始 .不能在末尾if(case1 && case2 && case3 && case4){System.out.println("字符串"+ s +"是一个邮箱地址");}System.out.println(s.charAt(0));}
}