java常用方法(String)6

public static final String module = StringUtil.class.getName();

/**
* Replaces all occurances of oldString in mainString with newString
* @param mainString The original string
* @param oldString The string to replace
* @param newString The string to insert in place of the old
* @return mainString with all occurances of oldString replaced by newString
*/
public static String replaceString(String mainString, String oldString, String newString) {
if (mainString == null) {
return null;
}
if (oldString == null || oldString.length() == 0) {
return mainString;
}
if (newString == null) {
newString = "";
}

int i = mainString.lastIndexOf(oldString);

if (i < 0) return mainString;

StringBuffer mainSb = new StringBuffer(mainString);

while (i >= 0) {
mainSb.replace(i, i + oldString.length(), newString);
i = mainString.lastIndexOf(oldString, i - 1);
}
return mainSb.toString();
}

/**
* Creates a single string from a List of strings seperated by a delimiter.
* @param list a list of strings to join
* @param delim the delimiter character(s) to use. (null value will join with no delimiter)
* @return a String of all values in the list seperated by the delimiter
*/
public static String join(List list, String delim) {
if (list == null || list.size() < 1)
return null;
StringBuffer buf = new StringBuffer();
Iterator i = list.iterator();

while (i.hasNext()) {
buf.append((String) i.next());
if (i.hasNext())
buf.append(delim);
}
return buf.toString();
}
public static String join(String prefix,List list, String delim) {
if (list == null || list.size() < 1)
return null;
StringBuffer buf = new StringBuffer();
Iterator i = list.iterator();

while (i.hasNext()) {
buf.append(prefix);
buf.append((String) i.next());
if (i.hasNext())
buf.append(delim);
}
return buf.toString();
}

/**
* Splits a String on a delimiter into a List of Strings.
* @param str the String to split
* @param delim the delimiter character(s) to join on (null will split on whitespace)
* @return a list of Strings
*/
public static List split(String str, String delim) {
List splitList = null;
StringTokenizer st = null;

if (str == null)
return splitList;

if (delim != null)
st = new StringTokenizer(str, delim);
else
st = new StringTokenizer(str);

if (st != null && st.hasMoreTokens()) {
splitList = new ArrayList();

while (st.hasMoreTokens())
splitList.add(st.nextToken());
}
return splitList;
}

/**
* Encloses each of a List of Strings in quotes.
* @param list List of String(s) to quote.
*/
public static List quoteStrList(List list) {
List tmpList = list;

list = new ArrayList();
Iterator i = tmpList.iterator();

while (i.hasNext()) {
String str = (String) i.next();

str = "'" + str + "'";
list.add(str);
}
return list;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值