Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Основні методи в String, частина 1 | String
Основи Java
course content

Зміст курсу

Основи Java

Основи Java

1. Початок Роботи
2. Основні Типи та Операції
3. Цикли
4. Масиви
5. String

book
Основні методи в String, частина 1

How to Work with Strings Using Methods?

As mentioned earlier in the "Array" section, we won't delve into what methods are and how to create them. We will discuss this later in a separate course. For now, it will be sufficient for you to know that methods are invoked using a dot (.) after the String variable we created.

Let's take a look at several commonly used methods:

  • length(): Returns the length of the string;
java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string"; int strLength = str.length(); System.out.println(strLength); } }

The first character in a string is at index 0, the second at index 1, and so on. This is called zero-based indexing.

So, when you work with strings, you use these indexes to refer to specific characters in the string.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string"; char characterAtPosition = str.charAt(2); System.out.println(characterAtPosition); } }

The substring(int beginIndex) method takes an integer beginIndex, which indicates the starting position of the substring. It returns a new string starting from the specified index and going to the end of the original string. The beginIndex must be within the string's length.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string"; String substringFromIndex = str.substring(3); System.out.println(substringFromIndex); } }

The substring(int beginIndex, int endIndex) method takes two int parameters: beginIndex (the starting position of the substring) and endIndex (the position just after the last character to include). It returns a new string that starts from beginIndex and ends at endIndex-1, excluding the character at endIndex.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string"; String substringFromIndexRange = str.substring(3, 5); System.out.println(substringFromIndexRange); } }

toLowerCase(): Returns a new String. It returns a new string with all characters converted to lowercase. The original string remains unchanged.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "sTRiNg"; String lowerCaseString = str.toLowerCase(); System.out.println(lowerCaseString); } }

toUpperCase(): Returns a new String. It returns a new string with all characters converted to uppercase. The original string remains unchanged.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string"; String upperCaseString = str.toUpperCase(); System.out.println(upperCaseString); } }

The contains(CharSequence sequence) method takes a CharSequence parameter, which is the sequence of characters to check for in the string. It returns a boolean value: true if the string contains the specified sequence, otherwise false.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string"; boolean containsSubstring = str.contains("a"); System.out.println(containsSubstring); } }

The replace(char oldChar, char newChar) method takes two char parameters: oldChar (the character to be replaced) and newChar (the character to replace it with). It returns a new String with all occurrences of oldChar replaced by newChar, while the original string remains unchanged.

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String str = "string is good"; String modifiedString = str.replace('i', 'f'); System.out.println(modifiedString); } }

toCharArray(): Returns a char array (char[]). It converts the string into an array of characters, where each character of the string is an element in the array.

java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { String str = "string"; char[] array = str.toCharArray(); for (char element : array) { System.out.print(element + " "); } } }

The list turned out to be quite extensive, so we'll continue exploring String methods in the next chapter.

In the following chapter, we will provide practical examples of using these methods.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt