String str = "Hello-World"; String substr = StringUtils.substringBefore(str, "-"); System.out.println(substr); //output: Hello
String str = "John Smith"; String substr = StringUtils.substringBefore(str, " "); System.out.println(substr); //output: John
String str = "HelloWorld"; String substr = StringUtils.substringBefore(str, "-"); System.out.println(substr); //output: HelloWorldIn all of these examples, we use the StringUtils class from the Apache Commons Lang3 library to get the substring before a specified delimiter using the substringBefore method.