JAVA基本語(yǔ)法,有好多常見(jiàn)的就不寫(xiě)了,就寫(xiě)上一些特別的:
Java語(yǔ)言中,把字符串作為對象來(lái)處理,類(lèi)String和StringBuffer都可以用來(lái)表示一個(gè)字符串。(類(lèi)名都是大寫(xiě)字母打頭)
String表示字符串常量
用String表示字符串:
String( char chars[ ] );
String( char chars[ ], int startIndex, int numChars );
String( byte ascii[ ], int hiByte );
String( byte ascii[ ], int hiByte, int startIndex, int numChars );
String使用示例:
String s=new String() ; 生成一個(gè)空串
下面用不同方法生成字符串"abc":
char chars1[]={‘a(chǎn)‘,‘b‘,‘c‘};
char chars2[]={‘a(chǎn)‘,‘b‘,‘c‘,‘d‘,‘e‘};
String s1=new String(chars1);
String s2=new String(chars2,0,3);
byte ascii1[]={97,98,99};
byte ascii2[]={97,98,99,100,101};
String s3=new String(ascii1,0);
String s4=new String(ascii2,0,0,3);
3.用StringBuffer表示字符串
StringBuffer( ); /*分配16個(gè)字符的緩沖區*/
StringBuffer( int len ); /*分配len個(gè)字符的緩沖區*/
StringBuffer( String s ); /*除了按照s的大小分配空間外,再分配16個(gè)
字符的緩沖區*/
類(lèi)String中提供了length( )、charAt( )、indexOf( )、lastIndexOf( )、getChars( )、getBytes( )、toCharArray( )等方法。
◇ public int length() 此方法返回字符串的字符個(gè)數
◇ public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范圍是0~length-1
◇ public int indexOf(int ch)
public lastIndexOf(in ch)
返回字符ch在字符串中出現的第一個(gè)和最后一個(gè)的位置
◇ public int indexOf(String str)
public int lastIndexOf(String str)
返回子串str中第一個(gè)字符在字符串中出現的第一個(gè)和最后一個(gè)的位置
◇ public int indexOf(int ch,int fromIndex)
public lastIndexOf(in ch ,int fromIndex)
返回字符ch在字符串中位置fromIndex以后出現的第一個(gè)和最后一個(gè)的位置
◇ public int indexOf(String str,int fromIndex)
public int lastIndexOf(String str,int fromIndex)
返回子串str中的第一個(gè)字符在字符串中位置fromIndex后出現的第一個(gè)和最后一個(gè)的位置。
◇ public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
srcbegin 為要提取的第一個(gè)字符在源串中的位置, end為要提取的最后一個(gè)字符在源串中的位置,字符數組buf[]存放目的字符串, dstbegin 為提取的字符串在目的串中的起始位置。
◇public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
參數及用法同上,只是串中的字符均用8位表示。
2.類(lèi)StringBuffer提供了 length( )、charAt( )、getChars( )、capacity()等方法。
方法capacity()用來(lái)得到字符串緩沖區的容量,它與方法length()所返回的值通常是不同的。
String類(lèi)提供的方法:
concat( )
replace( )
substring( )
toLowerCase( )
toUpperCase( )
◇ public String contat(String str);
用來(lái)將當前字符串對象與給定字符串str連接起來(lái)。
◇ public String replace(char oldChar,char newChar);
用來(lái)把串中出現的所有特定字符替換成指定字符以生成新串。
◇ public String substring(int beginIndex);
public String substring(int beginIndex,int endIndex);
用來(lái)得到字符串中指定范圍內的子串。
◇ public String toLowerCase();
把串中所有的字符變成小寫(xiě)。
◇ public String toUpperCase();
把串中所有的字符變成大寫(xiě)。
2.StringBuffer類(lèi)提供的方法:
append( )
insert( )
setCharAt( )
如果操作后的字符超出已分配的緩沖區,則系統會(huì )自動(dòng)為它分配額外的空間。
◇ public synchronized StringBuffer append(String str);
用來(lái)在已有字符串末尾添加一個(gè)字符串str。
◇ public synchronized StringBuffer insert(int offset, String str);
用來(lái)在字符串的索引offset位置處插入字符串str。
◇ public synchronized void setCharAt(int index,char ch);
用來(lái)設置指定索引index位置的字符值。
注意:String中對字符串的操作不是對源操作串對象本身進(jìn)行的,而是對新生成的一個(gè)源操作串對象的拷貝進(jìn)行的,其操作的結果不影響源串。
相反,StringBuffer中對字符串的連接操作是對源串本身進(jìn)行的,操作之后源串的值發(fā)生了變化,變成連接后的串。
還要理解一個(gè)短路(Short-Circuit)邏輯運算符,呵呵其實(shí)就是&& ,||
JAVA中沒(méi)有指針的概念但有引用的概念:每個(gè)引用占據32位的內存空間,基值指向對象實(shí)際所在的內存中的位置,例如:
Data d=new Date();
通常我們稱(chēng)d是Date型的對象,實(shí)際上d就是引用,它是一個(gè)32位的數據,它的值指向該Date對象實(shí)際所在的內存空間。
聯(lián)系客服