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 ) ; } }
Removes the leading and trailing delimiter from a string .