001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2003, Refractions Reserach Inc.
006: * (C) 2003, Ministry of Sustainable Resource Management,
007: * Government of British Columbia, Canada
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation;
012: * version 2.1 of the License.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: */
019: /*
020: $Id: StringUtil.java 20438 2006-07-10 09:04:43Z jgarnett $
021:
022: Copyright (c) 2003, Ministry of Sustainable Resource Management
023: Government of British Columbia, Canada
024:
025: All rights reserved.
026: This information contained herein may not be used in whole
027: or in part without the express written consent of the
028: Government of British Columbia, Canada.
029:
030: */
031:
032: package org.geotools.graph.util;
033:
034: /**
035: * Various string utilities.
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/util/StringUtil.java $
037: */
038: public class StringUtil {
039:
040: /**
041: * Strips any white space of the end of a string.
042: */
043: public static String strip(String s) {
044: StringBuffer sb = new StringBuffer();
045: int spaces = 0;
046: for (int i = 0; i < s.length(); i++) {
047: if (s.charAt(i) == ' ') {
048: if (spaces > 0)
049: return (sb.toString());
050: else
051: spaces++;
052: } else {
053: sb.append(s.charAt(i));
054: spaces = 0;
055: }
056: }
057: return (sb.toString());
058: }
059:
060: /**
061: * Removes any spaces within a string.
062: */
063: public static String noSpaces(String s) {
064: StringBuffer sb = new StringBuffer();
065: for (int i = 0; i < s.length(); i++) {
066: if (s.charAt(i) != ' ')
067: sb.append(s.charAt(i));
068: }
069:
070: return (sb.toString());
071: }
072:
073: /**
074: * Places a post fix on a filename just before the extension dot.
075: */
076: public static String postfixFilename(String filename, String postfix) {
077: int index = filename.lastIndexOf(".");
078: return (filename.substring(0, index) + postfix + filename
079: .substring(index));
080: }
081:
082: /**
083: * Places a prefix on the filename.
084: * <br><br>
085: * Note: Only works for windows style paths.
086: */
087: public static String prefixFilename(String path, String prefix) {
088: int index = path.lastIndexOf("\\");
089: return (path.substring(0, index) + "\\" + prefix + path
090: .substring(index + 1));
091: }
092:
093: /**
094: * Places an extension on a filename if it does not already exist.
095: * @param filename Path to the file.
096: * @param ext Extension of the file.
097: */
098: public static String addExtIfNecessary(String filename, String ext) {
099: if (hasExtension(filename, ext))
100: return (filename);
101: return (setExtension(filename, ext));
102: }
103:
104: public static String setExtension(String filename, String ext) {
105: if (hasExtension(filename, ext))
106: return (filename);
107: int index = filename.lastIndexOf(".");
108: if (index == -1)
109: return (filename + "." + ext);
110: return (filename.substring(0, index + 1) + ext);
111:
112: }
113:
114: public static boolean hasExtension(String filename, String ext) {
115: int index = filename.lastIndexOf(".");
116: if (index == -1)
117: return (false);
118: return (filename.substring(index + 1).compareTo(ext) == 0);
119: }
120:
121: public static String stripPath(String filename) {
122: int index = filename.lastIndexOf("\\");
123: if (index == -1)
124: return (filename);
125: return (filename.substring(index + 1));
126: }
127:
128: public static String stripExtension(String filename) {
129: int index = filename.lastIndexOf(".");
130: return (filename.substring(0, index));
131: }
132:
133: }
|