— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet423/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   419   420   421   422   423   424   425   426   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

458
 
chapter 14
parsing Strings with
 split()
Parsing with String split()
Imagine you have a fl ashcard like this:
What is blue + yellow?
green
question
answer
What is blue + yellow?/green
What is red + blue?/purple
Saved in a question fi le like this:
How do you separate the question and answer?
When you read the fi le, the question and answer are smooshed 
together in one line, separated by a forward slash “/” (because 
that’s how we wrote the fi le in the QuizCardBuilder code).
String split() lets you break a String into pieces.
The split() method says, “give me a separator, and I’ll break out all 
the pieces of this String for you and put them in a String array.”
token 1
token 2
separator
String toTest = “What is blue + yellow?/green”;
String[] result = toTest.split(“/”);
for (String token:result) {
System.out.println(token);
}
In the QuizCardPlayer app, this 
is what a single line looks like 
when it’s read in from the file.
Loop through the array and print each token 
(piece). In this example, there are only two 
tokens: “What is blue + yellow?” and “green”.
The split() method takes the “/” and uses it to 
break apart the String into (in this case) two 
pieces. (Note: split() is FAR more powerful than 
what we’re using it for here. It can do extremely 
complex parsing with filters, wildcards, etc.)


serialization 
and
file I/O
you are here
4
459
there are no
Dumb Questions
Q:
OK, I look in the API and there are about fi ve 
million classes in the java.io package. How the heck do 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   419   420   421   422   423   424   425   426   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish