String str = "https://www.example.com/home/index.html"; String delimiter = "/"; String result = StringUtils.substringBeforeLast(str, delimiter); System.out.println(result);
String filePath = "C:/Users/johndoe/Documents/report.docx"; String delimiter = "/"; String result = StringUtils.substringBeforeLast(filePath, delimiter); System.out.println(result);Here, we want to extract the directory path of a file by removing the file name (and extension) from the full file path. The result will be "C:/Users/johndoe/Documents". In both cases, we are using the StringUtils class from the org.apache.commons.lang package to manipulate strings in a more efficient and convenient way than would be possible with standard Java string methods. Overall, the StringUtils class is a useful tool for developers who frequently work with strings in Java, and is part of the larger Apache Commons Lang library of utilities.