@(Java知识点总结)[Java, Java经常使用类]java
[toc]数组
char c1 = 'a';安全
char c2 = 'n'; //表明换行符app
char c3 = 'a'; int i = c3+2; System.out.println(i); //99 char c4 = (char) i; System.out.println(c4); //c
String类对象建立后不能修改,由0或多个字符组成,包含在一对双引号之间ide
public final class Stringui
implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; //声明一个不可变数组用来存放字符 public String(String original) { this.value = original.value; this.hash = original.hash; }
public String(char value[]) {this
this.value = Arrays.copyOf(value, value.length); }
水果 | 价格 |
---|---|
public String() | 无参构造方法,用来建立空字符串的String对象 |
public String(String str) | 用已知的字符串str建立一个String对象 |
public String(char[] chars) | 用字符数组chars建立一个String对象 |
public String(byte[] bytes) | 用byte数组bytes建立一个String对象 |
public String(char[] chars, int startIndex, int numChars) | 用字符数组chars的startIndex开始的numChars个字符建立一个String对象 |
public String(StringBuffer buffer) | 用StringBuffer 建立一个String对象 |
public class Demo1 { // String 对象的建立 public static void test1() { String s = "apple"; String s1 = new String(); String s2 = new String("apple"); String s3 = new String(s2); byte[] bytes = new byte[] {1,2,3}; String s4 = new String(bytes); char[] chars = { 'a', 'b', 'c', 'd', 'e','f' }; String s5 = new String(chars); // abcdef String s6 = new String(chars, 1, 4); //bcde } // 经常使用方法 public static void test2(){ String s1 = "apple"; String s2 = " a,p,p l e "; System.out.println(s1+s2); //字符串链接 System.out.println(s1.length()); //字符串长度 System.out.println(s1.charAt(2)); // 获取指定未知的字符 char[] charArray = s1.toCharArray(); //转换为char[] System.out.println(s1.substring(1, 3)); //截取字符串 String[] ss = s2.split(","); // 用,分割字符串 for (String s : ss) { System.out.println(s); } System.out.println(s2.trim()); //去掉两端的空格 if (s1.equals(s2)) { //字符串比较 System.out.println(s1+"=="+s2); }else { System.out.println(s1+"!="+s2); } String s3 = "java is a good computer language"; System.out.println(s3.indexOf("o")); //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。 System.out.println(s3.indexOf("o",13)); // 从13开始向右查找 System.out.println(s3.indexOf("good")); //查找字符串 System.out.println(s3.lastIndexOf("o")); //从右往左查找 System.out.println(s3.toUpperCase()); //转大写字母 System.out.println(s3.startsWith("a")); //s3是否以a开头 System.out.println(s3.contains("good")); //s3是否包含"good" } // 字符串与基本类型的转换 public static void test3(){ //字符类型转为基本类型 System.out.println(Integer.parseInt("123")); System.out.println(Double.parseDouble("123.4")); System.out.println(Float.parseFloat("123.456f")); // 基本类型转为字符类型 System.out.println(String.valueOf(123.4)); //进制转换 String binaryString = Long.toBinaryString(123); //二进制 String octalString = Long.toOctalString(123); //8 String hexString = Long.toHexString(123); //16 String string = Long.toString(123,16); //任意进制 System.out.println(binaryString); System.out.println(octalString); System.out.println(hexString); System.out.println(string); } public static void main(String[] args) { test2(); test3(); } }
public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence{ public StringBuilder() { super(16); } public StringBuilder(String str) { super(str.length() + 16); append(str ); } public AbstractStringBuilder append(String str ) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) expandCapacity(minimumCapacity); } void expandCapacity(int minimumCapacity) { //数组扩容 int newCapacity = value .length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); }
public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence { @Override public synchronized int length() { return count; } @Override public synchronized int capacity() { return value.length; }
public class Demo2 { public static void main(String[] args) { test2(); } // 新建对象 private static void test1() { StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = new StringBuffer(20); StringBuffer sb3 = new StringBuffer("apple"); } // 经常使用方法 private static void test2() { StringBuffer sb = new StringBuffer(); sb.append("java"); //字符串表示形式追加到序列 sb.append("is a good computer language"); System.out.println(sb.toString()); //转换为string System.out.println(sb.charAt(11)); //返回此序列中指定索引处的 char 值 sb.delete(3, 15); //移除此序列的子字符串中的字符 System.out.println(sb.toString()); char[] chars = new char[20]; sb.getChars(4, 11,chars, 0); //将字符今后序列复制到目标字符数组 dst。 System.out.println(new String(chars)); System.out.println(sb.charAt(9)); //返回第一次出现的指定子字符串在该字符串中的索引。 System.out.println(sb.insert(2, "6666").toString()); //将字符串插入此字符序列中。 System.out.println(sb.replace(2, 6, "888")); //使用给定 String 中的字符替换此序列的子字符串中的字符。 System.out.println(sb.reverse()); //将此字符序列用其反转形式取代。 System.out.println(sb.lastIndexOf("8")); //返回最右边出现的指定子字符串在此字符串中的索引。 System.out.println(sb.substring(5,15).toString()); //返回一个新的字符序列,该字符序列是此序列的子序列。 System.out.println(sb.length()); //返回长度 } }