字符串是一个特殊的对象。正则表达式
字符串一旦初始化就不能够被改变。数组
String s="abc";//建立一个字符串对象在常量池中
String s1=new String("abc");//建立了两个对象,一个new字符串对象在堆内存中。类比new一个对象的内存图
特色:函数
字符串对象一旦被初始化就不会改变。spa
字符串常量池没有就创建;池中有,直接使用。共享对象
String构造函数内存
主要几个String构造函数字符串
String(byte[] bytes){}//字节型
String(byte[] bytes,int offset,int length){}
String(char[] arr){}//字符数组
String(char[] arr,int offset,int count){}
获取get
一、获取字符串中字符的个数(长度)string
int length();
二、根据位置获取字符it
char charAt(int index);
三、根据字符(字符串)获取字符串中第一次出现的位置。从前日后查
1. int indexOf(int ch);
2. int indexOf(int ch,int fromIndex); //从指定位置对ch进行查找
3. int indexOf(String str);
4. int indexOf(String str,int fromIndex);
四、根据字符(字符串)获取字符串中第一次出现的位置。从后往前查
1. int lastIndexOf(int ch);
2. int lastIndexOf(int ch,int fromIndex);
3. int lastIndexOf(String str);
4. int lastIndexOf(String str,int fromIndex);
五、获取字符串中一部分字符串,子串
String substring(int beginIndex, int endIndex);//左闭右开。(要begin不要end)
String substring(int beginIdex);
转换
一、将字符串转换成字符串数组(切割)
String[] split(String regex);//涉及到正则表达式
二、将字符串转换成字符(char)数组
char[] toCharArray();
三、将字符串转换成字节数组
byte[] getBytes();
四、将字符串中的字母转换成大小写
String toUpperCase();//大写
String toLowerCase();//小写
五、将字符串中的内容进行替换
String replace(char oldChar,char nowChar);
String replace(String s1,String s2);
六、将字符串两端空格去除
String trim();
七、将字符串进行链接
String concat(String str);
判断
一、两个字符串内容是否相同
boolean equals(Object obj);
boolean equalsIgnoreCase(String str); //忽略大小写比较字符串内容
二、字符串中是否包含指定字符串
boolean contains(String str);
三、字符串是否以指定字符串开头,或结尾
boolean startsWith(String str);//开头
boolean endsWith(String str);//结尾
比较
按字典顺序比较两个字符串
int compareTo(String anotherString)
字符串对象的规范化表示
String intern();
事例:
String t=new String("abc");//new一个String对象,在堆内存中
t.intern();//将"abc"添加进字符串池中