20130222!

package com.niu.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;

import com.niu.common.Const;

public class StringUtils {
private static StringUtils instance = new StringUtils();

private StringUtils() {
}

public static StringUtils getInstance() {
return instance;
}

/**
* @param languageType
* @return parseLanguage
*/
public ResourceBundle parseLanguage(String languageType) {
ResourceBundle rb = null;
if ((Const.LANGUAGE_TYPE_CHINA).equals(languageType)) {
rb = ResourceBundle.getBundle(Const.LANGUAGE_FILE_PATH,
Locale.CHINA);
} else if ((Const.LANGUAGE_TYPE_US).equals(languageType)) {

rb = ResourceBundle.getBundle(Const.LANGUAGE_FILE_PATH, Locale.US);
} else {
throw new IllegalArgumentException(languageType + "illegal!");
}

return rb;
}

/**
* @param languageType
* @param propertyKey
* @return getPropertyValue
*/
public String getPropertyValue(String languageType, String propertyKey) {
return this.parseLanguage(languageType).getString(propertyKey);
}

/**
* @param i
* @param str
* @return
*/
public String getStrByByte(int i, String str) {
String result = "";
for (;;) {
if (str.isEmpty()
|| (result + str.substring(0, 1)).getBytes().length > i) {
break;
}
result = result + str.substring(0, 1);
str = str.substring(1);
}

return result;
}

/**
* @param str
* @return
*/
public Map<character integer> countCharCnt(String str) {
Map<character integer> map = new HashMap<character integer>();

char[] chrs = str.toCharArray();

for (char ch : chrs) {
if (map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, ++count);
} else {
map.put(ch, 1);
}
}
return map;
}

/**
* @param src
* @param index
* @return
*/
public String[] getLoopArray(String[] src, int index) {
String[] dest = new String[src.length];
;

for (int i = 0; i = src.length) {
dest[i + index - src.length] = src[i];
} else {
dest[i + index] = src[i];
}
}
return dest;
}


public Set<string> getChar2SubStred(String src) {
Set<string> set = new HashSet<string>();

//子串长度
for(int i=1;i<src.length for index="0;index<src.length()-index;index++)" set.add return set o printobject public void print system.out.println obj : system.out.print map integer> map) {
// for (Character o : map.keySet()) {
// System.out.println("key: " + o + "    value:   " + map.get(o));
// }

Set<entry integer>> entrySet = map.entrySet();

for (Entry<character integer> entry : entrySet) {

System.out.println("key: " + entry.getKey() + "    value:   "
+ entry.getValue());
}
}

public void print(Collection<object> c) {
for (Object o : c) {
System.out.println(o);
}
}

public static void main(String[] args) {
StringUtils s = StringUtils.getInstance();

s.print(s
.getPropertyValue(Const.LANGUAGE_TYPE_US, Const.LOGIN_USERNAME));
s.print(s.getPropertyValue(Const.LANGUAGE_TYPE_CHINA,
Const.LOGIN_USERNAME));

s.print(s.getStrByByte(7, "abc中国"));

s.print(s.countCharCnt("aabbcddd"));

s.print(s.getLoopArray(new String[] { "a", "b", "c" }, 2));

s.print(s.getChar2SubStred("abab"));
}
}



----------------------------------------------------------------------------------------------map 统计字符/单词个数-----------------------------------------------------
/**
* map char count
*
* @param str
* @return
*/
public Map<character integer> getCharCnt(String str) {
char[] chrs = str.toCharArray();
Map<character integer> map = new HashMap<character integer>();

for (char ch : chrs) {
if (map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, ++count);
} else {
map.put(ch, 1);
}
}
return map;
}

/**
* map word cnt
*
* @param strs
* @return
*/
public Map<string integer> getWordCnt(String[] strs) {
Map<string integer> map = new HashMap<string integer>();
for (String str : strs) {
if (map.containsKey(str)) {
int count = map.get(str);
map.put(str, ++count);
} else {
map.put(str, 1);
}
}

return map;
}

/**
* map keySet
*
* @param map
*/
public void printMapkeySet(Map, ?> map) {
for (Object o : map.keySet()) {
System.out.println("key: " + o + "   " + "value: " + map.get(o));
}
}

/**
* map entrySet
*
* @param map
*/
public void printMapEntrySet(Map<character integer> map) {
Set<entry integer>> entrySet = map.entrySet();
for (Entry<character integer> entry : entrySet) {
System.out.println("key: " + entry.getKey() + "  " + "value:  "
+ entry.getValue());
}

}

public void print(Object o) {
System.out.println(o);

}

private static StringUtil instance = new StringUtil();

private StringUtil() {
}

public static StringUtil getInstance() {
return instance;
}

public void isvalidStr(String str) {

}

/**
* about word.....replace
*
* @param str
* @return
*/
public String getSplitWord(String str) {
if (str == null) {
throw new NullPointerException();
}
if (str.isEmpty()) {
return "";
}
if (!str.matches("^[a-zA-Z,。 ]+$")) {
throw new IllegalArgumentException();
}
StringBuffer msg = new StringBuffer("");
String[] strs = str.replaceAll(",", " ").replaceAll("。", " ")
.split("\\W");
for (String s : strs) {
msg = msg.append(s);
}
return msg.toString();
}

public String[] getSplitWordArray(String str) {
if (str == null) {
throw new NullPointerException();
}
if (str.isEmpty()) {
return new String[] { "" };
}
if (!str.matches("^[a-zA-Z,。 ]{1,}")) {
throw new IllegalArgumentException();
}

return str.replaceAll(",", " ").replaceAll("。", " ").split("\\W");
}
-------------------------------------------------------------------------------string,stringBuffer,StringBuilder---------------------------------------------------------------------------

/**
* string,stringBuffer,stringBuilder性能比较
* 数据较小时用StringBuffer
* 数据庞大时用StringBuilder
*
* @return
*/
public long getTimeString() {
String str = "";
long start = System.currentTimeMillis();
for (int i = 0; i = 0;
}

/**
* is letter
*
* @param ch
* @return
*/
public boolean isLetter(char ch) {
return "abcdefjhijklmnopqrstuvwsyz".indexOf(ch) >= 0;
}

----------------------------------------------------------------------------DBUtils------------------------------------------------------------------------------------
package com.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class DBUtil {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;

private static final String DB_FILE_PATH = "demo_db";
private static String drive;
private static String url;
private static String username;
private static String password;

static {
ResourceBundle resb = ResourceBundle.getBundle(DB_FILE_PATH);
drive = resb.getString("DRIVERS").trim();
url = resb.getString("URL").trim();
username = resb.getString("USER").trim();
password = resb.getString("PASSWORD").trim();
}

private static DBUtil instance = new DBUtil();

private DBUtil() {
}

public static DBUtil getInstance() {
return instance;
}

public Connection getConnection() {
try {
Class.forName(drive);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}

public void insert(String sql) {
try {
conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, "");
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(ps);
this.close(conn);
}
}

public void update(String sql) {
try {
conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, "");
rs = ps.executeQuery();

while (rs.next()) {
rs.getString("");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs);
this.close(ps);
this.close(conn);
}
}

public void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public void close(Statement ps) {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
----------------------------------------字节判断-----------------------
*  1:a
    *  3: abc
    *  5: abc中
    *  6:abc中
    *  7:abc中国
    * @version 
    */
    public String getStringByBytes(int num,String sourceStr){
    String destStr = "";
   
    for(;;){
   
    if((destStr+sourceStr.substring(0,1)).getBytes().length > num){
    break;
    }
    destStr = destStr+sourceStr.substring(0,1);
    sourceStr = sourceStr.substring(1);
    }
   
    return destStr;
    } </character></entry></character></string></string></string></character></character></character></object></character></entry></src.length></string></string></string></character></character></character>

你可能感兴趣的:(20130222!)