code: public static String removeLeadingAndTrailingDelimiter(String str, String delimiter) {
          final int strLength = str.length();
          final int delimiterLength = delimiter.length();
  
          final boolean leadingDelimiter = str.startsWith(delimiter);
          final boolean trailingDelimiter = strLength > delimiterLength && str.endsWith(delimiter);
          if (!leadingDelimiter && !trailingDelimiter) {
              return str;
          } else {
              final int startingDelimiterIndex = leadingDelimiter ? delimiterLength : 0;
              final int endingDelimiterIndex = trailingDelimiter ? Math.max(strLength - delimiterLength, startingDelimiterIndex) : strLength;
              return str.substring(startingDelimiterIndex, endingDelimiterIndex);
          }
      }
docs: Removes the leading and trailing delimiter from a string.
  
  @param str String to process.
  @param delimiter Delimiter to remove.
  @return The string with the leading and trailing delimiter removed.