Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Basic Methods In String | 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

How to work with strings using methods?

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 method charAt(int index) expects an integer index to specify the position of the character, starting from 0. It returns the character at that position. The index 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"; 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 delve deeper into string methods later.

Everything was clear?

How can we improve it?

Thanks for your feedback!

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