Basic Methods In String Part 1Basic 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;
java

Main.java

  • charAt(int index): Returns the character at the specified index in the string;
java

Main.java

  • substring(int beginIndex): Returns a new string that is a substring of the original string, starting from the specified index;
java

Main.java

  • substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string, starting from the beginIndex and ending at the endIndex-1;
java

Main.java

  • toLowerCase(): Converts the string to lowercase;
java

Main.java

  • toUpperCase(): Converts the string to uppercase;
java

Main.java

  • contains(CharSequence sequence): Checks if the string contains the specified sequence of characters;
java

Main.java

  • replace(char oldChar, char newChar): Replaces all occurrences of the specified oldChar with the newChar;
java

Main.java

  • toCharArray(): Converts the string to a character array.
java

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?

Section 5. Chapter 2