String类用法总结

来源:互联网 发布:windows内核编程 书籍 编辑:程序博客网 时间:2024/06/03 21:00

String类在编程中出现的频率是非常高的,熟练掌握是很有必要的

一.常用方法总结:

获取方法

1.1:字符串中包含的字符数,也就是字符串的长度。
int length():获取长度

1.2:根据位置获取位置上某个字符。
char charAt(int index)

1.3:根据字符获取该字符在字符串中的位置。
int indexOf(int ch):返回的是ch在字符串中第一次出现的位置。
int indexOf(int ch,int fromIndex):从fromIndex指定位置开始,获取ch在字符串中出现的位置。

int indexOf(String str):返回的是str在字符串中第一次出现的位置。
int indexOf(String str,int fromIndex):从fromIndex指定位置开始,获取str在字符串中出现的位置。

1.4:int lastIndexOf(String str):反向索引。

判断

2.1:字符串中是否包含某一个子串。
boolean contains(str);
特殊之处:indexOf(str):可以索引str第一次出现为止,如果返回-1,表示该str不在字符串中存在。
所以,也可以用于对指定判断是否包含。
if(str.indexOf(“a”)!=1)

而且该方法既可以判断,也可以获取出现的位置。

2.2:字符串中是否有内容。
boolean isEmpty():原理就是判断长度是否为0。

2.3:字符串是否以指定内容开头。
boolean startsWith(str);

2.4:字符串是否以指定内容结尾。
boolean endsWith(str);

2.5:判断字符内容是否相同,复写了object类中的equals方法。
boolean equals(str);

2.6:判断内容是否相同,并忽略大小写。
boolean.equalsIgnorecase();

转换

3.1:将字符数组转成字符串。
构造函数:String(char[])
String(char[],offset,count):将字符数组中的一部分转成字符串
静态方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data,int offset,int count);

static String valueOf(char[]);

3.2:将字符串转成字符组
char[] tocharArray();

3.3:将字节数组转成字符串。
String(byte[])
String(byte[],offset,count):将字节数组中的一部分转成字符串

3.4:将字符串转成字节数组。
byte[] getBytes()

3.5:将基本数据类型转成字符串,
static String valueOf(int)
static String valueOf(double)

// 3+”” 与 String.valueOf(3)的值是一样的
特殊:字符串和字节数组在转换过程中,是可以指定编码的。

替换

String replace(oldchar,newchar);

切割

String[] split(regex);

子串。获取字符串中的而一部分

String subString(begin);
String subString(begin,end);

转换,去除空格,比较。

7.1:将字符串转成大写或小写
String toUpperCsae() 大转小
String toLowerCsae() 小转大

7.2:将字符串两端的多个空格去除
String trim();

7.3:对两个字符串进行自然顺序的比较
int compareTo(string);

全部api总结:

构造方法:

String()
初始化一个新创建的 String 对象,它表示一个空字符序列。
String(byte[] bytes)
构造一个新的 String,方法是使用平台的默认字符集解码字节的指定数组。
String(byte[] bytes, int offset, int length)
构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。
String(byte[] bytes, int offset, int length, String charsetName)
构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。
String(byte[] bytes, String charsetName)
构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
String(char[] value)
分配一个新的 String,它表示当前字符数组参数中包含的字符序列。
String(char[] value, int offset, int count)
分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。
String(int[] codePoints, int offset, int count)
分配一个新的 String,它包含该 Unicode 代码点数组参数的一个子数组的字符。
String(String original)
初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。
String(StringBuffer buffer)
分配一个新的字符串,它包含当前包含在字符串缓冲区参数中的字符序列。
String(StringBuilder builder)
分配一个新的字符串,它包含当前包含在字符串生成器参数中的

字符序列

方法总结:

char charAt(int index)
返回指定索引处的 char 值。
int codePointAt(int index)
返回指定索引处的字符(Unicode 代码点)。
int codePointBefore(int index)
返回指定索引之前的字符(Unicode 代码点)。
int codePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本范围中的 Unicode 代码点数。
int compareTo(String anotherString)
按字典顺序比较两个字符串。
int compareToIgnoreCase(String str)
不考虑大小写,按字典顺序比较两个字符串。
String concat(String str)
将指定字符串联到此字符串的结尾。
boolean contains(CharSequence s)
当且仅当此字符串包含 char 值的指定序列时,才返回 true。
boolean contentEquals(CharSequence cs)
当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true。
boolean contentEquals(StringBuffer sb)
当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true。
static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的字符串。
static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的字符串。
boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
boolean equals(Object anObject)
比较此字符串与指定的对象。
boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 进行比较,不考虑大小写。
static String format(Locale l, String format, Object… args)
使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
static String format(String format, Object… args)
使用指定的格式字符串和参数返回一个格式化字符串。
byte[] getBytes()
使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
byte[] getBytes(String charsetName)
使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
int hashCode()
返回此字符串的哈希码。
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
int indexOf(int ch, int fromIndex)
从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。
int indexOf(String str)
返回第一次出现的指定子字符串在此字符串中的索引。
int indexOf(String str, int fromIndex)
从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
String intern()
返回字符串对象的规范化表示形式。
int lastIndexOf(int ch)
返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(int ch, int fromIndex)
从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(String str)
返回在此字符串中最右边出现的指定子字符串的索引。
int lastIndexOf(String str, int fromIndex)
从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
int length()
返回此字符串的长度。
boolean matches(String regex)
通知此字符串是否匹配给定的正则表达式。
int offsetByCodePoints(int index, int codePointOffset)
返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串匹配字面值目标序列的每个子字符串。
String replaceAll(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。
String replaceFirst(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的第一个子字符串。
String[] split(String regex)
根据给定的正则表达式的匹配来拆分此字符串。
String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。
boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
boolean startsWith(String prefix, int toffset)
测试此字符串是否以指定前缀开始,该前缀以指定索引开始。
CharSequence subSequence(int beginIndex, int endIndex)
返回一个新的字符序列,它是此序列的一个子序列。
String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
char[] toCharArray()
将此字符串转换为一个新的字符数组。
String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
String toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
String toString()
返回此对象本身(它已经是一个字符串!)。
String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
String toUpperCase(Locale locale)
使用给定的 Locale 规则将此 String 中的所有字符都转换为大写。
String trim()
返回字符串的副本,忽略前导空白和尾部空白。
static String valueOf(boolean b)
返回 boolean 参数的字符串表示形式。
static String valueOf(char c)
返回 char 参数的字符串表示形式。
static String valueOf(char[] data)
返回 char 数组参数的字符串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 数组参数的特定子数组的字符串表示形式。
static String valueOf(double d)
返回 double 参数的字符串表示形式。
static String valueOf(float f)
返回 float 参数的字符串表示形式。
static String valueOf(int i)
返回 int 参数的字符串表示形式。
static String valueOf(long l)
返回 long 参数的字符串表示形式。
static String valueOf(Object obj)
返回 Object 参数的字符串表示形式。

参考:
http://blog.csdn.net/lanruqin/article/details/4839920
http://www.webtag123.com/java/18823.html

我的微信二维码如下,欢迎交流讨论

这里写图片描述

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

这里写图片描述

12 2
原创粉丝点击