java常用方法(String)7

/**
* Creates a Map from an encoded name/value pair string
* @param str The string to decode and format
* @param trim Trim whitespace off fields
* @return a Map of name/value pairs
*/
public static Map strToMap(String str, boolean trim) {
if (str == null) return null;
Map decodedMap = new HashMap();
List elements = split(str, "|");
Iterator i = elements.iterator();

while (i.hasNext()) {
String s = (String) i.next();
List e = split(s, "=");

if (e.size() != 2) {
continue;
}
String name = (String) e.get(0);
String value = (String) e.get(1);
if (trim) {
if (name != null) {
name = name.trim();
}
if (value != null) {
value = value.trim();
}
}

try {
decodedMap.put(URLDecoder.decode(name, "UTF-8"), URLDecoder.decode(value, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
}
}
return decodedMap;
}

/**
* Creates a Map from an encoded name/value pair string
* @param str The string to decode and format
* @return a Map of name/value pairs
*/
public static Map strToMap(String str) {
return strToMap(str, false);
}

/**
* Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
* @param map The Map of name/value pairs
* @return String The encoded String
*/
public static String mapToStr(Map map) {
if (map == null) return null;
StringBuffer buf = new StringBuffer();
Set keySet = map.keySet();
Iterator i = keySet.iterator();
boolean first = true;

while (i.hasNext()) {
Object key = i.next();
Object value = map.get(key);

if (!(key instanceof String) || !(value instanceof String))
continue;
String encodedName = null;
try {
encodedName = URLEncoder.encode((String) key, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
String encodedValue = null;
try {
encodedValue = URLEncoder.encode((String) value, "UTF-8");
} catch (UnsupportedEncodingException e) {
}

if (first)
first = false;
else
buf.append("|");

buf.append(encodedName);
buf.append("=");
buf.append(encodedValue);
}
return buf.toString();
}

/**
* Create a Map from a List of keys and a List of values
* @param keys List of keys
* @param values List of values
* @return Map of combined lists
* @throws IllegalArgumentException When either List is null or the sizes do not equal
*/
public static Map createMap(List keys, List values) {
if (keys == null || values == null || keys.size() != values.size()) {
throw new IllegalArgumentException("Keys and Values cannot be null and must be the same size");
}
Map newMap = new HashMap();
for (int i = 0; i < keys.size(); i++) {
newMap.put(keys.get(i), values.get(i));
}
return newMap;
}

/** Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string. */
public static String cleanUpPathPrefix(String prefix) {
if (prefix == null || prefix.length() == 0) return "";

StringBuffer cppBuff = new StringBuffer(prefix.replace('\\', '/'));

if (cppBuff.charAt(0) != '/') {
cppBuff.insert(0, '/');
}
if (cppBuff.charAt(cppBuff.length() - 1) == '/') {
cppBuff.deleteCharAt(cppBuff.length() - 1);
}
return cppBuff.toString();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值