Зміст курсу
Основи Java
Основи Java
Основні методи в рядках, частина 2
Putting Methods into Practice
Now, let's dive into code examples for each of these methods. You'll quickly grasp their functionality:
Main
package com.example; public class Main { public static void main(String[] args) { String string = "Hello world"; System.out.println("string length = " + string.length()); System.out.println("char at 0s position is " + string.charAt(0)); System.out.println("substring from third position = " + string.substring(3)); System.out.println("substring from third to seventh position = " + string.substring(3, 7)); System.out.println("string in lower case = " + string.toLowerCase()); System.out.println("string in upper case = " + string.toUpperCase()); System.out.println("checking if string contains 'ell' = " + string.contains("ell")); System.out.println("replacing all the 'l' characters to 'b' = " + string.replace('l', 'b')); char[] chars = string.toCharArray(); System.out.println("as we can see, after all of this " + "operations our string hasn't changed, it is still " + string); } }
We just used many methods on a single string, but in the end, its value remained unchanged. That's exactly what I mentioned in the previous section. The value of a string doesn't alter without creating a new object in memory.
Now, let's explore a few practical applications of these methods through the following example tasks:
Ми просто застосували багато методів до одного рядка, але в результаті його значення залишилося незмінним. Це саме те, про що я говорив у попередньому розділі. Значення рядка не змінюється без створення нового об'єкта в пам'яті.
Тепер давайте розглянемо кілька практичних застосувань цих методів за допомогою наступних прикладів:
Main
package com.example; public class Main { public static void main(String[] args) { String string = "Make Java great again"; char[] array = string.toUpperCase().toCharArray(); //as you can see we can use multiple methods in one line for (char element : array) { System.out.print(element); } //we use for-each loop to print every element of our array } }
Task 2
Given a string that contains the word "helicopter," your task is to remove all the unnecessary letters from this string and display the new string on the screen, replacing all occurrences of the letter "e" with "u."
Task 2
You upload a document title, and it needs to be converted to uppercase with underscores _
added between words to create a system-friendly filename.
Main
package com.example; public class Main { public static void main(String[] args) { String documentTitle = "Quarterly financial report"; // Document title String systemFileName = documentTitle.toUpperCase().replace(" ", "_"); System.out.println(systemFileName); // Output } }
This code processes a document title to create a system-friendly filename. It uses toUpperCase()
to convert all letters to uppercase and replace(" ", "_")
to replace spaces with underscores, ensuring consistency and compatibility for file storage. The result is then displayed on the console.
1. Що буде виведено на консоль?
2. Що буде виведено на консоль?
3. What will be printed to the console?
Дякуємо за ваш відгук!