Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Strings.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Marc Wick.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.util;
00026 
00027 /**
00028  * This class provides utilities for Strings manipulation.
00029  * 
00030  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00031  * @version 1.0
00032  */
00033 public class Strings
00034 {
00035 
00036   /**
00037    * Replaces all occurrences of a String within another String.
00038    * 
00039    * @param sourceString source String
00040    * @param replace text pattern to replace
00041    * @param with replacement text
00042    * @return the text with any replacements processed, <code>null</code> if
00043    *         null String input
00044    */
00045   public static String replace(String sourceString, String replace, String with)
00046   {
00047     if (sourceString == null || replace == null || with == null
00048         || "".equals(replace))
00049     {
00050       return sourceString;
00051     }
00052 
00053     StringBuffer buf = new StringBuffer(sourceString.length());
00054     int start = 0, end = 0;
00055     while ((end = sourceString.indexOf(replace, start)) != -1)
00056     {
00057       buf.append(sourceString.substring(start, end)).append(with);
00058       start = end + replace.length();
00059     }
00060     buf.append(sourceString.substring(start));
00061     return buf.toString();
00062   }
00063 
00064   /**
00065    * Replaces all occurrences of a String within another String. The String to
00066    * be replaced will be replaced ignoring cases, all other cases are preserved
00067    * in the returned string
00068    * 
00069    * @param sourceString source String
00070    * @param replace text to replace, case insensitive
00071    * @param with replacement text
00072    * @return the text with any replacements processed, <code>null</code> if
00073    *         null String input
00074    */
00075   public static String replaceCasePreserving(String sourceString,
00076       String replace, String with)
00077   {
00078     if (sourceString == null || replace == null || with == null)
00079     {
00080       return sourceString;
00081     }
00082     String lower = sourceString.toLowerCase();
00083     int shift = 0;
00084     int idx = lower.indexOf(replace);
00085     int length = replace.length();
00086     StringBuffer resultString = new StringBuffer(sourceString);
00087     do
00088     {
00089       resultString = resultString.replace(idx + shift, idx + shift + length,
00090           with);
00091       shift += with.length() - length;
00092       idx = lower.indexOf(with, idx + length);
00093     }
00094     while (idx > 0);
00095 
00096     return resultString.toString();
00097   }
00098 
00099 }

Generated on Mon Apr 11 22:01:35 2005 for C-JDBC by  doxygen 1.3.9.1