Menu
Coddy logo textTech

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 the beginIndex (inclusive) to the endIndex (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 icon

Challenge

Easy

Create a method named analyzeString that takes a string as input and prints the following analysis using string methods:

  1. The length of the string
  2. The character at index 4
  3. The substring starting from index 7 to the end
  4. The substring from index 3 up to, but not including, index 6
  5. Whether the string ends with a dot .
  6. 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 string
  • charAt(int index): Returns the character at the specified index (0-based)
  • substring(int beginIndex): Returns a substring from the specified index to the end
  • substring(int beginIndex, int endIndex): Returns a substring from beginIndex (inclusive) to endIndex (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
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);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals