String : 字符串类型
一、构造函数
String(byte[ ] bytes):通过byte数组构造字符串对象。
String(char[ ] value):通过char数组构造字符串对象。
String(Sting original):构造一个original的副本。即:拷贝一个original。
String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。
例如:
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 = new String(b); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_copy = new String(sb); //abcdefghij
System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_copy:"+sb_copy);
输出结果:sb:abcdefghij
sb_sub:de
sc:0123456789
sc_sub:34
sb_copy:abcdefghij
二、方法:
说明:①、所有方法均为public。
②、书写格式: [修饰符] <返回类型><方法名([参数列表])>
例如:static int parseInt(String s)
表示此方法(parseInt)为类方法(static),返回类型为(int),方法所需要为String类型。
写个main方法,直接将这些new String()按照文档全部写完就行了,文档上都把每个构造函数的各个参数的意义都说清楚了,你看清楚意思,再添加进去就OK了
汗,学东西不是你这样学的,
搜索使用主要的,其次是要知道每隔构造函数的作用,有待以后使用。没有你这么玩的,
如果这样有很多 对象都有很多构造方法那你不是要崩溃。
String s1 = new String();//.......这个..不说了.
byte[] a = s1.getBytes();
try {
String s2 = new String(a);//第二个构造..把一个byte数组组合成一个字符串,
String s7 = new String (a,2, 3,"GB2312");//第七个构造..把一个byte数组以某个字符集的编码组合成一个字符串,
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
既然你都有文档了...还有问题.只能说明,你很懒.....!!
真的很蛋疼
import java.io.UnsupportedEncodingException;
public class StrConstructor {
public static void main(String[] args) throws UnsupportedEncodingException {
//String()
String one = new String();
System.out.println("String() -->" + one);
//String(byte[] bytes)
byte[] byteAry = {1, 3, 5, 7, 8};
String two = new String(byteAry);
System.out.println("String(byte[] bytes)-->" + two);
//String(byte[] ascii, int hibyte)
byte[] ary2 = {45, 66, 127, 90};
String three = new String(ary2, 0);
System.out.println("String(byte[] ascii, int hibyte)" + three);
//String(byte[] bytes, int offset, int length)
byte[] ary3 = {25, 33, 11,25, 123, 25};
String four = new String(ary3, 2, 2);;
System.out.println("String(byte[] bytes, int offset, int length)--->" + four);
//String(byte[] ascii, int hibyte, int offset, int count)
byte[] ary4 = {22, 37, 65, 73, 115, 91};
String five = new String(ary4, 0, 1, 3);
System.out.println("String(byte[] ascii, int hibyte, int offset, int count)" + "-->" + five);
//String(byte[] bytes, int offset, int length, String charsetName)
byte[] ary5 = {11, 33, 23, 56, 73, 39, 83};
String six = new String(ary5, 2, 3, "US-ASCII");
System.out.println("String(byte[] bytes, int offset, int length, String charsetName)" + "-->" + six);
//String(byte[] bytes, String charsetName)
byte[] ary6 = {11, 33, 55, 34, 67};
String seven = new String(ary6, "UTF-8");
System.out.println("String(byte[] bytes, String charsetName)" + "-->" + seven);
// String(char[] value)
char[] ary7 = {'a', 'A', 'e', 'H', 'f', ' ', 'K'};
String eight = new String(ary7);
System.out.println("String(char[] value)" + "-->" + eight);
//String(char[] value, int offset, int count)
char[] ary8 = {'z', 'z', 'b', ' ', 'C', 'e', 'M', 'L', 'G', 'B'};
String nine = new String(ary8, 2, 5);
System.out.println("String(char[] value, int offset, int count) " + "-->" + nine);
//String(int[] codePoints, int offset, int count)
int[] codePoints = {11, 22, 33, 55, 77};
String ten = new String(codePoints, 1, 2);
System.out.println("String(int[] codePoints, int offset, int count)" + "-->" + ten);
//String(String original)
String org = new String("original");
System.out.println("String(String original) " + "-->" + org);
//String(StringBuffer buffer)
StringBuffer sb = new StringBuffer();
sb.append("abcd");
sb.append(1234);
sb.append("ABCDEFG");
String eleven = new String(sb);
System.out.println("String(StringBuffer buffer)" + "-->" + eleven);
//String(StringBuilder builder)
StringBuilder sbld = new StringBuilder();
sbld.append("abcd");
sbld.append(1234);
sbld.append("ABCDEFG");
String tweleve = new String(sbld);
System.out.println("String(StringBuilder builder)--->" + tweleve);
}
}