Робота із стрічками в мові Java Стрічка в мові Java – це послідовність символів, яка реалізована у вигляді об’єкта типу String. Оригінальна стрічка є фіксованою і незмінною. Класи для роботи із стрічками (пакет java. lang): String. Buffer String. Builder Інтерфейс Char. Sequence
Конструктори стрічок (1) 1. String() String s = new String(); 2. String(chars[]) chars[] = { ‘a’, ‘b’, ‘c’ }; String s = new String(chars); “abc” 3. String(chars[], int start. Index, int num. Chars) chars[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; String s = new String(chars, 2, 3); “cde” 4. String(String str. Obj) class Make. String { public static void main(String args[]) { char c[] = {'J', 'a', 'v', 'a'}; String s 1 = new String(c); String s 2 = new String(s 1); System. out. println(s 2); } } Java
Конструктори стрічок (2) 5. String(byte ascii. Chars [ ] ) 6. String(byte ascii. Chars [ ], int start. Index, int num. Chars) class Sub. String. Cons { public static void main(String args[]) { byte ascii[] = {65, 66, 67, 68, 69, 70 }; String s 1 = new String(ascii); System. out. println(s 1); String s 2 = new String(ascii, 2, 3); System. out. println(s 2); } } ABCDEF CDE
Конструктори стрічок (3) 7. String(String. Buffer str. Buf. Obj) 8. String(int code. Points[ ], int start. Index, int num. Chars) 9. String(String. Builder str. Build. Obj)
Визначення довжини стрічки int length() chars[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; String s = new String(chars); System. out. println(s. length()); 6
Спеціальні стрічкові операції 1. Автоматичне створення нових екземплярів String із стрічкових літералів. 2. Конкатенація множини об’єктів String за допомогою операції +. 2. Перетворення інших типів даних в стрічкове представлення.
Стрічкові літерали 1. Створення об’єкта за допомогою стрічкового літералу chars[] = { ‘a’, ‘b’, ‘c’ }; String s 1 = new String(chars); String s 2 = “abc”; // використання стрічкового літералу 2. Використання стрічкового літералу як об’єкта System. out. println( “abc”. length() ); 3
Конкатенація стрічок String age = “ 17”; String s = “Jon” + age + “years”; System. out. println(s); Jon 17 years class Con. Cat { public static void main(String args[]) { String long. Str = "This could have been " + "a very long line that would have " + "wrapped around. But string concatenation " + "prevents this. "; System. out. println(long. Str); } } This could have been a very long line that would have wrapped around. But string concatenation prevents this.
Конкатенація стрічок з іншими типами даних int age = 17; String s = “Jon” + age + “years”; System. out. println(s); Jon 17 years
Перетворення стрічок String to. String() class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } public String to. String() { return "Dimensions are " + width + " by " + depth + " by " + height + ". "; }} class to. String. Demo { public static void main(String args[]) { Box b = new Box(10, 12, 14); String s = "Box b: " + b; // concatenate Box object System. out. println(b); // convert Box to string System. out. println(s); } }
Одержання символів char. At(int where) void get. Chars(int source. Start, int source. End, char target[ ], int target. Start)) class get. Chars. Demo { public static void main(String args[]) { String s = "This is a demo of the get. Chars method. "; int start = 10; int end = 14; char buf[] = new char[end - start]; s. get. Chars(start, end, buf, 0); System. out. println(buf); } } byte [ ] get. Bytes() char to. Char. Array()
Порівняння стрічок (1) boolean equals(Object str) boolean equals. Ignore. Case(Object str) boolean region. Matches(int start. Index, String str 2, int str 2 Start. Index, int num. Chars) boolean region. Matches(boolean ignore. Case, int start. Index, String str 2, int str 2 Start. Index, int num. Chars) boolean start. With(String str) boolean end. With(String str)
Порівняння стрічок (2) boolean equals(String str) class Equals. Not. Equal. To { public static void main(String args[]) { String s 1 = "Hello"; String s 2 = new String(s 1); } } System. out. println(s 1 + " equals " + s 2 + " -> " + s 1. equals(s 2)); System. out. println(s 1 + " == " + s 2 + " -> " + (s 1 == s 2)); Hello equals Hello -> true Hello == Hello -> false
Порівняння стрічок (3) int compare. To(String str) int compare. To. Ignore. Case(String str) class Sort. String { static String arr[] = { "Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "their", "country" }; public static void main(String args[]) { for(int j = 0; j < arr. length; j++) { for(int i = j + 1; i < arr. length; i++) { if(arr[i]. compare. To(arr[j]) < 0) { String t = arr[j]; arr[j] = arr[i]; arr[i] = t; } } System. out. println(arr[j]); } } } Now aid all come country for good is men of the their time to to
Пошук стрічок (1) int index. Of(char ch) int last. Index. Of(char ch) int index. Of(String str) int last. Index. Of(String str) int index. Of(char ch, int start. Index) int last. Index. Of(char ch , int start. Index) int index. Of(String str , int start. Index) int last. Index. Of(String str , int start. Index)
Пошук стрічок (2) } class index. Of. Demo { public static void main(String args[]) { String s = "Now is the time for all good men " + "to come to the aid of their country. "; System. out. println(s); System. out. println("index. Of(t) = " + s. index. Of('t')); System. out. println("last. Index. Of(t) = " + s. last. Index. Of('t')); System. out. println("index. Of(the) = " + s. index. Of("the")); System. out. println("last. Index. Of(the) = " + s. last. Index. Of("the")); System. out. println("index. Of(t, 10) = " + s. index. Of('t', 10)); System. out. println("last. Index. Of(t, 60) = " + s. last. Index. Of('t', 60)); System. out. println("index. Of(the, 10) = " + s. index. Of("the", 10)); System. out. println("last. Index. Of(the, 60) = " + s. last. Index. Of("the", 60)); } Now is the time for all good men to come to the aid of their country. index. Of(t) = 7 last. Index. Of(t) = 65 index. Of(the) = 7 last. Index. Of(the) = 55 index. Of(t, 10) = 11 last. Index. Of(t, 60) = 55 index. Of(the, 10) = 44 last. Index. Of(the, 60) = 55
Модифікація стрічок (1) String substringing(int start. Index)