Your code looks great! If you wanted to make it shorter you could use the ternary operator:

public String firstTwo(String str) {
    return str.length() < 2 ? str : str.substring(0, 2);
}
Answer from Andrew Jenkins on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › get first n characters in a string in java
Get First n Characters in a String in Java | Baeldung
July 20, 2024 - Similarly, the method returns the first three characters “Wel” of the string “Welcome”. We should remember that this method throws IndexOutOfBoundsException if beginIndex or endIndex is negative, or if endIndex is greater than the string length, or when beginIndex is greater than endIndex. The String class provides the chars() as another option to retrieve the first n characters. This new method is introduced in Java 9 to manipulate a given string as a Stream.
🌐
Home and Learn
homeandlearn.co.uk › java › substring.html
java for complete beginners - substring
Start a new programme to test this out. Add a print line to the end and your code should be this: When the programme runs, the Output window should look like this: So the substring method has allowed us to grab the first two characters of the name "Bill". To get the first characters, we had ...
🌐
IndiBlogger
indiblogger.in › indipost.php
How to get first two characters of a string in java
We can get first two character of a string in java by using subString() method in java To get first N numbers of a string s.substring(0,n); Lets see an example java program on how to get first two characters or first N characters. Program #1: Java Program
🌐
Dirask
dirask.com › posts › Java-get-first-2-characters-from-string-jo4lN1
Java - get first 2 characters from string
The below example shows how to use substring() method to get the first 2 characters from the text string. public class StringUtils { public static String getFirstCharacters(String text, int charactersCount) { int offset = Math.min(charactersCount, ...
🌐
ISA-Ali
alias-i.com › home › java get first character of string: essential techniques
Java Get First Character of String: Essential Techniques - ISA-Ali
August 18, 2023 - ... Java provides versatile tools to address this need, building upon the techniques explored earlier. To extract the first two characters using the `charAt()` method, you invoke the method twice, once for each index.
🌐
w3resource
w3resource.com › java-exercises › string › java-string-exercise-63.php
Java - The first two characters of a string appear at the end
import java.util.*; // Define a class named Main public class Main { // Method to check if the first two characters of a string appear at the end of the string public boolean firstInLast(String str) { if (str.length() < 2) // If the length of ...
🌐
CodingTechRoom
codingtechroom.com › question › how-to-extract-the-first-two-characters-from-a-string-in-java
How to Extract the First Two Characters from a String in Java? - CodingTechRoom
In Java, you can extract the first two characters from a string using the `substring()` method from the `String` class. This method allows you to specify the starting and ending indices, enabling you to retrieve a specific portion of the string.
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › string › get first 4 chars of a string in java
Get First 4 Chars of a String in Java
December 12, 2022 - For example, to get the first 2 characters we can change the method call to input.substring(0, 2). Include the latest version of Apache commons lang from the Maven repo. <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons...
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-to-get-first-and-last-character-of-String-in-java-example.html
How to get first and last character of String in Java - Example
Here is a sample java program to demonstrate how you can use the charAt() method from the CharSequence interface to retrieve the leading and trailing character from String in Java.
🌐
Coderanch
coderanch.com › t › 391349 › java › characters-string
getting first characters from string... (Beginning Java forum at Coderanch)
In VBScript there is functions LEFT() and MID() for performing these operations... How about java? I didn't found it in jsp specification.. Thx in advance.. ... Ilja What you are looking for is the substring method of the string class. It takes two ints as arguments.
🌐
Sololearn
sololearn.com › en › Discuss › 2185882 › how-to-extract-two-characters-from-a-given-string-at-the-same-time
How to extract two characters from a given string at the same time? | Sololearn: Learn to code for FREE!
February 29, 2020 - if you don't like indexes import java.util.Scanner; .. String str = "12345678"; Scanner sc = new Scanner(str) .useDelimiter("(?<=\\G..)"); //(?<= look behind, \\G from last match, .. two chars while( sc.hasNext() ) { System.out.println( sc.next() ); } ... Everytime you can't go for index finding to put it in substring function. ... I want first two characters ...
🌐
Blogger
javahungry.blogspot.com › 2021 › 04 › get-first-character-of-string-java.html
How to get the first character of a string in Java | Java Hungry
public class FirstCharacterTwo ... "+charArray[0]); } } Output: first character is A · Another way to get the first character of a string in Java is toCharArray() method....
🌐
W3Docs
w3docs.com › java
Java - removing first character of a string | W3Docs
In this example, the substring() method extracts the characters of the string starting at index 1 and ending at the end of the string. This removes the first character (the character at index 0) of the string.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-find-the-first-and-last-character-of-a-string-in-java
How to find the first and last character of a string in Java - GeeksforGeeks
Below is the implementation of ... { // Finding string length int n = str.length(); // First character of a string char first = str.charAt(0); // Last character of a string char last = str.charAt(n - 1); // Printing first and ...
Published: July 15, 2025
🌐
W3Schools
w3schools.com › java › ref_string_charat.asp
Java String charAt() Method
Java Examples Java Videos Java ... Java Syllabus Java Study Plan Java Interview Q&A ... The charAt() method returns the character at the specified index in a string....
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Retrieving First n Characters in a String in Java - Java Code Geeks
April 10, 2024 - The substring() method in StringUtils has a null-safety advantage over the core JDK implementation. It returns an empty String instead of throwing an exception if the input String is null.
🌐
Java67
java67.com › 2018 › 05 › java-string-chartat-example-how-to-get-first-last-character.html
How to get First and Last Character of String in Java? charAt Example | Java67
You can further see these free Java programming courses to learn more about String and characters in Java. If you want the first letter from a given String then just call the charAt(0) because the first character stays at the zeroth position ...
🌐
Delft Stack
delftstack.com › home › howto › java › java first character of string
How to Get the First Character of a String in Java | Delft Stack
February 2, 2024 - Let’s learn the different ways of fetching the first character from a string. The charAt() method takes an integer index value as a parameter and returns the character present at that index.