Tải bản đầy đủ (.pdf) (6 trang)

Lập trình hướng đối tượng trong java (Phần 5) docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (272.58 KB, 6 trang )










1
LẬP TRINH HƯỚ NG ĐÔ
́
I TƯỢNG TRONG JAVA
phần 5

LỚ P STRING VÀ NUMBER

I. 

 
 




. 






 







 (Java


  



 ),  




 , 










 .  

 








  

?




, 

 , 







 






 



,
 .lang. 

  










.

II.   STRINGBUFFER
Trong g.








 







 : 


StringBuffer, 

, 
 , 




. StringBuffer 

 



.
V d : 



















.
public class ReverseString {
public static String reverse(String source) {
int i, len = source.length();










2
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i )
dest.append(source.charAt(i));
return dest.toString();
}
}

1. Lơ
́
p String

To mt đi tưng
 



  . 

  


ba

 



 , 





. 






, 

String s = new String();
String s = new String("Gobbledygook.");




:
String s = "Hola Mundo";
- :
String()
String(byte bytes[])
String(byte bytes[],int startIndex, int numChars)
String(char chars[])
String(char chars[], int startIndex, int numChars)
String(String s)
String(StringBuffer s)
V d :

byte b[] = {65, 66, 67, 68, 69, 70};
String s1 = new String(b); // K



DEF
String s2 = new String(b,2,3); // K














3
String s3 = 

(c); // Kh
String s4 = new String(c,2,3); // 









String s5 = new String(s2); // T3 








s2


- int length() : cho chi
int len = source.length();
int len = "Goodbye Cruel World".length();
- char charAt(int index) : t
char c = source.charAt(1);
 //G
- boolean equals(String object) : ki, 








() v= = . 










  .  = = so s2  




  



.
String s1 
String s2 = new String(s1); // t 2  1, 

 

//  


 




:
s1 equals s2 : true
s1 = = s2 : false
- int compareTo(String str) : so s2 , :
 < 0 : 


 > 0 : 


 = 0 :  
- int indexOf(int character) :  (c 

 ) 
character
int lastIndexOf(int character)









4
- int indexOf(int character, int from) :  (c 


) 
character, 



 

from   (  )
int lastIndexOf(int character, int from)
- int indexOf(String string) : (c 

) string
int lastIndexOf(String string)
- int indexOf(String string, int from) :  (c 

) 
string, 



 

from   (  )
int lastIndexOf(String string, int from)
- String subString(int startIndex, int endIndex) :  
 

 


  

 -1,  



  

String org 

result = org.subString(8);
- String replace(char orginal, char replacement) :  





 // Cho s  
- String trim() :  



 




- String toLowerCase() : 







- String toUpperCase() : 





String upper = s.toUpperCase();
- Ton tư
̉
+ :  ,  



 
,
String s1 = "two";
System.out.println("one" + s1 + "three");
System.out.println("Word v. " + 9+7);
V 





 






 


,  , 





 +, 


Java 






s
, 






 ,  




sang StringBuffer
public class ReverseString {









5
public static String reverse(String source) {
int i, len = source.length();

for (i = (len - 1); i >= 0; i )
dest = dest + source.charAt(i);
return dest;
}
}
- static String valueOf(object/var x) :
, tr

V d : 


 





, :
System.out.println(String.valueOf(Math.PI));

2. Lp StringBuffer

To mt đi tưng StringBuffer










:
StringBuffer() : 16 
StringBuffer(int length) : 
V d : StringBuffer dest = new StringBuffer(25);

Cc phương thc thưng dng ca lp StringBuffer
- int length() : cho chi
- char charAt(int index) : tex

- void setCharAt(int index, char ch) : 



, 
- StringBuffer append(object/var x) : 

  

 




 . 







 




int a = 20;
StringBuffer sb = new StringBuffer(40);



();
- StringBuffer insert(int index, object/var x) : ch
x
StringBuffer sb = new StringBuffer("I Java!");









6
sb.insert(3, "like ");
System.out.println(sb); // 
- StringBuffer reverse() : 








StringBuffer sb = new StringBuffer("I Java!");
sb.reverse();
- StringBuffer delete(int startIndex, int endIndex) : x  
endIndex-1

- StringBuffer deleteCharAt(int index) : 
- StringBuffer subString(int startIndex, int endIndex) : 
 -1,  



 

- StringBuffer replace(int startIndex, int endIndex, String str) :  


dex-1 

 .lang.String and java .lang.



 


×