String类
String类定义在java.lang包中,这个类在Java中被声明为final类,不能拥有子类。String类对象创建后不能更改
构造方法
- public String():空参构造。
- public String(String original):把一个字符串常量转成字符串。
- public String(char[] value):把字符数组转成字符串。
- public String(char[] value, int offset, int count):把字符数组一部分转成字符串。
- public String(int[] codePoints, int offset, int count):把整数数组的一部分转为字符串
- public String(StringBuffer buffer):将StringBuffer对象转为字符串
- public String(StringBuilder builder):将StringBuilder对象转为字符串
- public String(byte[] bytes):把字节数组转成字符串。
- public String(byte[] bytes, int offset, int length):把字节数组的一部分转成字符串。
- public String(byte bytes[],int offset,int length,String charsetName),
public String(byte byte[],int offset,int length,Charset charset),
public String(byte byte[],String charsetName),
public String(byte byte[],Charset charset),
public String(byte byte[],int offset,int length)
这几个构造器调用StringCoding.decode()方法进行解码,使用的解码的字符集就是我们指定的charset和charsetName
其他方法
获取功能
- int length():求串长
- char charAt(int index):获取指定索引位置的字符
- int indexOf(int ch):返回指定字符再次字符串中第一次出现处的索引
为什么这里参数是int类型,而不是char类型?
原因是:‘a’ 和 97 其实都可以代码‘a’。 - int indexOf(String str):返回指定字符串再次字符串第一次出现处的索引
- int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
- int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
- String substring(int start):从指定位置开始获取字符串,默认到末尾,包括start索引
- String substring(int start,int end):从指定位置开始到指定位置结束获取字符串,包括start索引,不包括end索引
- int offsetByCodePoints(int index, int codePointOffset):返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引
- int codePointAt(int index):返回index 处字符的代码点值
- int codePointBefore(int index):返回指定索引之前的字符的代码点值
- int codePointCount(int beginIndex, int endIndex):返回指定文本范围中的代码点数(个数)
- int offsetByCodePoints(int index,int codePointOffset)返回此String 中从给定的 index 处偏移 codePointOffset 个代码点的索引
- int hashCode():得到对象的hashcode 值
- int lastIndexOf(String str):返回子字符串最后出现的位置
- int lastIndexOf(String str, int fromIndex):返回子字符串在此字符串中的指定位置前最后一次出现处的索引
- CharSequence subSequence(int beginIndex, int endIndex):返回此序列从beginIndex到endIndex-1位置的子序列
判断功能
- boolean isEmpty():判空串
- boolean equals(Object anObject):判相等(比较String对象内容序列的异同)
- boolean contentEquals(StringBuffer sb):比较String对象内容序列的异同
- boolean contentEquals(CharSequence cs):比较String对象内容序列的异同
差别就是equals方法只有在另一个对象是String的情况下才可能返回true,boolean contentEquals(StringBuffer sb)方法要接收StringBuffer类的参数,而contentEquals(CharSequence cs)只要求另一个对象是CharSequence或其子类的对象 - boolean equalsIgnoreCase(String anotherString):比较两个字符串并在比较过程中忽略大小写
- boolean regionMatches(int toffset, String other, int ooffset, int len):从当前字符串的toffset位置开始,取一个长度为len的子串,再从另一个字符串other的ooffset位置开始也取一个长度为len的子串,比较这两个子串是否相同,如果这两个子串相同则返回true,否则返回false。
- boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len):上一个方法的重载,多出来的boolean类型的参数,用来确定比较时是否忽略大小写,值为true则忽略
- boolean startsWith(String prefix):如果参数prefix串所表示的字符序列是由该字符串所表示的字符序列的前缀,则返回true,否则为false。
- boolean startsWith(String prefix, int toffset):上一个方法的重载,表示参数从toffset开始判断
- boolean endWith(String suffix): 用于测试字符串是否以指定的后缀结束
- boolean matches(String regex):判断这个字符串是否在给定的正则表达式匹配
- boolean contains(CharSequence s):断字符串中是否包含指定字符
转换功能
- char charAt(int index):字符串按索引转为单个字符
- byte[] getBytes():把字符串转换成字节数组
- byte[] getBytes(String charsetName):上一个方法的重载
- byte[] getBytes(Charset charset):上一个方法的重载
- char[] toCharArray():把字符串转换为字符数组
- static String valueOf(char[] chs):把字符数组转换成字符串
- static String valueOf(int i):把int类型的数据转成字符串
注意:String类的valueOf方法可以把任意类型的数据转成字符串 - String toLowerCase():把字符串转成小写
- String toLowerCase(Locale locale):将所有在此字符串中的字符使用给定Locale的规则转换为小写
- String toUpperCase():把字符串转成大写
- String toUpperCase(Locale locale):将所有在此字符串中的字符使用给定Locale的规则转换为大写
静态方法
- static String join(CharSequence delimiter, CharSequence… elements):返回一个新的字符串,由CharSequence elements的副本组成,并附有指定的delimiter的delimiter
- static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements):返回一个新String的副本组成CharSequence elements与指定的副本一起加入delimiter
- static String format(String format, Object… args):返回指定的格式化的字符串
- static String format(Locale l, String format, Object… args):使用指定的语言环境、格式字符串和参数返回一个格式化字符串
- static String valueOf(Object obj):将 obj 对象转换成 字符串, 等于 obj.toString()
- static String valueOf(boolean b) : 将 boolean 变量 b 转换成字符串
- static String valueOf(char c) : 将 char 变量 c 转换成字符串
- static String valueOf(char[] data) : 将 char 数组 data 转换成字符串
- static String valueOf(char[] data, int offset, int count) : 将 char 数组 data 中,由 data[offset] 开始取 count 个元素,转换成字符串
- static String valueOf(double d) : 将 double 变量 d 转换成字符串
- static String valueOf(float f) : 将 float 变量 f 转换成字符串
- static String valueOf(int i) : 将 int 变量 i 转换成字符串
- static String valueOf(long l) : 将 long 变量 l 转换成字符串
- static String copyValueOf(char data[], int offset, int count):返回表示所指定的数组中的字符序列的字符串
- static String copyValueOf(char data[]):返回指定数组中表示该字符序列的字符串
其他
- String concat(String str):将指定字符串连接到此字符串的结尾
- void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin):将当前字符串从srcBegin到srcEnd-1位置上的字符复制到字符数组dst中,并从c的dstBegin处开始存放
- int compareTo(String anotherString):按字典顺序比较两个字符串,如果按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数;如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数;如果这两个字符串相等,则结果为0。
- int compareToIgnoreCase(String str):按字典顺序比较两个字符串,与上一个方法不同的是忽略大小写
- String replace(char oldChar, char newChar):将字符串中的oldChar字符替换为newChar字符
- String replaceFirst(String regex, String replacement):使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串
- String replaceAll(String regex, String replacement):使用给定的参数 replacement 替换字符串每一个匹配给定的正则表达式的子字符串
- String replace(CharSequence target, CharSequence replacement):使用指定的replacement替换序列替换此字符串所有匹配target的子字符串
- String[] split(String regex):使用regex进行字符串切割
- String[] split(String regex, int limit):上一个方法的重载,limit用于控制模式匹配使用的次数,可以影响到数组的长度:
· limit>0:
模式匹配将被最多应用n-1次,数组的长度将不会大于n,数组的最后一项将包含所有超出最后匹配的定界符的输入。
· limit<0:
模式匹配将应用尽可能多的次数,而且数组的长度是任何长度。
· limit=0:
模式匹配将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。 - String trim():把字符串中可能存在的首尾空格去掉
- String toString():返回该对象的字符串表示