一、String构造器
1、String()
Initializes a newly created
String
object so that it represents an empty character sequence.
初始化一个新创建的字符串对象,它代表了一个空的字符序列
2、String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default charset.
构造一个新的字符,解码的字节数组指定使用平台的默认字符集。
3、String(byte[] bytes,Charset charset)
Constructs a new String by decoding the specified array of bytes using the specified charset.
构造一个新的字符串,解码的字节数组指定使用指定的字符集。
4、
String(byte[] ascii, int hibyte)
已弃用。
5、
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.
构造一个新的字符串,解码的字节是指定的子数组使用平台的默认字符集。
6、
String(byte[] bytes, int offset, int length,Charset charset)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
构造一个新的字符串,解码的字节是指定的子数组使用指定的字符集。
7、
String(byte[] ascii, int hibyte, int offset, int count)
已弃用。
8、
String(byte[] bytes, int offset, int length,String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
构造一个新的字符串,解码的字节是指定的子数组使用指定的字符集。
9、
String(byte[] bytes,String charsetName)
Constructs a new String by decoding the specified array of bytes using the specified charset.
构造一个新的字符串,解码的字节数组指定使用指定的字符集。
10、
String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
分配一个新的字符串,它代表了当前字符序列中包含的字符数组参数。
11、
String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument.
分配一个新的字符串,它包含字符的子数组的字符数组参数。
12、
String(int[] codePoints, int offset, int count)
Allocates a new String that contains characters from a subarray of the Unicode code point array argument.
分配一个新的字符串,它包含字符的Unicode代码点阵列的子数组参数。
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
初始化一个新创建的字符串对象,它代表了相同的字符序列作为参数;换句话说,新创建的字符串参数是原字符串的副本。
14、
String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
分配一个新的字符串,它包含当前字符序列中包含字符串缓冲区的部分。
15、
String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.
分配一个新的字符串,它包含当前字符序列中包含字符串生成器的部分。
例如:
byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c ={'0','1','2','3','4','5','6','7','8','9'};
String sb = newString(b); //abcdefghij
String sb_sub = newString(b,3,2); //de
String sc = newString(c); //0123456789
String sc_sub = newString(c,3,2); //34
String sb_copy = newString(sb); //abcdefghij
System.out.println("sb:"+sb); //sb:abcdefghij
System.out.println("sb_sub:"+sb_sub);//sb_sub:de
System.out.println("sc:"+sc); //sc:0123456789
System.out.println("sc_sub:"+sc_sub);//sc_sub:4
System.out.println("sb_copy:"+sb_copy);//sb_copy:abcdefghij
二、String方法
①、所有方法均为public。
②、书写格式: [修饰符] <返回类型><方法名([参数列表])>
例如:static int parseInt(String s)
表示此方法(parseInt)为类方法(static),返回类型为(int),方法所需要为String类型。
1.char charAt(int index)
取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。
例如:
String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
结果为:s.charAt(5): f
2. int compareTo(String anotherString)
当前String对象与anotherString比较。
相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。
例如:
String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) );//返回长度差
System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) );//返回'k'-'a'的差
结果为:s1.compareTo(s2):4
s1.compareTo(s3): 10
将该String对象与str连接在一起。
4. boolean contentEquals(StringBuffer sb)
将该String对象与StringBuffer对象sb进行比较。
5. static String copyValueOf(char[] data)
将char数组转换成String,与其中一个构造函数类似
6. static String copyValueOf(char[] data, int offset, int count)
这两个方法将char数组转换成String,与其中一个构造函数类似。
7. boolean endsWith(String suffix)
该String对象是否以suffix结尾。
例如:
String s1 = newString("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
结果为:s1.endsWith(s2):true
8. boolean equals(Object anObject)
当anObject不为空并且与当前String对象一样,返回true;否则,返回false。
9. byte[] getBytes()
将该String对象转换成byte数组。
10. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。
例如:
char[] s1 = {'I',' ','l','o','v','e','','h','e','r','!'};//s1=I love her!
String s2 = new String("you!");
s2.getChars(0,3,s1,7); //s1=Ilove you!
System.out.println( s1 );
结果为:I loveyou!
11. int hashCode()
返回当前字符的哈希表码。
12. int indexOf(int ch)
只找第一个匹配字符位置。
13. int indexOf(int ch, int fromIndex)
从fromIndex开始找第一个匹配字符位置。
只找第一个匹配字符串位置。
15. int indexOf(String str, int fromIndex)
从fromIndex开始找第一个匹配字符串位置。
例如:
String s =new String("write once, run anywhere!");
String ss = newString("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
结果为:s.indexOf('r'): 1
s.indexOf('r',2): 12
s.indexOf(ss): 12
16. int lastIndexOf(int ch)
只找最后一个匹配字符位置。
17. int lastIndexOf(int ch, int fromIndex)
从fromIndex开始找最后一个匹配字符位置。
18. int lastIndexOf(String str)
只找最后一个匹配字符位置。
19. int lastIndexOf(String str, int fromIndex)
从fromIndex开始找最后一个匹配字符位置。
public class CompareToDemo {
public static void main (String[] args) {
String s1 = new String("acbdebfg");
System.out.println(s1.lastIndexOf((int)'b',7));
}
}
运行结果:5
(其中fromIndex的参数为 7,是从字符串acbdebfg的最后一个字符g开始往前数的位数。即是从字符c开始匹配,寻找最后一个匹配b的位置。所以结果为 5)
20. int length()
返回当前字符串长度。
21. String replace(char oldChar, char newChar)
将字符号串中第一个oldChar替换成newChar。
22. boolean startsWith(String prefix)
该String对象是否以prefix开始。
23. boolean startsWith(String prefix, int toffset)
该String对象从toffset位置算起,是否以prefix开始。
例如:
String s = new String("writeonce, run anywhere!");
String ss = new String("write");
String sss = new String("once");
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );
System.out.println("s.startsWith(sss,6): " +s.startsWith(sss,6) );
结果为:s.startsWith(ss):true
s.startsWith(sss,6): true
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()
将字符串转换成大写。
例如:
String s = newString("java.lang.Class String");
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
System.out.println("s.toLowerCase(): " + s.toLowerCase() );
结果为:
s.toUpperCase():JAVA.LANG.CLASS STRING
s.toLowerCase():java.lang.class string
29. static String valueOf
(boolean b)
30. static String valueOf
(char c)
31. static String valueOf
(char[] data)
32. static String valueOf
(char[] data, intoffset, int count)
33. static String valueOf
(double d)
34. static String valueOf
(float f)
35. static String valueOf
(int i)
36. static String valueOf
(long l)
37. static String valueOf
(Object obj)
以上(29-37)方法用于将各种不同类型转换成Java字符型。这些都是类方法。
38.public boolean equalsIgnoreCase(String anotherString)
比较字符串与another是否一样(忽略大小写);
39.public String trim()
返回该字符串去掉开头和结尾空格后的字符串
40.public String[] split(String regex)
将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
实例1:
<pre name="code" class="java">public class SplitDemo{
public static void main(String[] args) {
String date = "2008/09/10";
String[] dateAfterSplit= new String[3];
dateAfterSplit=date.split("/"); //以“/”作为分隔符来分割date字符串,并把结果放入3个字符串中。
for(int i=0;i<dateAfterSplit.length;i++){
System.out.print(dateAfterSplit[i]+"");
}
}
}
运行结果:20080910 //结果为分割后的3个字符串
实例2:
TestString1.java程序代码
<pre name="code" class="java">public class TestString1
{
public static void main(String args[]){
String s1 ="Hello World" ;
String s2 ="hello world" ;
System.out.println(s1.charAt(1)) ;//e
System.out.println(s2.length()) ;//11
System.out.println(s1.indexOf("World")) ;//6
System.out.println(s2.indexOf("World")) ;//-1
System.out.println(s1.equals(s2)) ;//false
System.out.println(s1.equalsIgnoreCase(s2)) ;//true
String s ="我是J2EE程序员" ;
String sr =s.replace('我','你') ;
System.out.println(sr) ;//你是J2EE程序员
}
}
TestString2.java程序代码
public class TestString2
{
public static void main(String args[]){
String s ="Welcome to Java World!" ;
String s2 =" magci " ;
System.out.println(s.startsWith("Welcome")) ;//true
System.out.println(s.endsWith("World")) ;//false
String sL =s.toLowerCase() ;
String sU =s.toUpperCase() ;
System.out.println(sL) ;welcome to java world!
System.out.println(sU) ;WELCOME TO JAVA WORLD!
String subS =s.substring(11) ;
System.out.println(subS) ;Java World!
String s1NoSp= s2.trim() ;
System.out.println(s1NoSp) ;magci
}