String:字符串类型 1、构造函数。 String() :构造一个空字符串对象。 String(byte[] bytes) :通过byte数组构造字符串对象。 String(byte[] bytes, int offset, int length) :通过byte数组,从offset开始,总共length长的字节构造字符串对象。 String(char[] value) :通过char数组构造字符串对象。 String(char[] value, int offset, int count) :通过char数组,从offset开始,总共length长的字节构造字符串对象。 String(String original) :构造一个original的副本。既,拷贝一个original。 String(StringBuffer buffer) :通过StringBuffer数组构造字符串对象; byte[] b = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘}; 2、方法。 说明: 1. 所有方法均为public; 2. 书写格式:[修饰符] <返回类型> <方法名([参数列表])> 如: static int parseInt(String s) 表示:此方法(parseInt)为类方法(static),返回类型为(int),方法所需参数为String类型。 1. char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。 String s = new String("abcdefghijklmnopqrstuvwxyz");
3. int compareTo(Object o) :如果o是String对象,和2的功能一样;否则抛出ClassCastException异常。 String s1 = new String("abcdefghijklmn"); 4. String concat(String str) :将该String对象与str连接在一起。 5. boolean contentEquals(StringBuffer sb) :将该String对象与StringBuffer对象sb进行比较。 6. static String copyValueOf(char[] data) : 7. static String copyValueOf(char[] data, int offset, int count) :这两个方法将char数组转换成String,与其中一个构造函数类似。 8. boolean endsWith(String suffix) :该String对象是否以suffix结尾。 String s1 = new String("abcdefghij"); 9. boolean equals(Object anObject) :当anObject不为空并且与当前String对象一样,返回true;否则,返回false。 10. byte[] getBytes() :将该String对象转换成byte数组。 11. 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!
13. int indexOf(int ch) :只找第一个匹配字符位置。 14. int indexOf(int ch, int fromIndex) :从fromIndex开始找第一个匹配字符位置。 15. int indexOf(String str) :只找第一个匹配字符串位置。 16. int indexOf(String str, int fromIndex) :从fromIndex开始找第一个匹配字符串位置。 String s = new String("write once, run anywhere!");
18. int lastIndexOf(int ch, int fromIndex) 19. int lastIndexOf(String str) 20. int lastIndexOf(String str, int fromIndex) 以上四个方法与13、14、15、16类似,不同的是:找最后一个匹配的内容。 21. int length() :返回当前字符串长度。 22. String replace(char oldChar, char newChar) :将字符号串中第一个oldChar替换成newChar。 23. boolean startsWith(String prefix) :该String对象是否以prefix开始。 24. boolean startsWith(String prefix, int toffset) :该String对象从toffset位置算起,是否以prefix开始。 String s = new String("write once, run anywhere!");
26.String substring(int beginIndex, int endIndex) :取从beginIndex位置开始到endIndex位置的子字符串。 27. char[] toCharArray() :将该String对象转换成char数组。 28. String toLowerCase() :将字符串转换成小写。 29. String toUpperCase() :将字符串转换成大写。 String s = new String("java.lang.Class String");
31. static String valueOf(char c) 32. static String valueOf(char[] data) 33. static String valueOf(char[] data, int offset, int count) 34. static String valueOf(double d) 35. static String valueOf(float f) 36. static String valueOf(int i) 37. static String valueOf(long l) 38. static String valueOf(Object obj) 以上方法用于将各种不同类型转换成Java字符型。这些都是类方法。 |
|