import javax.xml.crypto.Data;
public class Stringxuexi {
public static void main(String[] argc)
{
//charAt(int index) 返回index處的Unicode字符
String strCom = "JAVA程序設計";
System.out.println(strCom.charAt(4));
//codePointAt(int index) 返回index處字符的Unicode編碼值
strCom = "I like JAVA ,too";
System.out.println(strCom.codePointAt(8));
//codePointBefore 返回index-1處字符的Unicode編碼值
System.out.println(strCom.codePointBefore(2));
//codePointCount(int beginIndex,int endIndex)方法 返回指定文本范圍內Unicode代碼點的數量
System.out.println(strCom.codePointCount(0, 3));
//compareTo(String str)
//如果兩個字符串不同,那么他們要么在某個索引處的字符不同,要么長度不同,或者同時具備這兩種情況
//如果在一個或多個索引處字符不同,假設k是這類索引的最小值,那么返回值就是這兩個字符串在位置k處
//兩個char值之差,如果沒有字符不同的索引位置,則返回值是兩個字符串長度的差
System.out.println(strCom.compareTo("I like PHP"));
System.out.println(strCom.compareTo("I like JAVA too"));
//compareToIgnoreCase(String str) 忽略大小寫比較字符串大小
System.out.println(strCom.compareToIgnoreCase("I Like PHP"));
//concat(String str) 將另一字符串連接在此字符串的后面,如果參數字符串的長度為0,
//則返回此字符串,否則創建一個新的String對象
System.out.println(strCom.equals(strCom.concat("")));
System.out.println(strCom.concat(strCom));
//contains(CharSequence s)判斷字符串是否包含指定的char值序列
System.out.println(strCom.contains("JAVA"));
//valueOf(char []data) 靜態方法,返回包含字符數組的字符的字符串
char [] array={'山','東'};
System.out.println(String.valueOf(array));
//valueOf(char[] data,int offset,int count)返回包含字符數組從offset處開始的count個字符
//組成的字符串
System.out.println(String.valueOf(array, 0, 1));
//endwith(String suffix)測試字符串是否是指定的后綴結束
System.out.println(strCom.endsWith("JAVA"));
//equals(object obj) 如果給定的對象表示的String與此String相等,則返回true,否則false
System.out.println(strCom.equals("I like JAVA"));
//equalsIgnoreCase(String anotherString) //忽略大小寫與另一字符串進行比較,注意與equals方法的參數類型不同
System.out.println(strCom.equalsIgnoreCase("I Like JAva"));
//format(String format,Object ...args)靜態方法 使用指定的格式字符串和參數返回一個格式話字符串
//%d 格式化為十進制整數
//%o 格式化為八進制整數
//%x %X 格式化為十六進制整數
System.out.println(String.format("%e %x %o %d %a %% %n", 15.000,15,15,15,15.0));
//format(Locale l,String format,Object ... args)
//通過給定的特殊轉換符作為參數來實現對日期和時間字符串的格式化
//%te 一個月中的某一天
//%tb 指定語言環境的月份簡稱
//%tB 指定語言環境的月份全稱
//%tA 指定語言環境的星期幾全稱
//%ta 指定語言環境的星期幾簡稱
//%tc 包括全部日期和時間信息
//%tY 4位年份
//%ty 二位年份
//%tm 月份
//%tj 一年中的第幾天
//%td 一個月中的第幾天
Date date = new Date();
Locale form = Locale.CHINA;
String year = String.format(form, "%tY", date);
String month = String.format(form, "%tm", date);
String day = String.format(form, "%td", date);
System.out.println("今天是: "+ year + "年"+month+"月"+day+"日");
System.out.println(String.format(form, "%tc", date));
//byte[] getBytes() 得到字符串的byte序列
byte[] str = strCom.getBytes();
for (int i = 0;i < str.length;i++)
System.out.print(str[i]+" ");
//getBytes(Charset charset)
//getBytes(String string)
//得到編碼字符集的所得字符序列
try {
str = strCom.getBytes(Charset.defaultCharset());
for (int i = 0; i < str.length; i++)
System.out.println(str[i] + " ");
} catch (UnsupportedCharsetException e) {
// TODO: handle exception
e.printStackTrace();
}
//getchars(int srcBegin,int srcEnd,char[] dst,int dstBegin)
//將字符從此字符串復制到目標字符數組
char[] dst = new char[10];
strCom.getChars(0, 10, dst, 0);
for (int i = 0; i < dst.length;i++)
System.out.print(dst[i]);
System.out.println();
//hashCode() 返回字符串的哈希碼,String對象的哈希碼的計算公式是
//s[0]*31^(n-1)+s[1]*31^(n-2)+...+s[n-1]
//空串的哈希碼為0
System.out.println(strCom.hashCode());
//indexOf(int ch) 獲取字符的第一個索引,ch是一個字符,如果沒有,返回-1
System.out.println(strCom.indexOf('A'));
//indexOf(int ch,int fromIndex) //返回從從指定的索引處開始的指定字符的索引
//fromIndex沒有限制,如果為負,與0等效,如果大于等于字符串長度,則返回-1
System.out.println(strCom.indexOf('A', 9));
//indexOf(String str)
//indexOf(String str,int fromIndex)
//返回指定字符串在此字符串第一次出現處的索引
System.out.println(strCom.indexOf("JAVA"));
//intern() 返回字符串對象的規范化表示形式
//當調用intern方法時,如果池已經包含一個等于此String對象的字符串,則返回池中的字符串
//否則將此字符串對象添加到池中,并返回此String對象引用
//了解這個處理機制也可以讓我們在用到字符串常量的時候了解如何節省這些字符串所占用的內存。
String strCom2 = new String("I like JAVA");
System.out.println(strCom == strCom2);
System.out.println(strCom.endsWith(strCom2));
System.out.println(strCom.compareTo(strCom2));
System.out.println(strCom.intern() == strCom2.intern());
String s1 = new String("你好,Java自由人");
String s2 = new String("你好,") + "Java自由人";
System.out.println(s1==s2);
System.out.println(s1.intern()==s2.intern());
//同indexOf,注意fromIndex 參數,是指從fromIndex處反向搜索
System.out.println(strCom.lastIndexOf('A'));
System.out.println(strCom.lastIndexOf('A',10));
System.out.println(strCom.lastIndexOf("JAVA"));
System.out.println(strCom.lastIndexOf("JAVA", 10));
//返回字符串長度
System.out.println(strCom.length());
//matchs(String regex)匹配正則表達式
try {
String regex = "1234";
System.out.println(regex.matches("http://d{4}"));
System.out.println(regex.replaceAll("http://d{4}", "chen"));
System.out.println(regex.replaceFirst("http://d{4}", "chen"));
} catch (PatternSyntaxException e) {
// TODO: handle exception
e.printStackTrace();
}
// offsetByCodePoints(int index,int codePointOffset)
//返回從給定的index處偏移codePointOffset個代碼點的索引
System.out.println(strCom.offsetByCodePoints(7, 4));
//測試兩個字符串區域是否相等,第一個參數為true時表示忽略大小寫
System.out.println(strCom.regionMatches(true, 0, "I lIke", 0, 3));
System.out.println(strCom.regionMatches(0, "I like", 0, 3));
System.out.println(strCom.replace('A', 'a'));
System.out.println(strCom.replace("JAVA", "PHP"));
//String[] split(String regex,int limit)
//按指定的分隔符會字符串內容分割并存放到字符串數組中,limit為控制模式應用次數
String[] info = strCom.split(" ,");
for (int i = 0; i < info.length;i++)
System.out.println(info[i]);
info = strCom.split(" ", 2);
for (int i = 0; i < info.length;i++)
System.out.println(info[i]);
//startsWith(String prefix,int toffset)//判斷是否以指定前綴開始
//toffset為負或大于字符串長度結果為false
System.out.println(strCom.startsWith("I"));
System.out.println(strCom.startsWith("I",-1));
//CharSequeuece subSequeuece(int beginIndex,int endIndex)
//返回一個新的字符序列
System.out.println(strCom.subSequence(2, 6));
//String substring(int beginindex,int endIndex)
//返回子字符串
System.out.println(strCom.substring(2));
System.out.println(strCom.substring(2, 6));
//toCharArray() 字符串變字符數組
char[] str1 = strCom.toCharArray();
for (int i = 0; i < str1.length;i++)
System.out.print(str1[i]+" ");
System.out.println();
//toLowerCase(Locale locale) 將字符串中的所有字符變成大/小寫返回新的字符串
System.out.println(strCom.toLowerCase());
System.out.println(strCom.toUpperCase());
System.out.println(strCom.toUpperCase(form));
System.out.println(strCom.toLowerCase(form));
//trim()方法取出字符串的前后空白
System.out.println((" "+strCom).trim());
//valueOf() 靜態方法 實現基本數據類型轉成字符串
System.out.println(String.valueOf(true));
System.out.println(String.valueOf('A'));
System.out.println(String.valueOf(12.0));
}
}
新聞熱點
疑難解答