Conteúdo do Curso
Noções Básicas de Java
Noções Básicas de Java
Métodos Básicos em String - Parte 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
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.
Main
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.
Main
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
.
Main
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.
Main
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.
Main
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
.
Main
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.
Main
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.
Main
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.
Obrigado pelo seu feedback!