Title: ????? ?????? String ?????? ??????
1 ??????? ???? ????? ????? ?????21/03/07
????? ?????? String?????? ??????
2?????? ????? ?-JAVA ??????? ???? ??????? ???????
??????? java.lang.String
3?????? 2 ?????? ???????
- ??? 3 ????? ???????? 6) ???? (
- ????? ?-C ??????
- ???? ????
- ?????? ???? ?????? ????? ?????? ????.
- ????? ??????
- ??????? ??? ??? ?????? ????? ??????? ??????
????? ????? ?????????? ???? ???????. - ????? ??????? ?????? ?? ?????? String
- ???? ??????, ?????? ???????, ????? ???????,
????? ???????, ????? ???????, ?????
??-??????.
4?? ??? ?? !!
- ?????? String ????? ???? ?????? ??? ???.
- ??? ?????? String ???? ???? ?? ?????!! (????
????? ?? ???? ????.) - ??? ?????? String ??? ???? ???-???? ????? ?? ???
????? ?????? ??????!! - ????? ??????? ????? ?"? ????? ???-??????? ??????
??????. - ???? ????? StringBuffer ????? ???????? ???????
?-Pascal ?-C. ?? ???? ?????? ??.
5??????? (1)
- ?????? String ??? ????? ???? ????.
?????? ?????? ?-final ??? ????? ???-???? ?????
????? ?"?????" ?-String
6????? ??????
- ????? ?? ????? ?????? String
- String s
- ?????
- sHello
- ????? ??????
- String sHello
7????? ????? ????? ??? ?????
95
40
int mark markIO.readInt()
if(markgt90)IO.writeln("good")
if(marklt55)IO.writeln("bad")
good
bad
int mark String s markIO.readInt()
if(markgt90)s"good" if(marklt55)s"bad"
IO.writeln(s)
Error variable s might not have been initialized
8??????? ?????? ???????
- ???
- IO.writeln(s)
- System.out.println(s)
- ????
- String s1aa
- String s2s1
- ?????
- String s3s1s2
- s1s2
?? ??? ?-JAVA ??? ????? ??? ?? ?????? ???!
String s1"abc" String s2s1 String
s3"ABC" s1s3 System.out.println(s1)
System.out.println(s2) System.out.println(s3)
abcABC abc ABC
9?????
- ????? ?????? ?????? ?? ??? ???????? .
- ????? ????? ??? ???
- s s t \n
- ?? ??? ???? ???? ???????? ??????? ???? ?????
- int count
- System.out.println(Count is count)
four22
String sfour22
10????? ????/??? ???????
??? ?????? ?????? ????? ????
??? int iNuminput.nextInt()
???? double dNuminput.nextDouble()
??????? boolean binput.nextBoolean()
?????? String sinput.next() ??? ? ???? ?? ?????? ?? ???? ??????.
??? ??? ????? ??????!char cinput.next().charAt(0)
11- ????? ?? ????? ??????? String ?? ?????.
- ???? ?? ??????? ?????? ????? ???? ????? ?? ??????
String, ????? ??? ?????? ?????? ???.
12????? ???????? ?????? ??????
- ????? ?????? ????
- String snew String()
- ????? ?????? ??? ??????? ???? s1
- String snew String(s1)
- ????? ?????? ????? ?????
- String snew String(charArr)
- ????? ?????? ???? ?? ???? ?????
- String snew String(charArr, 3, 5)
13????? ??????? ???? ?????? ???????
String s0 s0"aaa" String s1"aaa"
if(s1s0)System.out.println("yes")
else System.out.println("no") String
s2"aaa" if(s1s2)System.out.println("yes")
else System.out.println("no")
String s3new String(s1) if(s1s3)System.out
.println("yes") else
System.out.println("no") String
s4IO.readString() if(s1s4)System.out.prin
tln("yes") else
System.out.println("no")
yes
yes
no
aaa
no
14????? ??????? ???? ?????? ???????- ????
String s0 s0"aaa" String s1"aaa"
if(s1s0)System.out.println("yes")
else System.out.println("no") String
s2"aaa" if(s1s2)System.out.println("yes")
else System.out.println("no")
String s3new String(s1) if(s1s3)System.out
.println("yes") else
System.out.println("no") String
s4IO.readString() if(s1s4)System.out.prin
tln("yes") else
System.out.println("no")
s0
yes
aaa
yes
no
aaa
aaa
no
15???????
- ?? ????? ?? ?????? String ???? ?????? ??????
???????? ??????. - ???? indexOf, substring, length, replace
16??????? ??? ?? ???????
- ???? ???????? ?? ?????, ???? ??????? ?????
?????? ???????? ?????? String, ??????? ??????
????? ????? ?? ???. - ????? ?? ???? ... ???? ?? ?????? ...
????? ???????
)
(
?? ????
.
?? ??????
String s1 abcde int p s1.indexOf(d)
17?????? int length()
- ??? ?????? ????? ????? length(), ??????? ?? ?????
?? ???????.
String sABCD System.out.println(s.length())
4
String s System.out.println(s.length())
0
String s System.out.println(s.length())
Error variable s might not have been initialized
String snull System.out.println(s.length())
NullPointerException
18?????? ????? ???? (length)
- ??? ??!!
- ?????? ??? ??? ????? ?????? ????? ??? ???????
length(). - if (s.length() gt 0) .
- ??? ???? ????? ????? length.
- int a new int10
- for (int i 0 i lt a.length i)
19???? ?????? ??????char charAt(int index)
- ???? ????? ????? ?????? ??? ??????
for (int i 0 i lt s.length() i) if
(s.charAt(i) ) count
????? ????? ?-0!
String saaa System.out.println(s.charAt(s.leng
th()))
StringIndexOutOfBoundsException String index
out of range 3
String s"aaa" s.charAt(1)'b'
Error unexpected type required variable found
value
20????? ????? ?????
- void getChars(int srcBegin, int srcEnd,
char dst, int dstBegin)
String s"We love Java" int start3,
end7 char dstnew charend-start
s.getChars(start,end,dst,0) for(int
i0iltdst.lengthi) System.out.println(dst
i)
l o v e
String s"We love Java" char
chs.toCharArray()
s.length() ch.length
21????? ????? ???????
- public static String valueOf(char data,
int
start,
int count)
- public static String valueOf(char data)
char a'W','e',' ','l','o','v','e','
','J','a','v','a' String
sdString.valueOf(a) System.out.println(sd
)
We love Java
????? ?????
22?????? --gt ???? --gt ??????
???? ?????? ??? ????? ?????? ??????? ?? ??????
??? ??? ??? ???????
We love Java
String sIO.readString() System.out.println(s)
char temps.toCharArray()
Arrays.sort(temp) sString.valueOf(temp)
System.out.println(s)
We love Java
JWaaeelovv
23??????? ??? ???????
?????? String ????? ???? ?????? ?????? ?????? ???
??????? ?????? String ????? ???????? ??? ??????
String.
public boolean equals(String otherString) ??????
true ?? ??????? ??????? ?? ??? otherString ???
??????? ??????? ?? ??? ???????? ????? ????? ??
??????.
public boolean equalsIgnoreCase(String
otherString) ?????? true ?? ??????? ??????? ??
??? otherString ??? ??????? ??????? ?? ???
???????? ????? ????? ?? ??????. ?????? ???????
??? ??????? ??????? ???? ?????? ????? ???????.
public int compareTo(String otherString) ?????? 0
?? ??? ???????? ????, ???? ???? ???.(???? ?????
?? ?? ??? ?????? ?????? ??"? ????
?-Unicode). ?????? ??? ????? ?? ??????? ??????
???? ??? ???? ??????. ?????? ??? ????? ?? ???????
?????? ???? ??? ???? ??????
24???? ??? ?-equal
- ???? ???? ?? ????? ?????? String ??? ???, ???
????? ???? ????? ???? ??? ??? ?????? ?? ????.
????? ??? ???? ?? ??? ?????? ??? ???? No
String s1 abcde String s2 abc s2 s2
de if (s1 s2) IO.writeln(Yes) else I
O.writeln(No)
abcde
de
s2
abc
abcde
No
25???? ??? ?-equal
- ?? ????? ??????? ?? ?????? ???????????? ??? ????
??? Yes - ?? ?????? ?????? equals ??????? ??????
String.
String s1 abcde String s2 abc de if
(s1.equals(s2)) IO.writeln(Yes) else IO.wr
iteln(No)
26??????? ??? ???????
- equals ????? boolean (true/false - ????/?? ????)
- compareTo ????? ??? ??????? ??? ??? ??????
lexicographic. ???????? ?????? int
s1.compareTo(s2)gt0 s1 ???? s2
s1.compareTo(s2)lt0 s1 ???? s2
s1.compareTo(s2)0 s1 ??? ?-s2
27??? ????. ??? ?????
import java.util. public class test42 static
Scanner readernew Scanner(System.in) public
static void main(String args) String
s1"abc" String s2reader.next()
System.out.println(s1.compareTo(s2))
?
-3
d
ae
abf
abcabc
abc123
?
?
?
abc
28?????? ??????? - ?????? ??????
- public boolean startsWith(String prefix)
- ?????? true ?? ??????? ?????? ?????? ???????
????? (prefix) -
- public boolean endsWith(String suffix)
- ?????? true ?? ??????? ??????? ?? ??? ????????
??????? ??????? ????? (suffix). -
- public boolean regionMatches(int thisBgn,
- String otherStr, int
otherStr, int length) - ?????? true ?? ??? ???????? ???? ????? ??????.
??????? ????? ??? ????? ??????? thisBgn ???????
?????? ???????? ????? length ?????. ??????? ?????
??????? ????? ??? ???? ????? ??????? ??? ???
otherStr. ????? ?? ?????? ??????? ?? ??????
?????/??????.
29 String sIO.readString(Enter Phone Number)
if(s.startsWith("02"))IO.writeln("Jerusalem")
if(s.startsWith("03"))IO.writeln("Gush Dan")
if(s.startsWith("04"))IO.writeln("North")
if(s.startsWith("08"))IO.writeln("S
outh")
String s1IO.readString("Your Web-site")
if(s1.endsWith("il"))IO.writeln("????")
else IO.writeln("Hello")
30????? ????? ???-???????
- ?? ??? ???????? ?????? ?? ????? ??? ?? ?? ???????
???????? ??????? ??"? ??????? ?????? ?-0 ?? .
length-1 ?? ??? ?? ?? ??????? ??????? ?? ??????
??????? ?????? ???????? ?? ????? ???? .1
- public int indexOf(char ch)
- ????? ??????? ?? ???? ??????? ??? ?? ???? ??? ch
??????? ?????? ????????. ?????? ???? ??? ?????
??????? ?? ???????, ????? ??????? ?? ??? ??????,
????? ??? ??? ch, ?????. - public int lastIndexOf(char ch)
- ????? ??????? ?? ???? ??????? ??? ?? ???? ??? ch
??????? ?????? ????????. ?????? ???? ??? ?????
????? ?? ???????, ??????? ?? ??? ?????? ????? ???
??? ch ?????.
31????? ????? ?? ?? ?????
String sIO.readString() for(char ch'a'
chlt'z'ch) if(s.indexOf(ch)-1)
IO.write(ch)
?????? ?????? ?????? ???????
boolean btrue String sIO.readString()
for(int i0 ilts.length()i) char
lets.charAt(i) if(s.indexOf(let)!s.lastIn
dexOf(let))bfalse if
(b)IO.writeln("Yes")else IO.writeln("No")
????? ??? ?? ????? ?? ??? ???
32????? ????? ???-???????
public int indexOf(char ch, int begin) ?????
??????? ?? ???? ??????? ??? ?? ???? ??? ch
??????? ?????? ????????. ?????? ????? ????? ?????
??????? ????? ?? ??????, ?- begin, ?????
??????? ?? ??? ?????? ????? ??? ??? ch
?????. public int lastIndexOf(char ch, int
fromIndex) ????? ?? ????? ?? ??? ch ??????? ?????
??? ?????? ??? ??????? fromIndex ?????? ??????.
??? ?????? ??????? ????? ???? ??? ch ????? ?? ???.
33????? ????? ???-???????
- public int indexOf(String str)
- ????? ?? ????? ??????? ?? ??????? ????????? ?????
?? ??????? str. ???? ??????? ??? ??? ????? ?????
?? ??? ??????. - public int indexOf(String str, int begin)
- ????? ?? ????? ??????? ????????? ????? ?? ???????
str ??? ?????? ????? ??????? ??? ??? begin
?????? ????. - public int lastIndexOf(String str)
- ????? ?? ????? ??????? ?????? ???????? ?? ???????
??????? ?? ??? str. ?????? ????? ????? ?? ???????
?????? ??????. ???? ??????? ???? ???? str ?????
??????? ????? ?? ??? ??????. -
- public int lastIndexOf(String str, int
fromIndex) - ????? ?? ????? ??????? ?????? ???????? ?? ???????
??????? ?? ??? str. ?????? ????? ?????? fromIndex
?????? ?????? ?? ???????. ???? ??????? ???? ????
str ????? ??????? ????? ?? ??? ??????.
34"????? ???????"
???? ?????, ?? ?? ??? ???????? ??????? ??????
String, ?? ???? ?? ??????? ??????? ?? ???
????????.
?? ??? ???????? ??????? ???? ?? ?????? ?????
???????? ??? ?????? String ??? ????? ????? ??
??????? ??????? ?????? ???????? ????? ??????
??????.
35"????? ???????"
- public String replace (char oldChar, char
newChar) - ????? ?? ?????? ?????? ???????? ??? ?????? String
??? ????? ?????? ????? ??????? ????????, ??????
???????? ????? ?????? ??????, ???? ??? ?? ????
??? oldChar ????? ??? newChar. - public String toLowerCase()
- ????? ?? ?????? ?????? ???????? ??? ?????? String
??? ???? ?? ?????? ????? ??????? ???????? ??????
???????? ???? ??????? ?? ?? ??? ???????? ???????
???? ????. - public String toUpperCase()
- ????? ?? ?????? ?????? ???????? ??? ?????? String
??? ???? ?? ?????? ?????? ??????? ???????? ??????
???????? ???? ??????? ?? ?? ??????? ??????
??????? ??????. - public String trim()
- ????? ?? ?????? ?????? ???????? ??? ?????? String
????? ?????? ????? ??????? ???????? ??????
???????? ???? ??? ???? ?????? ???????? ??????
??????? ?????? ??????.
36"????? ???????"
String s new String("We love Java")
System.out.println(s) s.toLowerCase()
System.out.println(s) s.toUpperCase()
System.out.println(s) s.replace(' ','')
System.out.println(s)
We love Java We love Java We love Java We love
Java
37"????? ???????"
String s"We love Java" System.out.println(s)
String s1s.toLowerCase()
System.out.println(s1) ss.toUpperCase()
System.out.println(s) ss.replace(' ','')
System.out.println(s)
We love Java we love java WE LOVE
JAVA WELOVEJAVA
38????? ??? ????? ???? ??????
String s"We love Java" System.out.println(s
)
We love Java
ss.replace("Java",IO.readString())
System.out.println(s)
We love C
C
ss.replace(" ","") System.out.println(s)
??????
WeloveC
Error cannot find symbol symbol method
replace(char,java.lang.String) location class
java.lang.String
ss.replace('e',"ee") System.out.println(s)
39????? ??-??????
- public String substring(int begin)
- ????? ?? ?????? ?????? ???????? ??? ?????? String
?????? ?? ?????? ??????? ?????? ????????. ??
??????? ??????? ?????? ??? ?????? ???? ???? ?????
??????? ??? begin ??? ????? ?? ??????? ??????
????????. -
- public String substring(int begin, int end)
- ????? ?? ?????? ?????? ???????? ??? ?????? String
?????? ?? ?????? ??????? ?????? ????????. ??
??????? ??????? ?????? ??? ?????? ??? ???? ?????
??????? ??? begin (????) ??? ??? ????? ???????
??? end (?? ????).
40????? StringBuffer
???? ????? ????? ?????? String. ?? ?? ?????? ???
?????? ?????? String, ?????? ????? ???? ????
?????? String ??? ??? ??????? ?????? ???????
?????? StringBuffer ???? ????? (?? ???????
??????? ?? ??? ??????? ?????? String ?? ????
?????). ??????? ?????? StringBuffer ??????
?????? ??????? ?? ??????? ?????. ?????? append
????? ?????? ?????? ????? ??????? ????? ???????
?????? StringBuffer. append ????? ???????? ??
??? ??? ????? ????.
41?? ???? ?????
???? ????????? ???? ????? ?????? String sabc
def ghi ????? ??? ????? ?????? ???
?????? String s(((new StringBuffer(abc)) .
append(def)).
append(ghi)).
toString()
42???? ??? String ?-StringBuffer
String s new String("ssssss") StringBuffer sb
new StringBuffer("bbbbbb") s.concat("-aaa")
sb.append("-aaa") System.out.println(s)
System.out.println(sb)
ssssss bbbbbb-aaa
- ?? ???? ??? ???? ??????? ??? ?????? String ??
???? ????? ?? ??????? ???? ?????
- ?? ??????? ?????? ??????? ?????? StringBuffer ??
???? ?????
43?? ?????? ??? ???
- ??? ?? ???? ???? ???? ?????? ????? ????? ????? ??
???????? - ???? ????? ?????? ?? ?????? ??????? ?? ????
??????? ?? ?? ????? ???? ??????.
???? ??????? String ??? ????? ???? ????, ?????
?????? ?????? ?? ???? ??????? ???. ???????
??? ????? ?????? ?? ?????? ?????? ?????
(static). ?????? ???? ?? ??????? ??????.
44public class ExString public static int
numOfChar(String s, char c)
int counter 0 for (int
i0 i lt s.length() i)
if ( s.charAt(i) c )
counter return
counter public static
void main(String args)
String s1 "abcabcaasa" int m
numOfChar(s1, 'a')
IO.writeln(m)
45????? ????? ??-??????
- static String delete(String s, int from, int to)
- String start s.substring(0, from)
- String end s.substring(to1)
- return start end
-
- public static void main(String args)
- String sI didnt love JAVA
- s delete(s, 2, 8)
- System.out.println(s)
I love JAVA
46????? ????? ?? ?????? ?? ??-??????
static String deleteSubString(String s, String
subS) int lensubS.length() while
(s.indexOf(subS)!-1) ss.substring(0,s.in
dexOf(subS)) s.substring(s.indexOf(sub
S)len) return s
strstr.replace(subStr,"")
String strI didnt didnt didnt love JAVA
String subStr didnt strdeleteSubString(s
tr,subStr) System.out.println(str)
I love JAVA
47????? ????? ??-??????
- static String insert(String s, String sub, int
at) - String start s.substring(0, at)
- String end s.substring(at)
- return start sub end
-
- String s "We love Java!"
- s insert(s, " the", 7)
48- class What
- public static void main(String args)
- String s "the quick brown fox jumped over the
lazy dog" - String t ""
- int space s.lastIndexOf(' ')
- while (space ! -1)
- t t s.substring(space1) " "
- s s.substring(0, space)
- space s.lastIndexOf(' ')
-
- t t s
- System.out.println(t)
-
dog lazy the over jumped fox brown quick the
49??????? - ??? ?? ???????
- ???? ? ?????? ?????? ?????? ????
- ?? ??? ????, ?? ???? ??? ??? ?????? ??????? ????
???? ???????. - ???? ? ?????? ???????
- ?? ????, ?????? ?? ????? ? equals ????? ?
- ???? ? ??? ????? ??????? ?? ?????
- ??? ?????, ?? ????? ?????? ?????? ?????? "????"
?????? ?????? ?????? ?????? ?????? "?? ????..."
50??????? ??????
?????? ???? ?????? ????? ??? ?? ????? ?????
?????? (????? ???? ???? ???...)
?????? ???? ?????? ??? ????? ????? ?? ???? ??
???? ?? ????? ????? ?? ?????. ?????? - ??????
???? ????? ???? "?? ????". ?? ??? ???? ?? ?????
?????? ?????? ?????? ????? ???? ??? ?????? ???
???? ????? ???? ???? ????? ?? ???? ?? ??????
???? ????.
- ???? ?????? ??? ???? ?? ?????? ?????? ??????
????? - ???? "????? ????????"
- ????? ?????? ??????? ??????? ????? ?? ??????
??????? ?? ?"???? ?????" - ????? "???? ???????" ?? ????? ??????? ????
?"?????? ???????". - ?? ????? ?? ??????? ???????? ??? ??? ???????? ??
"???? ???????" ?????? ??? ?????? ?????.
51???? ? ????? ?????? ?? ?????? "????" ?? ????? ??
???... ????? ?... ???!!!, ?? ???????? ??????
????? ?? ???? ?????? ??????? ??????? ????
??????, ????? ?? ???? ?????? ???? ?????? ??? ????
???? "????".
52??????? .... ????????
53????? ???????
- ?? ????? ???????? ?- java ??? ?? ??
- ????? ?? ???
- ?????
- ????? ?????? ?????? ???? ????????
- ???? ??????? ?? ???????? ???????? (int, float,
double, char, boolean) - ??????? ?????? ?? ????????, ??? ????? ????? ?????
?? ????? ?? ??????? "??????". - ?? ???? ???????? ?? ???? ???? ??? ??? ????
- ????? ?????? ?? ????? ???? - ?? ????? ??? ?? ???
?????, ?? ?? ???? ???? ???????!!!
54????? ??????? ?????? (????? 1)
public static void zeroAnySecondCell1(int a)
for (int i1 i lt a.length i2)
ai 0 IO.write("in method
") for (int
i0 ilta.length i)
IO.write(ai " ") IO.writeln()
55????? ??????? ?????? (????? 1 - ????)
int a1 new int10 for (int i0 ilt10
i) a1i i zeroAnySecondCell1(a1) for
(int i0 ilta1.length i)
IO.write(a1i " ") IO.writeln()
void zeroAnySecondCell1(int a) for
(int i1 i lt a.length i2)
ai 0 IO.write("in method ")
for (int i0
ilta.length i) IO.write(ai
" ") IO.writeln()
in method 0 0 2 0 4 0 6 0
8 0 0 0 2 0 4 0 6 0
8 0
56????? ??????? ?????? (????? 2)
public static int zeroAnySecondCell2(int a)
for (int i1 i lt a.length i2)
ai 0 return a
57????? ??????? ?????? (????? 2 - ????)
int a2 new int10 for (int i0 ilt10 i)
a2i i a2 zeroAnySecondCell2(a2)
for (int i0 ilta2.length i) IO.write(a2i
" ") IO.writeln()
int zeroAnySecondCell2(int a) for (int
i1 i lt a.length i2) ai 0
return a
0 0 2 0 4 0 6 0 8 0
58????? ??????? ?????? (????? 3)
int a1 new int10 for (int i0 ilt10 i)
a1i i int a2 a2 zeroAnySecondCell2(a1)
for (int i0 ilta1.length i)
IO.write(a1i " ") for (int i0
ilta2.length i) IO.write(a2i "
") IO.writeln()
int zeroAnySecondCell2(int a) for (int
i1 i lt a.length i2) ai 0
return a
59????? ??????? ?????? (????? 4)
public static int zeroAnySecondCell3(int a)
int tempnew inta.length for
(int i0ilta.lengthi)tempiai for (int
i1 i lt a.length i2)
tempi 0 return temp
60????? ??????? ?????? (????? 4 - ????)
int a1 new int10 for (int i0 ilt10 i)
a1i i int a2 a2 zeroAnySecondCell3(a1)
for (int i0 ilta1.length i)
IO.write(a1i " ") for (int i0
ilta2.length i) IO.write(a2i "
") IO.writeln()
int zeroAnySecondCell3(int a) int
tempnew inta.length for (int
i0ilta.lengthi)tempiai for (int i1 i
lt a.length i2) tempi 0
return temp
61??????? - ??? ?? ???????
???? ??????? ???? ??, ?? ????? ??? ?????? ?? ??
????? ????.
- ????? ?????? 1 ?? ??????? ????? ? "????? ?????
???? ?????" (????), ???? ?????? ????? ??????? ??
????? ???????, ??????? ?????? ????? ?? ????
?????? ??? ????? ????? ????? ???? ???? ???
?????? - ?????? a2 zeroAnySecondCell2(a)
62??????? - ??? ?? ???????
- ????? ?????? 2 ?? ??????? ????? ??????? ???
???? ????? ?? ?????, ?? ?? ????? ? "???? ?????"
?????, ??? ????? ?????? ???? ???? ????? ??????,
?????? ???? ?? ?????? ?? ????? ?????? ??????,
?????? ?? ????? ?? ?? ?? ??????.