Course Content
Java Basics
Java Basics
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.
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 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.
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 delve deeper into string methods later.
Thanks for your feedback!