字符串String,属于引用数据类型
字符串内部核心是char,但两者不是同一个内容
String 类的 substring 方法可以从一个较大的字符串提取出一个子串 (需要记忆)
--------------------------------------------------------------------------
数组为什么要提前申请足够大的空间?
因为如果不提前申请内存空间,不知道该数组的长度是多少,编辑该数组可能会对内存中其他数组产生影响,防止它越界
-------------------------------------------------------------------------------
不可变字符串指的是要修改字符串内容,不会在原地址内容进行修改,而是新开辟一段内存空间
,更改字符串变量的指向,指向新开辟的内存空间的地址,新开辟的内存空间内容就是修改后的内容
常量池
String 字符串每次使用可能会有重复的内容,常量池中保存大量字符串,将各种字符串存放在公共的常量池中,字符串变量指向常量池中相应的位置。
常量池会自己定期检查长时间没有被引用的字符然后销毁
1.常见String类的获取功能
public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串
代码如下
public class Test{
public static void main(String[] aaa)
{
String str = "anAdEfg";
System.out.println("String类的获取功能");
//获取字符串的长度
int length = str.length();
//获取指定索引位置的字符
char c1 = str.charAt(0);
//返回指定字符在此字符串中第一次出现处的索引
int c2 = str.indexOf('n');
//返回指定字符串在此字符串中第一次出现的索引
int c3 = str.indexOf("fg");
//返回指定字符在此字符串中从指定位置后第一次出现处的索引。
int c4= str.indexOf('f', 2);
//返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
int c5 = str.indexOf("fg", 2);
//从指定位置开始截取字符串,默认到末尾
String c6 = str.substring(2);
//从指定位置开始到指定位置结束截取字符串
String c7 = str.substring(2, 4);
System.out.println(length);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println(c7);
}
}
运行结果如下:
2.常见String类的判断功能
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空串""。
代码如下
public class Test{
public static void main(String[] aaa)
{
String str="anAdEfg";
String str1 = "axcde";
String str2 = "Axcde";
System.out.println("String类的判断功能");
//比较字符串的内容是否相同,区分大小写
boolean b = str1.equals(str2);
//比较字符串的内容是否相同,忽略大小写
boolean b1 = str1.equalsIgnoreCase(str2);
//判断字符串中是否包含传递进来的字符串
boolean b2 = str1.contains("cde");
//判断字符串是否以传递进来的字符串开头
boolean b3 = str1.startsWith("ax");
//判断字符串是否以传递进来的字符出结尾
boolean b4 = str2.endsWith("de");
//判断字符串的内容是否为空
boolean b5 = str1.isEmpty();
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
}
}
运行结果如下
3.常见String类的转换功能
public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。(String类的valueOf方法可以把任意类型的数据转成字符串。)
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
代码如下
public class Test{
public static void main(String[] aaa)
{
String str = "anAdEfg";
String str1 = "axcde";
int a=123323;
//把字符串转换为字节数组
byte[] bytes = str.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]+" ");
}
//把字符串转换为字符数组
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]+" ");
}
System.out.println();
//把字符数组转换成字符串
String s2 = new String (chars);
//把int类型的数据转成字符串
String s3 = Integer.toString(a);
//把字符串转换成小写
String s = str.toLowerCase();
//把字符串变成大写
String s1 = str.toUpperCase();
//字符串拼接
String s4 = str.concat(str1);
System.out.println(s);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
}
运行结果如下
4.常见String类的其他常用功能
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
public String trim() 去除两端空格
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果连个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
代码如下
public class Test{
public static void main(String[] aaa)
{
//统计字符串中的小写字母,大写字母数字空格的个数
String str = "1123ahdiASDFGF shaid";
int upper = 0;
int lower = 0;
int num = 0;
int space = 0;
for (int i = 0; i < str.length(); i++) {
//返回该索引处的char值
char c = str.charAt(i);
System.out.print(c+" ");
if(c>'a'&&c<'z'){
lower++;
}
if(c>'A'&&c<'Z'){
upper++;
}
if(c>'0'&&c<'9'){
num++;
}
if(c==' '){
space++;
}
}
System.out.println();
System.out.println(lower);
System.out.println(upper);
System.out.println(num);
System.out.println(space);
}
}
运行结果如下
补充:
//String 类的 substring 方法
String a = "宋宇30岁的时候有资产100个亿,房子10套";
String b = a.substring(4,9); //获取的是[4,6)这几个字符
System.out.println(b);
---------------------------------------------------
*/
/*String str1 = "大家都很帅,特别是宋宇最帅";
int a = str1.length(); //获取长度.length方法需要记住
char b = str1.charAt(3); //获取下标为3的那个字符
int c = str1.codePointAt(6); //获取下标为6的编码值
System.out.println(a); //a的长度就是代码单元
System.out.println(b);
System.out.println(c);
int[] arr = str1.codePoints().toArray(); //获取所有编码值,将编码值保存到数组中
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}
如果是比较基本数据类型,比较的是值
如果是比较引用数据类型,比较的是地址
equals比较的是值,使用方法 a.eauals(b) (a和b是变量)
-------------------------------------------------------------------
String a = null;与String b = "";是有区别的
前者是没地址没内容 后者是空数组符号
-----------------------------------------------------------------------
char编码值 码点值就是编码值,每个字母或者汉字有一个编码值
int a = str.length()获取长度.length方法需要记住
char a = str.chatAt(3)获取下标为3的那个字符
int a = str.codePointAt(n) 获取下标为n的编码值
int[] arr = str.codePoints().toArray(); 获取所有编码值,将编码值保存到数组中
----------------------------------------------------------------------------
String ApI
String转大小写 case意思是转化
.toUpperCase()为转字母为大写
.toLowerCase()为转字母为小写
String replace( CharSequence oldString,CharSequence newString)
返回一个新字符串。这个字符串用 newString 代替原始字符串中所有的 oldString。可
以用 String 或 StringBuilder 对象作为 CharSequence 参数
Sting切割
String[] arr = a4.split("下"); //用于切割字符串,用数组来接收
字符串拼接
StingBuilder实现原理
页的4KB分类是按照变量来分 buffer内部基本类型数组实现,buffer代表高性能
原来String不可变,是因为内容多大就申请多大的空间,修改需要重新申请。
buffer先申请足够大的空间,这样如果修改字符串,可以在原地址进行修改,不会超过操作系统的一个页,节省了空间



