Chapter 11: Strings


Section 11.5: Splitting Strings


Download 0.83 Mb.
bet6/12
Sana18.12.2022
Hajmi0.83 Mb.
#1031312
1   2   3   4   5   6   7   8   9   ...   12
Bog'liq
dasturlsh string

Section 11.5: Splitting Strings


String

.

split

You can split a String on a particular delimiting character or a Regular Expression, you can use the () method that has the following signature:

E'tibor bering, chegaralovchi belgi yoki muntazam ifoda hosil bo'lgan string massividan o'chiriladi.
Chegaruvchi belgidan foydalanishga misol:
String lineFromCsvFile = "Mickey;Bolton;12345;121216";
String[] dataCells = lineFromCsvFile.split(";");

// Result is dataCells = { "Mickey", "Bolton", "12345", "121216"}; Example using regular expression:
String lineFromInput = "What do you need from me?";
String[] words = lineFromInput.split("\\s+"); // one or more space chars

// Result is words = {"What", "do", "you", "need", "from", "me?"}; You can even directly split a String literal:

String[] firstNames = "Mickey, Frank, Alicia, Tom".split(", ");
// Result is firstNames = {"Mickey", "Frank", "Alicia", "Tom"};
Warning: Do not forget that the parameter is always treated as a regular expression.

Oldingi misolda. har qanday belgiga mos keladigan muntazam ifoda joker belgisi sifatida ko'rib chiqiladi va har bir belgi chegaralovchi bo'lgani uchun natija bo'sh massiv bo'ladi.

Splitting based on a delimiter which is a regex meta-character


Quyidagi belgilar regexda maxsus (aka meta-belgilar) hisoblanadi
Yuqoridagi chegaralovchilardan biriga asoslanib satrni ajratish uchun ularni \\ orqali olib tashlashingiz yoki foydalanishingiz kerak.


Download 0.83 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   12




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