Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Methods In String Part 2 | String
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

book
Basic Methods In String Part 2

Putting Methods into Practice

Now, let's dive into code examples for each of these methods. You'll quickly grasp their functionality:

java

Main

copy
12345678910111213141516171819
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(); // creating a char array from our string 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:

Task 1

Display an array of characters containing the characters of a given string. First, convert each character to uppercase, and then print it on the screen.

java

Main

copy
12345678910111213
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."

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { String string = "13snhelicopterkpf-qo"; String result = string //creating the new string that will contain modified value of the first string .substring(4, string.length() - 6) // substring from the 4 symbol (h) to length - 6 symbol (r) .replace("e", "u"); // replacing all the letters 'e' to letter 'u' System.out.println(result); // printing the result } }

You have observed that it's possible to use various methods simultaneously and how they will impact our string. I understand that the topic is quite intricate but highly significant. In the next chapter, you will tackle such a task independently and solidify your understanding through practical application.

1. What will be printed to the console?
2. What will be printed to the console?
3. What will be printed to the console?
What will be printed to the console?

What will be printed to the console?

Select the correct answer

What will be printed to the console?

What will be printed to the console?

Select the correct answer

What will be printed to the console?

What will be printed to the console?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt