01: /*
02: * Copyright 2005-2007 Noelios Consulting.
03: *
04: * The contents of this file are subject to the terms of the Common Development
05: * and Distribution License (the "License"). You may not use this file except in
06: * compliance with the License.
07: *
08: * You can obtain a copy of the license at
09: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
10: * language governing permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL HEADER in each file and
13: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
14: * applicable, add the following below this CDDL HEADER, with the fields
15: * enclosed by brackets "[]" replaced with your own identifying information:
16: * Portions Copyright [yyyy] [name of copyright owner]
17: */
18:
19: package com.noelios.restlet.util;
20:
21: /**
22: * String manipulation utilities.
23: *
24: * @author Jerome Louvel (contact@noelios.com)
25: */
26: public class StringUtils {
27: /**
28: * Strips a delimiter character from both ends of the source string.
29: *
30: * @param source
31: * The source string to strip.
32: * @param delimiter
33: * The character to remove.
34: * @return The stripped string.
35: */
36: public static String strip(String source, char delimiter) {
37: return strip(source, delimiter, true, true);
38: }
39:
40: /**
41: * Strips a delimiter character from a source string.
42: *
43: * @param source
44: * The source string to strip.
45: * @param delimiter
46: * The character to remove.
47: * @param start
48: * Indicates if start of source should be stripped.
49: * @param end
50: * Indicates if end of source should be stripped.
51: * @return The stripped source string.
52: */
53: public static String strip(String source, char delimiter,
54: boolean start, boolean end) {
55: int beginIndex = 0;
56: int endIndex = source.length();
57: boolean stripping = true;
58:
59: // Strip beginning
60: while (stripping && (beginIndex < endIndex)) {
61: if (source.charAt(beginIndex) == delimiter) {
62: beginIndex++;
63: } else {
64: stripping = false;
65: }
66: }
67:
68: // Strip end
69: stripping = true;
70: while (stripping && (beginIndex < endIndex - 1)) {
71: if (source.charAt(endIndex - 1) == delimiter) {
72: endIndex--;
73: } else {
74: stripping = false;
75: }
76: }
77:
78: return source.substring(beginIndex, endIndex);
79: }
80:
81: }
|