QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Extract Text Between Delimiters

Extracts a text between two given delimiters from a string

  • string
  • delimiters
  • start
  • end

Code

public static String extractBetweenDelimiters(String text, String start, String end) {
    int startIndex = text.indexOf(start);
    int endIndex = text.indexOf(end, startIndex + start.length());
    
    if (startIndex == -1 || endIndex == -1)
        return "";
    
    return text.substring(startIndex + start.length(), endIndex);
}

Usage

System.out.println(extractBetweenDelimiters("hello, world!", ",", "!")); // " world"