String Methods Part 1
Part of the Fundamentals section of Coddy's Java journey — lesson 67 of 73.
Java has a rich set of built-in string methods that allow you to perform various operations on strings. These methods provide functionalities for manipulating, searching, and transforming strings. Here are some commonly used string methods:
length(): Returns the number of characters in the string.charAt(int index): Returns the character at the specified index (0-based).substring(int beginIndex): Returns a substring starting from the specified index to the end of the string.
substring(int beginIndex, int endIndex): Returns a substring from thebeginIndex(inclusive) to theendIndex(exclusive).startsWith(String prefix): Checks if the string starts with the specified prefix.endsWith(String suffix): Checks if the string ends with the specified suffix.
toUpperCase(): Converts the string to uppercase.toLowerCase(): Converts the string to lowercase.
Here's an example that demonstrates some of these methods:
String message = "Hello, World!";
int length = message.length();
char firstChar = message.charAt(0);
String sub = message.substring(7);
String sub2 = message.substring(7, 12);
int index = message.indexOf("World");
boolean starts = message.startsWith("Hello");
String lower = message.toLowerCase();
System.out.println("Length: " + length);
System.out.println("First char: " + firstChar);
System.out.println("Substring: " + sub);
System.out.println("Substring 2: " + sub2);
System.out.println("Index of 'World': " + index);
System.out.println("Starts with 'Hello': " + starts);
System.out.println("Lowercase: " + lower);Challenge
EasyCreate a method named analyzeString that takes a string as input and prints the following analysis using string methods:
- The length of the string
- The character at index 4
- The substring starting from index 7 to the end
- The substring from index 3 up to, but not including, index 6
- Whether the string ends with a dot
. - The string converted to uppercase
The output should follow this exact format:
Length: [The length]
Char at 4: [The Char at index 4]
Substring: [The First substring]
Substring 2: [The Second substring]
Ends with dot: [true or false]
Uppercase: [The string in uppercase]Note: Assume the input string will be long enough for all operations.
Cheat sheet
Java provides built-in string methods for manipulating and analyzing strings:
length(): Returns the number of characters in the stringcharAt(int index): Returns the character at the specified index (0-based)substring(int beginIndex): Returns a substring from the specified index to the endsubstring(int beginIndex, int endIndex): Returns a substring from beginIndex (inclusive) to endIndex (exclusive)startsWith(String prefix): Checks if the string starts with the specified prefixendsWith(String suffix): Checks if the string ends with the specified suffixtoUpperCase(): Converts the string to uppercasetoLowerCase(): Converts the string to lowercase
String message = "Hello, World!";
int length = message.length(); // 13
char firstChar = message.charAt(0); // 'H'
String sub = message.substring(7); // "World!"
String sub2 = message.substring(7, 12); // "World"
boolean starts = message.startsWith("Hello"); // true
boolean ends = message.endsWith("."); // false
String upper = message.toUpperCase(); // "HELLO, WORLD!"Try it yourself
import java.util.Scanner;
public class Main {
public static void analyzeString(String str) {
// Write your code here
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();
analyzeString(message);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else