📄 stringutils.java
字号:
package org.apache.velocity.util;
/*
* Copyright 2001,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.FileReader;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Map;
/**
* This class provides some methods for dynamically
* invoking methods in objects, and some string
* manipulation methods used by torque. The string
* methods will soon be moved into the turbine
* string utilities class.
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @version $Id: StringUtils.java,v 1.16.8.1 2004/03/03 23:23:07 geirm Exp $
*/
public class StringUtils
{
/**
* Line separator for the OS we are operating on.
*/
private static final String EOL = System.getProperty("line.separator");
/**
* Length of the line separator.
*/
private static final int EOL_LENGTH = EOL.length();
/**
* Concatenates a list of objects as a String.
*
* @param list The list of objects to concatenate.
* @return A text representation of the concatenated objects.
*/
public String concat(List list)
{
StringBuffer sb = new StringBuffer();
int size = list.size();
for (int i = 0; i < size; i++)
{
sb.append(list.get(i).toString());
}
return sb.toString();
}
/**
* Return a package name as a relative path name
*
* @param String package name to convert to a directory.
* @return String directory path.
*/
static public String getPackageAsPath(String pckge)
{
return pckge.replace( '.', File.separator.charAt(0) ) + File.separator;
}
/**
* <p>
* Remove underscores from a string and replaces first
* letters with capitals. Other letters are changed to lower case.
* </p>
*
* <p>
* For example <code>foo_bar</code> becomes <code>FooBar</code>
* but <code>foo_barBar</code> becomes <code>FooBarbar</code>.
* </p>
*
* @param data string to remove underscores from.
* @return String
* @deprecated Use the org.apache.commons.util.StringUtils class
* instead. Using its firstLetterCaps() method in conjunction
* with a StringTokenizer will achieve the same result.
*/
static public String removeUnderScores (String data)
{
String temp = null;
StringBuffer out = new StringBuffer();
temp = data;
StringTokenizer st = new StringTokenizer(temp, "_");
while (st.hasMoreTokens())
{
String element = (String) st.nextElement();
out.append ( firstLetterCaps(element));
}
return out.toString();
}
/**
* <p>
* 'Camels Hump' replacement of underscores.
* </p>
*
* <p>
* Remove underscores from a string but leave the capitalization of the
* other letters unchanged.
* </p>
*
* <p>
* For example <code>foo_barBar</code> becomes <code>FooBarBar</code>.
* </p>
*
* @param data string to hump
* @return String
*/
static public String removeAndHump (String data)
{
return removeAndHump(data,"_");
}
/**
* <p>
* 'Camels Hump' replacement.
* </p>
*
* <p>
* Remove one string from another string but leave the capitalization of the
* other letters unchanged.
* </p>
*
* <p>
* For example, removing "_" from <code>foo_barBar</code> becomes <code>FooBarBar</code>.
* </p>
*
* @param data string to hump
* @param replaceThis string to be replaced
* @return String
*/
static public String removeAndHump (String data,String replaceThis)
{
String temp = null;
StringBuffer out = new StringBuffer();
temp = data;
StringTokenizer st = new StringTokenizer(temp, replaceThis);
while (st.hasMoreTokens())
{
String element = (String) st.nextElement();
out.append ( capitalizeFirstLetter(element));
}//while
return out.toString();
}
/**
* <p>
* Makes the first letter caps and the rest lowercase.
* </p>
*
* <p>
* For example <code>fooBar</code> becomes <code>Foobar</code>.
* </p>
*
* @param data capitalize this
* @return String
*/
static public String firstLetterCaps ( String data )
{
String firstLetter = data.substring(0,1).toUpperCase();
String restLetters = data.substring(1).toLowerCase();
return firstLetter + restLetters;
}
/**
* <p>
* Capitalize the first letter but leave the rest as they are.
* </p>
*
* <p>
* For example <code>fooBar</code> becomes <code>FooBar</code>.
* </p>
*
* @param data capitalize this
* @return String
*/
static public String capitalizeFirstLetter ( String data )
{
String firstLetter = data.substring(0,1).toUpperCase();
String restLetters = data.substring(1);
return firstLetter + restLetters;
}
/**
* Create a string array from a string separated by delim
*
* @param line the line to split
* @param delim the delimter to split by
* @return a string array of the split fields
*/
public static String [] split(String line, String delim)
{
List list = new ArrayList();
StringTokenizer t = new StringTokenizer(line, delim);
while (t.hasMoreTokens())
{
list.add(t.nextToken());
}
return (String []) list.toArray(new String[list.size()]);
}
/**
* Chop i characters off the end of a string.
* This method assumes that any EOL characters in String s
* and the platform EOL will be the same.
* A 2 character EOL will count as 1 character.
*
* @param string String to chop.
* @param i Number of characters to chop.
* @return String with processed answer.
*/
public static String chop(String s, int i)
{
return chop(s, i, EOL);
}
/**
* Chop i characters off the end of a string.
* A 2 character EOL will count as 1 character.
*
* @param string String to chop.
* @param i Number of characters to chop.
* @param eol A String representing the EOL (end of line).
* @return String with processed answer.
*/
public static String chop(String s, int i, String eol)
{
if ( i == 0 || s == null || eol == null )
{
return s;
}
int length = s.length();
/*
* if it is a 2 char EOL and the string ends with
* it, nip it off. The EOL in this case is treated like 1 character
*/
if ( eol.length() == 2 && s.endsWith(eol ))
{
length -= 2;
i -= 1;
}
if ( i > 0)
{
length -= i;
}
if ( length < 0)
{
length = 0;
}
return s.substring( 0, length);
}
public static StringBuffer stringSubstitution( String argStr,
Hashtable vars )
{
return stringSubstitution( argStr, (Map) vars );
}
/**
* Perform a series of substitutions. The substitions
* are performed by replacing $variable in the target
* string with the value of provided by the key "variable"
* in the provided hashtable.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -