日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

JAVA String類經(jīng)常用方法

 techres 2011-03-10
JAVA String類經(jīng)常用方法 作者:翼風(fēng)Ephonre tags:Java String str java String ing 1.charAt 方法,返回指定index的字符。
String string ="123456789";
char a =string.charAt(2);
System.out.print(a);
   a=3
2.codePointAt 方法,返回指定索引處的字符(Unicode 代碼點(diǎn))。
string ="abc";
int codePoint=string.codePointAt(2);
System.out.print(codePoint);
 結(jié)果:99
3.codePointCount 字符的長(zhǎng)度
引用:
在JDK5.0中,Java引入了對(duì)Unicode4.0中所謂"增補(bǔ)碼"的支持,這種字符的長(zhǎng)度大于2B,因此用char類型無(wú)法表示(Java中char類型占2B).
Java的設(shè)計(jì)者于是利用兩個(gè)char變量空間來(lái)表示這樣的一個(gè)字符
于是,假如字符串中有這樣的字符,則length方法無(wú)法精確的表示字符長(zhǎng)度 (length方法返回的是String中char的個(gè)數(shù)) ,于是有了codePointCount方法,利用這個(gè)方法可以精確統(tǒng)計(jì)出String中字符的數(shù)量,考慮到了String中可能含有的"增補(bǔ)碼"

4.compareTo 字符串的比較
假如2個(gè)字符串相同,返回值為0;
string ="abcdefg";
int codePoint=string.compareTo("ea");
將返回e在ea中的索引0減去e在“abcdefg”中的索引4,所以codePoint=-4
引用:
按字典順序比較兩個(gè)字符串。該比較基于字符串中各個(gè)字符的 Unicode 值。按字典順序?qū)⒋? String 對(duì)象表示的字符序列與參數(shù)字符串所表示的字符序列進(jìn)行比較。假如按字典順序此 String 對(duì)象位于參數(shù)字符串之前,則比較結(jié)果為一個(gè)負(fù)整數(shù)。假如按字典順序此 String 對(duì)象位于參數(shù)字符串之后,則比較結(jié)果為一個(gè)正整數(shù)。假如這兩個(gè)字符串相等,則結(jié)果為 0;compareTo 只在方法 equals(Object) 返回 true 時(shí)才返回 0。
這是字典排序的定義。假如這兩個(gè)字符串不同,那么它們要么在某個(gè)索引處的字符不同(該索引對(duì)二者均為有效索引),要么長(zhǎng)度不同,或者同時(shí)具備這兩種情況。假如它們?cè)谝粋€(gè)或多個(gè)索引位置上的字符不同,假設(shè) k 是這類索引的最小值;則在位置 k 上具有較小值的那個(gè)字符串(使用 < 運(yùn)算符確定),其字典順序在其他字符串之前。在這種情況下,compareTo 返回這兩個(gè)字符串在位置 k 處兩個(gè)char 值的差

5.compareToIgnoreCase 忽略大小寫(xiě)
6.indexOf(int ch)
ch:unicode code point,假如字符串中沒(méi)有ch,則返回-1
String ss = "abcde";
System.out.println(ss.indexOf(2));
System.out.println(ss.indexOf(98));
結(jié)果:-1 1
因?yàn)?對(duì)應(yīng)的unicode在字符串ss中不存在,所以返回值-1,98對(duì)應(yīng)的unicode 是b,所以返回值是index=1
7.indexOf(String str) 返回參數(shù)在字符串中第一次出現(xiàn)的位置索引
string ="abcaabc";
int codePoint=string.indexOf("bc");
結(jié)果: index =1
8.indexOf(int ch, int fromIndex)
ch (Unicode)
String string ="abcaabc";
int codePoint=string.indexOf(99, 5);
System.out.println(codePoint);
System.out.println(string.indexOf(99, 5));
結(jié)果:
6
2

因?yàn)閏對(duì)應(yīng)的Unicode為99
indexOf(String str,int fromIndex) 返回從索引fromIndex開(kāi)始,str第一次出現(xiàn)的位置
String string ="abcaabc";
System.out.println(string.indexOf("bc", 0));
System.out.println(string.indexOf("bc", 5));
結(jié)果:
1
5

lastIndexOf(int ch)返回 unicode 在字符串中最后出現(xiàn)時(shí)的位置索引
String string ="abcaabc";
System.out.println( 
   string.lastIndexOf(99));//99 is unicode of c
結(jié)果:
6
lastIndexOf(String str)返回 str 在字符串中最后出現(xiàn)時(shí)的位置索引
String string ="abcaabc";
System.out.println(string.lastIndexOf("bc"));

結(jié)果:5
lastIndexOf(int ch, int fromIndex)返回從0到fromIndex,ch最后出現(xiàn)的位置索引
String string ="abcdafaa";
System.out.println(string.lastIndexOf(97, 4));
返回值<=fromIndex
結(jié)果:4
lastIndexOf(int ch, int fromIndex)返回從0到fromIndex,str最后出現(xiàn)的位置索引
String string ="abcbcbcbcbc";
System.out.println(    string.lastIndexOf("bc", 6));
返回值
k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
結(jié)果:4       
9.offsetByCodePoints(int index,int codePointOffset)
codePointOffset 偏移量
string ="abcaabcdef漢子";
int codePoint=string.offsetByCodePoints(8, 2);
結(jié)果:
codePoint=10
引用解釋:
一個(gè)完整的Unicode字符叫代碼點(diǎn)/CodePoint,而一個(gè)Java char 叫代碼單元code unit;
string對(duì)象以UTF-16保存Unicode字符,需要用2個(gè)字符表示一個(gè)超大字符集漢字,這種表示方式為
Sruuogate,第一個(gè)字符叫Surrogate High,第二個(gè)就是Surrogate Low
判定一個(gè)char是否是Surrogate區(qū)的字符,用Character的isHighSurrogate()/isLowSurrogate()方法。
從兩個(gè)Surrogate High/Low字符,返回一個(gè)完整的Unicode CodePoint用Character.toCodePoint()/codePointAt()
一個(gè)Code Point,可能需要一個(gè)也可能需要兩個(gè)char表示,因此不能直接使用CharSequence.length()
方法返回一個(gè)字符串到底有多少個(gè)漢字,而需要用String.codePointCount()/Character.codePointCount()
要定位字符串中的第N個(gè)字符,不能直接將n作為偏移量,而需要從字符串頭部依次遍歷得到,需要
String.offsetByCodePoints()
從字符串的當(dāng)前字符,找到上一個(gè)字符,不能直接用offset實(shí)現(xiàn),而需要
String.codePointBefore(),或String.offsetByCodePoints()
從當(dāng)前字符,找下一個(gè)字符,需要判定當(dāng)前CodePoint的長(zhǎng)度,再計(jì)算得到
String.offsetByCodePoints()
。
個(gè)人測(cè)試:假如沒(méi)有拋出異常,返回值總是index + codePointOffset
10.concat(String str)將參數(shù)連接到字符串的末尾

concatenate 如鎖鏈般連續(xù),使連鎖,連結(jié)

string ="abc";
System.out.print(string.concat("123"));
結(jié)果:abc123

假如str的length是0,那么這個(gè)String就會(huì)被返回。
11.intern 字符串扣留 返回字符串對(duì)象的規(guī)范化表示形式。

java API解釋:
public String intern()
一個(gè)初始時(shí)為空的字符串池,它由類 String 私有地維護(hù)。
當(dāng)調(diào)用 intern 方法時(shí),假如池已經(jīng)包含一個(gè)等于此 String 對(duì)象的字符串(該對(duì)象由 equals(Object) 方法確定),則返回池中的字符串。否則,將此 String 對(duì)象添加到池中,并且返回此 String 對(duì)象的引用。
它遵循對(duì)于任何兩個(gè)字符串 st,當(dāng)且僅當(dāng) s.equals(t)true 時(shí),s.intern() == t.intern() 才為true。

String string1 = "Too many";
String string2 = " cooks";
String string3="Too many cooks";
String string4 =string1 +string2;
System.out.println(string3==string4);
string4=string4.intern();
System.out.println(string3==string4);
結(jié)果:
false
true

12 hashCode 返回字符串的hashCode值
String string0 ="abc";
String string1 ="abc";
System.out.println(string1.hashCode());
System.out.println(string1.hashCode());
結(jié)果:
96354
96354

13 contains(CharSequence s)是否包含參數(shù)
String string0 ="abcdef";
System.out.println( string0.contains("de"));
結(jié)果:true
14 contentEquals(CharSequence cs)
將此字符串與指定的 CharSequence 比較。當(dāng)且僅當(dāng)此 String 與指定序列表示相同的 char 值序列時(shí),結(jié)果才為 true。
參數(shù)
cs - 要與此 String 比較的序列
返回:
假如此 String 與指定序列表示相同的 char 值序列,則返回 true;否則返回 false。
String string0 ="abcdef";
System.out.println( string0.contentEquals("abcdef"));
結(jié)果:true
15 contentEquals(StringBuffer sb)
將此字符串與指定的 StringBuffer 比。相同,true,否則:false
String string0 ="abcdef";
System.out.println( string0.contentEquals(new StringBuffer("abcdef")));
結(jié)果:true
16 getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將當(dāng)前字符串的部分字符復(fù)制到目標(biāo)自負(fù)數(shù)組dst中,從srcBegin(包含在內(nèi))到srcEnd(不包含在內(nèi))之間的字符復(fù)制到目標(biāo)字符數(shù)組中的字符從dstBegin位置開(kāi)始存放.
String ss="hello,word";
char dst[]={'a','b','c','d','e','f'};
ss.getChars(4,6,dst,3);
System.out.println(dst);
結(jié)果 abco,f

17.getBytes(String charsetName);
String ss = "abcde";
byte dst[] = new byte[5];
try {
dst = ss.getBytes("GB2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
for (int i = 0; i < dst.length; i++) {

System.out.print(i==4?dst[i]:dst[i]+",");
}
}
結(jié)果:97,98,99,100,101
18 getBytes
String ss = "abcde";
byte dst[] = new byte[5];
dst = ss.getBytes();
for (int i = 0; i < dst.length; i++) {
System.out.print(i==4?dst[i]:dst[i]+",");
}
結(jié)果:97,98,99,100,101
startsWith(String perfix) 是否以perfix開(kāi)頭,yes 返回true ,no返回false
String string ="abcbd";
System.out.println(    string.startsWith("abc"));
System.out.println(    string.startsWith("Ab"));
結(jié)果:true
false

startsWith(String prefix, int toffset)偏移toffset,是否以perfix開(kāi)頭,yes 返回true ,no返回false
String string ="abcde";
System.out.println(    string.startsWith("cd", 2));
包含toffset 對(duì)應(yīng)的字符串
結(jié)果:true

endsWith(String suffix)是否以suffix結(jié)尾,yes 返回true ,no返回false
String string ="abcde";
System.out.println(    string.endsWith("e"));
結(jié)果:true
trim()去掉字符串的前后空格
String string =" abc ";
System.out.println(string.length()+","+    string.trim().length());
結(jié)果:5,3

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多