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;
charAt(int index): Returns the character at the specified index in the string;
substring(int beginIndex): Returns a new string that is a substring of the original string, starting from the specified index;
substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string, starting from the
beginIndexand ending at the
toLowerCase(): Converts the string to lowercase;
toUpperCase(): Converts the string to uppercase;
contains(CharSequence sequence): Checks if the string contains the specified sequence of characters;
replace(char oldChar, char newChar): Replaces all occurrences of the specified oldChar with the newChar;
toCharArray(): Converts the string to a character array.
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?