You can use the split() method. It returns an array String[], so you can access to an element by using the syntax someArray[index]. Since you want to get the first elemnt, you can use [0].

String first_word = s.split(",")[0];

Note:

  • Indices in most programming languages start with 0. So the first elemnt will be in the index 0. The second in the index 1. So on.
Answer from Christian Tapia on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › split a string only on the first occurrence of delimiter
Split a String Only on the First Occurrence of Delimiter | Baeldung
August 7, 2025 - We can see that both the parts are separated by a ” ” (space), just like the other words in the right part. So, our delimiter is going to be the space character. The split() instance method from the String class splits the string based on the provided regular expression.
Discussions

regex - java - after splitting a string, what is the first element in the array? - Stack Overflow
Someone who has, or who knows the java regex code internally might be able to explain this better. 2013-11-15T04:27:02.62Z+00:00 ... This seems to be a pretty clear explanation. "Split the string when you encounter nothing, then go to the next character". Note - that second part is important. You don't get an infinite array of empty strings; only the first element ... More on stackoverflow.com
🌐 stackoverflow.com
split string only on first instance - java - Stack Overflow
If there are 2 matches to the expression, the second element of the returned array will not be split. 2018-01-03T15:25:29.817Z+00:00 ... You can use the indexOf method on String which will returns the first Occurance of the given character, Using that index you can get the desired output More on stackoverflow.com
🌐 stackoverflow.com
How to consider first token of splitted string using Java 8 streams - Stack Overflow
Parentheses after a method reference String::split(/*anything*/) is always invalid. See the Oracle tutorial on method references for more info. ... thank u for the info and the link, but for some reason the code returns an empty list ... also I don;t know how will it handle a case when there;s no \\. in the string element 2018-05-30T13:24:09.857Z... More on stackoverflow.com
🌐 stackoverflow.com
java - String split method returning first element as empty using regex - Stack Overflow
But the split method returning me String array with first value as empty, and then "1" inside index 1 and 2 respectively. Could anyone please tell me what's wrong I'm doing in this regex expression, so that I only get the digits in the returned String array from split method? ... Either update to Java 8 ... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
🌐
Medium
medium.com › @khalludi123 › using-javas-split-to-find-first-occurrence-5c09cb8cdf8a
Using Java’s .split() to Find First Occurrence | by Khalid Ali | Medium
May 6, 2020 - In summary, when the split method ... “That rug looks nice” — → [“That ”, “ looks nice”] When the search term is at the beginning or the end of the String, the output array has one element....
Top answer
1 of 4
14

Consider the split expression ",1,2,3,4".split(",");

What would you expect? Right, an empty-string to start with. In your case you have a 'nothing' in front of the first 'a' as well as one behind it.

Update: comments indicate this explanation is not enough of an explanation (which it may not be)... but, it really is this simple: the engine starts at the beginning of the string, and it looks to see if what's in front of it matches the pattern. If it does, it assigns what's behind it to a new item in the split.

On the first character, it has "" (nothing behind it), and it looks to see if there's "" (the pattern) in front of it. There is, so it creates a "" match.

It then moves on, and it has 'a' behind it, and again, it again has "" in front of it. So the second result is an "a" string.

An interesting observation is that, if you use split("", -1) you will also get an empty-string result in the last position of the result array.


Edit 2: If I wrack my brains further, and consider this to be an academic exercise (I would not recommend this in real life...) I can think of only one good way to do a regex split() of a String into a String[] array with 1 character in each string (as opposed to char[] - which other people have given great answers for....).

String[] chars = str.split("(?<=.)", str.length());

This will look behind each character, in a non-capturing group, and split on that, and then limit the array size to the number of characters (you can leave the str.length() out, but if you put -1 you will get an extra space at the end)

Borrowing nitro2k01's alternative (below in the comments) which references the string beginning and end, you can split reliably on:

String[] chars = str.split("(?!(^|$))");
2 of 4
2

You can just use the built in java method from the string class. myString.toCharArray() the empty string is being stored at index 0

🌐
Apdp
moodle-escola.apdp.pt › jmbi › java-8-split-string-get-first-element
java 8 split string get first element
Indices in most programming languages start with 0. So the first elemnt will be in the index 0. The second in the index 1. So on. If you look at String.split (String regrex), you will find that it will return an array of strings. Java 8 includes support for lambda expressions, and offers a ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - String[] splitted = input.trim().split("\\s*,\\s*"); Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter. We can achieve the same result by using Java 8 Stream features:
🌐
Medium
gainjavaknowledge.medium.com › how-to-split-a-string-only-on-the-first-occurrence-of-delimiter-1c8e1eb5ae24
How to Split a String Only on the First Occurrence of Delimiter ? | by Real World Java | Medium
May 10, 2022 - [“This”, “is”, “a”, “java” “program”] But now our requirement is that , we want output in this format : ... We are splitting over only the first occurrence of the delimiter. We can do this using the second parameter of the split() function, which is the limit. String str ...
🌐
Guru99
guru99.com › home › java tutorials › java string split() method with example
Java String split() Method with Example
July 30, 2026 - Leading empty strings behave differently again: splitting ",a,b" always yields an empty first element regardless of the limit, because the pattern matches at position zero. Guard against it by trimming the input, or by filtering the resulting array before use.
🌐
Briebug
blog.briebug.com › home › articles › using the java string.split() method
Using the Java String.split() Method | Briebug
October 13, 2022 - Notice, it pulls the first string before the delimiter and puts it in the first element of the array. Because the limit is 2, the rest of the string is placed into the second element, with the delimiter still in the string. Example number 3, with the limit set to 5, does the same as the second example (limit == 2), but the string is split into 5 elements in our String array.
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
Java Examples Java Videos Java ... myStr.split(regex); for (String s : myArray) { System.out.println(s); } ... The split() method splits a string into an array of substrings using a regular expression as the separator....
🌐
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java | mkyong.com
February 9, 2022 - In Java, we can use String#split() to split a string. String phone = "012-3456789"; // String#split(string regex) accepts regex as the argument String[] output = phone.split("-"); String part1 = output[0];
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - Each byte receives the 8 low-order bits of the corresponding character. The eight high-order bits of each character are not copied and do not participate in the transfer in any way. The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1.
🌐
Pass4sure
pass4sure.com › blog › mastering-string-splitting-in-java-extracting-the-final-element-with-precision-and-performance
Mastering String Splitting in Java: Extracting the Final Element with Precision and Performance – IT Exams Training – Pass4Sure
You may want to split only the first two colons to extract log level and timestamp, leaving the rest intact as the message: ... This yields three segments: “INFO”, “2025-06-30”, and the full message. Using a limit prevents over-splitting and preserves structure where necessary. Negative limits, as discussed earlier, instruct Java to preserve all tokens, including trailing empty strings.
🌐
Stack Abuse
stackabuse.com › how-to-split-a-string-in-java
How to Split a String in Java
September 20, 2023 - Once the string is split, the result is returned as an array of Strings. Strings in the returned array appear in the same order as in the original string. The results are packed in the String array. To retrieve the separate names, we can access each element:
🌐
YouTube
youtube.com › watch
Java 8 Stream API 🔥| Find First Element (Easy Trick) | Most Asked Interview Question. - YouTube
September 10, 2026 - 🚀 In this Java 8 Stream API tutorial, you'll learn how to find the first element from a list using the **findFirst()** method.This is one of the commonly as...