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
To mt đ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); // T3
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 s2
.
String s1
String s2 = new String(s1); // t 2 1,
//
:
s1 equals s2 : true
s1 = = s2 : false
- int compareTo(String str) : so s2 , :
< 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();
- Ton 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. Lp StringBuffer
To mt đi tưng StringBuffer
:
StringBuffer() : 16
StringBuffer(int length) :
V d : StringBuffer dest = new StringBuffer(25);
Cc phương thc thưng dng ca lp StringBuffer
- int length() : cho chi
- char charAt(int index) : tex
- 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.