When you work with strings that you know is in a specific format, you can use this to your advantage to extract pieces of data from it.
More specifically, if you know the string will contain a specific character, you can get the substring before that character and use it for whatever you need.
In this post, we'll learn all the different ways we can get the substring before a character in Java.
Using split
An easy way to accomplish this is to just split the string on the character you want to extract.
This will divide up the string into an array of string values, with the first element being what you want.
Let's start with our example string:
JAVA
Stringstring="Hello:World";
Let's say we wanted to get the substring before the colon: