/** * 关于String类的常用方法总结 * @author lsq * */ public class StringAPIDemo { public static void main(String[] args) { /* * public char[] toCharArray()将一个字符串变成字符数组 * public byte[] getBytes()将一个字符串变成字节数组 */ String str = "Hello"; char[] charArray = str.toCharArray(); byte[] bytes = str.getBytes(); //循环输出字符数组 for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]+","); } //循环输出字节数组 for (int i = 0; i < bytes.length; i++) { System.out.print(bytes[i]+","); } System.out.println(""); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * public String(char[] value)将字符数组变成一个字符串 * public String(char[] value, int offset, int count)将制定范围内的字符变为字符串,offset是起始点,count是长度 * public String(byte[] bytes)将全部的字节数组变成字符串 * public String(byte[] bytes, int offset, int length)将制定范围内的字节数组变成字符串,offset是起始点,count是长度 */ char charArray2[] = {'H','E','L','L','O',' ','J','A','V','A'}; byte bytes2[] = {'1','2','3','4'}; String char2str1 = new String(charArray2); String char2str2 = new String(charArray2, 3, 2); String byte2str1 = new String(bytes2); String byte2str2 = new String(bytes2, 1, 2); System.out.println("char2str1的值是:"+char2str1); System.out.println("char2str2的值是:"+char2str2); System.out.println("byte2str1的值是:"+byte2str1); System.out.println("byte2str2的值是:"+byte2str2); System.out.println(""); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * public char charAt(int index)从一个字符串中取出指定位置的字符 */ String charAtTest = "charAtTest"; char c = charAtTest.charAt(4); System.out.println("charAtTest位置为4的字符是:"+c); System.out.println(""); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * public int length()获取字符串的长度 */ String strLength = "strLength"; int length = strLength.length(); System.out.println(length); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * 查找指定的字符串是否存在: * public int indexOf(String str)从头开始查找字符串是否存在,返回int类型,若查找不到,返回-1 * public int indexOf(String str, int fromIndex)从指定位置开始查找指定的字符串是否存在,返回int类型,找不到返回-1 */ String strindexOf = "abcdefgcdhi"; System.out.println(strindexOf.indexOf("cd")); System.out.println(strindexOf.indexOf("c",3)); System.out.println(strindexOf.indexOf("j")); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * public String trim()去掉字符串中左右的空格,但中间的空格不会去掉 */ String strTrim = " Hello java "; System.out.println(strTrim.trim()); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * 字符串截取:从一个字符串中取出部分内容 * substring(int beginIndex)从指定位置开始截取至字符串结尾、 * subString(int beginIndex, int endIndex)截取指定范围的字符串,不包括结束位置的字符(☆) */ String strsubString = "i love java!"; System.out.println(strsubString.substring(2)); System.out.println(strsubString.substring(2,8)); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * public String[] split(String regex)拆分字符串,以某一个字符串作为拆分点 */ String strsplit = "Hello World! How are you?"; String[] splits = strsplit.split(" "); for (int i = 0; i < splits.length; i++) { System.out.println(splits[i]); } System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * public String toLowerCase():将字符串字符都变为小写 * public String toUpperCase():将字符串字符都变为大写 */ String strToCase = "Hello java!"; System.out.println(strToCase.toLowerCase()); System.out.println(strToCase.toUpperCase()); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * 判断是否以指定的字符串开头或者结尾 * public boolean startsWith(String prefix):判断是否以指定的字符串开头 * public boolean endsWith(String suffix):判断是否以指定的字符串结尾 */ String strWith = "Hello World!"; boolean s = strWith.startsWith("Hel"); System.out.println(s); boolean e = strWith.endsWith("d!"); System.out.println(e); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * 判断两个字符串是否相等: * public boolean equals(Object anObject):区分大小写 * public boolean equalsIgnoreCase(String anotherString):不区分大小写 */ String strEquals1 = "String"; String strEquals2 = "string"; System.out.println(strEquals1.equals(strEquals2)); System.out.println(strEquals1.equalsIgnoreCase(strEquals2)); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); /* * 字符串替换功能 * public String replace(CharSequence target, CharSequence replacement) * public String replaceAll(String regex, String replacement):将指定的字符regex替换成指定的replacement */ String strReplace = "Hello java!"; System.out.println(strReplace.replace('o', 'l')); System.out.println(strReplace.replaceAll("java", "World")); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } }