Basic Methods In String Part 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;
Main.java
charAt(int index)
: Returns the character at the specified index in the string;
Main.java
substring(int beginIndex)
: Returns a new string that is a substring of the original string, starting from the specified index;
Main.java
substring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string, starting from thebeginIndex
and ending at theendIndex-1
;
Main.java
toLowerCase()
: Converts the string to lowercase;
Main.java
toUpperCase()
: Converts the string to uppercase;
Main.java
contains(CharSequence sequence)
: Checks if the string contains the specified sequence of characters;
Main.java
replace(char oldChar, char newChar)
: Replaces all occurrences of the specified oldChar with the newChar;
Main.java
toCharArray()
: Converts the string to a character array.
Main.java
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.
Everything was clear?
Course Content
Java Basics
Java Basics
Basic Methods In String Part 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;
Main.java
charAt(int index)
: Returns the character at the specified index in the string;
Main.java
substring(int beginIndex)
: Returns a new string that is a substring of the original string, starting from the specified index;
Main.java
substring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string, starting from thebeginIndex
and ending at theendIndex-1
;
Main.java
toLowerCase()
: Converts the string to lowercase;
Main.java
toUpperCase()
: Converts the string to uppercase;
Main.java
contains(CharSequence sequence)
: Checks if the string contains the specified sequence of characters;
Main.java
replace(char oldChar, char newChar)
: Replaces all occurrences of the specified oldChar with the newChar;
Main.java
toCharArray()
: Converts the string to a character array.
Main.java
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.
Everything was clear?