📄 stringutil.java
字号:
/*
* OPIAM Suite
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package opiam.admin.faare.utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;
/**
* Class with utility methods for strings.
*/
public final class StringUtil
{
/** Utility class. */
private StringUtil()
{
}
/** Instance of logger. */
private static Logger _logger = Logger.getLogger(StringUtil.class);
/**
* Replace a pattern in string.
*
* @param string Input string.
* @param oldString Pattern to be replaced.
* @param newString Replacing pattern.
*
* @return Modified string.
*/
public static String replace(String string, String oldString,
String newString)
{
StringBuffer returnedString = new StringBuffer();
int i = string.indexOf(oldString);
int j = oldString.length();
returnedString.append(string.substring(0, i));
returnedString.append(newString);
returnedString.append(string.substring(i + j));
return returnedString.toString();
}
/**
* Gets a substring delimited a by separator from a string.
*
* @param s Input string.
* @param separator Separator.
*
* @return Substring.
*/
public static String getSubstring(String s, String separator)
{
int startSep = s.indexOf(separator);
StringBuffer temp = new StringBuffer();
temp.append(s.substring(startSep + 1));
int endSep = temp.toString().indexOf(separator);
StringBuffer temp2 = new StringBuffer();
temp2.append(temp.substring(0, endSep));
return temp2.toString();
}
/**
* Generates a unique random string.
*
* @return Generated string.
*/
public static String getUniqueId()
{
long now = System.currentTimeMillis();
Random rand = new Random();
return (Long.toString(now) +
Integer.toString(Math.abs(rand.nextInt())));
}
/**
* Compares two string enumerations.
*
* @param e1 First string enumeration
* @param e2 Second string enumeration
*
* @return 0 if both enumerations are equals, 1 if they differ.
*/
public static int compareStringEnumeration(Enumeration e1, Enumeration e2)
{
int res = 0;
List l1 = new ArrayList();
List l2 = new ArrayList();
CollectionUtils.addAll(l1, e1);
CollectionUtils.addAll(l2, e2);
if (l1.size() != l2.size())
{
res = 1;
}
else
{
Collections.sort(l1);
Collections.sort(l2);
int i = 0;
boolean equal = true;
while (equal && (i < l1.size()))
{
if (((String) l1.get(i)).compareTo((String) l2.get(i)) != 0)
{
equal = false;
res = 1;
}
i++;
}
}
return res;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -