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&lt;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>&gt; 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, ?&gt; 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>&gt; 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) &gt;= 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 &gt; 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值