String构造器中文手册

String类提供了大量的构造器来创建String对象,其中如下几个有特殊用途

1.String():创建一个包含0个字符串序列的String对象(并不是返回null)

2.String(byte[] bytes,Charset charset):使用指定的字符集将指定的byte[]数组解码成一个新的String对象

3.String(byte[] bytes,int offset,int length):使用平台的默认字符集将指定的byte[]数组从offset开始、长度的子数组解码成一个新的String对象

4.Sting(byte[],int offset,int length,String chatName):使指定的字符集将指定的byte[]数组从offset开始,长度为length的子数组解码成一个新的String对象

5.String(byte[] bytes,String charsetName):使用指定的字符集将指定的byte[]数组解码成一个新的String对象

6.String(byte[] value,int offset,int count):将指定的字符串从offset开始,长度为count的字符元素连缀成字符串

7.String(String original):根据字符串直接量来创建一个String对象,也就是说,新创建的String对象是该参数字符串的副本。

8.String(StringBuffer buffer):根据StringBuffer对象来创对应的String对象

9.String(StringBuilder builder):根据StringBuilder对对象来创建对应的String对象

String对象也提供了大量的方法来操作字符串对象,

1.char charAt(int index):获取字符串中指定位置的字符。其中,参数index值得是字符串的序数,字符串的序数从0开始到length()-1

String s=new String("fkit.org");
System.out.println("s.charAt(5):"+s.charAt(5));
2.int compareTo(String anotherString):比较两个字符串的大小。如果两个字符串的字符序列相等,则返回0;不相等时,从两个字符串第0个字符第0个字符开始比较,返回第一个不相等的字符差。另一种情况,较长字符串的前面部分恰好是较短的字符串,则返回它的长度差。
String s1=new String("abcdefghijklmn");
String s2=new String("abcdefghij");
String s3=new String("abcdefghijklmn");
System.out.println("s1.compareTo(s2):"+s1.compareTo(s2));//输出s1.compareTo(s2):4
System.out.println("s1.compareTo(s3):"+s1.compareTo(s3));//输出s1.compateTo(s3):0
System.out.println("s2.compareTo(s1):"+s2.compareTo(s1));//输出s2.compareTo(s1):-4
3.String concat(String str):将该String对象与str对象链接在一起。与Java提供的字符串链接运算符“+”的功能相同

4.boolean contentEquals(StringBuffer sb):将String对象与StringBuffer对象sb进行比较当他们包含相同的字符串序列相同时返回true

5.static String copyValueOf(char[] data):将字符数组连缀成字符串,与String(char[] content)构造器功能相同

6.static String copyValueOf(char[] data,int offerset,int count):将char数组的子数组中的元素连缀成字符串,与String(char[] value,int offerset,int count)用法相同

7.boolean endsWith(String suffix):返回该String对象是否以suffix结尾

String s1="fkit.org";
String s2=".org";
System.out.println("s1.endsWith(s2):"+s1.endsWith(s2));
8.boolean equals(Object anObject):将该字符串与指定对象比较,如果二者包含的字符串序列相同的,则返回true,否则返回false。

9.boolean equalsIgnoreCase(String str):与前一个方法基本相似,知识忽略字符的大小写。

10.byte[] getByte():将该String对象转换为byte数组

11.void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin):该方法将字符串从srcBegin开始,到srcEnd结束的字符复制到dst字符数组中去,其中dstBegin为目标字符数字组的起始复制位置。

12.int indexOf(char ch):找出ch字符在该字符串中第一次出现的位置

13.int indexOf(char ch,int fromIndex):找出ch字符在该字符串中从fromIndex开始后第一次出现的位置

14.int indexOf(String str):找出str子字符串在该字符串中第一次出现的位置

15.int indexOf(String str,int fromIndex):找出str字符串在该字符串中从fromIndex开始后第一次出现的位置。

String s="www.fkit.org";
String ss="it";
System.out.println("s.indexOf('r'):"+s.indexOf('r'));//输出s.indexOf('r'):10
System.out.println("s.indexOf('r',2):"+s.indexOf('r',2));//输出s.indexOf('r',2):10
System.out.println("s.indexof(ss):"+s.indexOf(ss));//输出s.indexOf(ss):6
System.out.println(s.length());

16.int lastIndexOf(int ch):找出ch字符在该字符串中最后一次出现的位置

17.int lastIndexOf(int ch,int fromIndex):找出ch字符在该字符串中从fomIndex开始到最后一次出现的位置

18.int lastIndexOf(String str):找出str子字符串中最后一次出现的位置

19.int lastIndexOf(String str,int fromIndex):找出str子字符串从fromIndex开始到最后一次出现的位置

20.int length():返回当前字符串的长度

21.String replace(char oldChar,char newChar):将字符串中第一个oldChar替换成newChar

22.boolean startsWith(String prefix):该String对象是否以prefix开始

23.boolean startsWith(String perfix,int toffset):该String对象从toffset对象开始是否以perfix开始。

String s="www.fkit.org";
String ss="www";
String sss="fkit";
System.out.println("s.startsWith(ss):"+s.startsWith(ss));
System.out.println("s.statrtsWith(ss,4):"+s.startsWith(ss, 4));

24.String substring(int beginIndex):获取从beginIndex位置开始到结束的字符串

25.String substring(int beginIndex,int endIndex):获取从beginIndex位置开始到endIndex位置的子字符串

26.char[] toCharArray():将该String对象转换成char数组

27.String toLowerCase():将字符串转换为小写

28.String toUpperCase():将字符串转换为大写

29.static String valueOf(X x):一系列将基本类型值转换为String对象方法



猜你喜欢

转载自blog.csdn.net/xiao_chainiao/article/details/76228670