Java 自定义方法 - BASE:修订间差异
跳到导航
跳到搜索
无编辑摘要 |
无编辑摘要 |
||
第9行: | 第9行: | ||
|1 | |1 | ||
|isnull | |isnull | ||
| | |(null, "", [], {}) = true | ||
| | | | ||
|- | |- | ||
|2 | |2 | ||
|log, logerror | |log, logerror, logwarn | ||
| | |log | ||
| | | | ||
|- | |- | ||
|3 | |3 | ||
|dt | |dt | ||
| | |date, time | ||
| | | | ||
|- | |- | ||
|4 | |4 | ||
|hash, md5 | |hash, md5 | ||
|sha, sha-256, md5 | |sha, sha-256, md5, etc hash | ||
| | | | ||
|- | |- | ||
第34行: | 第34行: | ||
|6 | |6 | ||
|des, udes | |des, udes | ||
|DES | |DES crypt | ||
| | | | ||
|- | |- | ||
第49行: | 第49行: | ||
|9 | |9 | ||
|reverse | |reverse | ||
| | |reverse string, abc -> cba | ||
| | | | ||
|- | |- | ||
|10 | |10 | ||
| | |trim | ||
|remove substring | |remove substring, left & right | ||
| | |||
|- | |||
|11 | |||
|at | |||
|CAS, (1, -1, n) | |||
| | | | ||
|} | |} | ||
<small><small>package com.udf | <small><small> package com.udf; | ||
/* | /* | ||
------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ||
Name : Udf.base.BASE | Name : Udf.base.BASE | ||
Purpose : string crypt | Purpose : string crypt, hash, datetime, isnull, trim, etc... | ||
Author : Adam | Author : Adam | ||
Revisions: | Revisions: | ||
Ver Date Author Description | Ver Date Author Description | ||
--------- ---------- --------------- ------------------------------------ | --------- ---------- --------------- ------------------------------------ | ||
1.0 2024/ | 1.0 2024/02/20 Adam | ||
1.1 2024/ | 1.1 2024/02/27 Adam merge STR | ||
1.2 2024/ | 1.2 2024/02/29 Adam remove commons-codec, replace util | ||
1.21 2024/ | 1.21 2024/03/20 Adam Add log, reverse2 | ||
1.22 2024/ | 1.22 2024/03/28 Adam Add isnull, log4j --> slf4j | ||
1.23 2024/04/09 Adam Add at(Atomicinteger) | |||
format: | format: | ||
object : | object : | ||
property: | property: VERSION, json, UDEFLOGOFF, LOOPMAX | ||
method : isnull, log, dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse | method : at, isnull, log, dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse | ||
<nowiki><!-- Json --></nowiki> | <nowiki><!--Json--></nowiki> | ||
<dependency> | <dependency> | ||
<groupId>com.google.code.gson</groupId> | <groupId>com.google.code.gson</groupId> | ||
第83行: | 第88行: | ||
<version>2.8.9</version> | <version>2.8.9</version> | ||
</dependency> | </dependency> | ||
<nowiki><!- | <nowiki><!--log4j--></nowiki> | ||
<dependency> | <dependency> | ||
<groupId>log4j</groupId> | <groupId>log4j</groupId> | ||
第89行: | 第94行: | ||
<version>1.2.17</version> | <version>1.2.17</version> | ||
</dependency> | </dependency> | ||
<nowiki><!--slf4j--></nowiki> | |||
<dependency> | <dependency> | ||
<groupId>org.slf4j</groupId> | <groupId>org.slf4j</groupId> | ||
第94行: | 第100行: | ||
<version>2.0.9</version> | <version>2.0.9</version> | ||
</dependency> | </dependency> | ||
------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ||
*/ | */ | ||
import com.google.gson.Gson; | import com.google.gson.Gson; | ||
import org.apache.log4j.PropertyConfigurator; | import org.apache.log4j.PropertyConfigurator; | ||
第117行: | 第118行: | ||
import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||
import java.util.*; | import java.util.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | |||
public class BASE { | public class BASE { | ||
public static final String VERSION = "v1. | // Property | ||
public static final String VERSION = "v1.23"; | |||
public static boolean UDEFLOGOFF = false; | |||
public static String CHARSET = "UTF-8"; | |||
public static int LOOPMAX = 1024; | |||
public static Gson json = new Gson(); | |||
// Private | |||
private static final AtomicInteger _UDEFAT = new AtomicInteger(0); | |||
static { | static { | ||
PropertyConfigurator.configure("config/log4j.properties"); | PropertyConfigurator.configure("config/log4j.properties"); | ||
} | } | ||
public static | // Method | ||
public static | public static int at() { | ||
public static | return _UDEFAT.get(); | ||
if (obj1 == null) return | } | ||
public static int at(int flag) { | |||
switch(flag) { | |||
case 1: | |||
return _UDEFAT.getAndIncrement(); | |||
case -1: | |||
return _UDEFAT.getAndDecrement(); | |||
default : | |||
_UDEFAT.set(flag); | |||
return flag; | |||
} | |||
} | |||
// Default behavior for unknown types (e.g., return "null") | |||
public static String type(Object obj1) { | |||
if (obj1 == null) return "null"; | |||
if (obj1 instanceof String) | if (obj1 instanceof String) return "String"; | ||
else if (obj1 instanceof List) | else if (obj1 instanceof List) return "List"; | ||
else if (obj1 instanceof Map) | else if (obj1 instanceof Map) return "Map"; | ||
else if (obj1 instanceof Properties) | else if (obj1 instanceof Properties) return "Properties"; | ||
else | else return "null"; | ||
} | } | ||
// Default behavior for unknown types (e.g., return default1[false]) | |||
public static boolean isnull(Object obj1) { | public static boolean isnull(Object obj1) { | ||
return isnull(obj1, false); | return isnull(obj1, false); | ||
} | |||
public static boolean isnull(Object obj1, boolean default1) { | |||
if (obj1 == null) return true; | |||
switch (type(obj1)) { | |||
case "String": | |||
return ((String)obj1).isEmpty(); | |||
case "List": | |||
return ((List)obj1).isEmpty(); | |||
case "Map": | |||
return ((Map)obj1).isEmpty(); | |||
case "Properties": | |||
return ((Properties)obj1).isEmpty(); | |||
default: | |||
return default1; | |||
} | |||
} | } | ||
public static void log(Object log1) { | public static void log(Object log1) { | ||
第165行: | 第204行: | ||
public static String dt(String p, int len) { | public static String dt(String p, int len) { | ||
SimpleDateFormat df = new SimpleDateFormat(); | SimpleDateFormat df = new SimpleDateFormat(); | ||
if (p | if (isnull(p)) | ||
df.applyPattern("yyyyMMddHHmmssSSS"); | df.applyPattern("yyyyMMddHHmmssSSS"); | ||
else | else | ||
df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"+"."+"SSS"); | df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"+"."+"SSS"); | ||
Date | Date date1 = new Date(); | ||
p = df.format( | p = df.format(date1); | ||
len = (len<1||len>p.length())?p.length():len; | len = (len<1 || len>p.length()) ? p.length() : len; | ||
return p.substring(0, len); | return p.substring(0, len); | ||
} | } | ||
第186行: | 第225行: | ||
} | } | ||
public static String hash(String s1, String type) { | public static String hash(String s1, String type) { | ||
if ((s1 | if (isnull(s1)) return ""; | ||
if (isnull(type)) type = "md5"; | |||
if ((type | |||
String hash1 = ""; | String hash1 = ""; | ||
try { | try { | ||
第206行: | 第242行: | ||
} | } | ||
public static String base64(String s1) { | public static String base64(String s1) { | ||
if ((s1 | if (isnull(s1)) return ""; | ||
try { | try { | ||
String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes( | String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes(CHARSET)); | ||
return b64s1; | return b64s1; | ||
} catch (Exception e){ | } catch (Exception e){ | ||
第218行: | 第253行: | ||
} | } | ||
public static String ubase64(String s1) { | public static String ubase64(String s1) { | ||
if ((s1 | if (isnull(s1)) return ""; | ||
try { | try { | ||
byte[] b1 = Base64.getDecoder().decode(s1); | byte[] b1 = Base64.getDecoder().decode(s1); | ||
return new String(b1, | return new String(b1, CHARSET); | ||
} catch (Exception e){ | } catch (Exception e){ | ||
logerror(e); | logerror(e); | ||
return | return ""; | ||
} | } | ||
} | } | ||
public static String des(String s1, String k1) { | public static String des(String s1, String k1) { | ||
if (s1 | if (isnull(s1) || isnull(k1)) { | ||
logerror("The parameter is NULL."); | |||
return | return ""; | ||
} | } | ||
while (k1.length() < 8){ | while (k1.length() < 8){ | ||
logerror("The parameter is short."); | |||
k1 += "0"; | k1 += "0"; | ||
} | } | ||
String DES_ECB = "DES/ECB/PKCS5Padding"; | String DES_ECB = "DES/ECB/PKCS5Padding"; | ||
try { | try { | ||
// get key | // get key | ||
DESKeySpec dk1 = new DESKeySpec(k1.getBytes( | DESKeySpec dk1 = new DESKeySpec(k1.getBytes(CHARSET)); | ||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); | ||
Key secretKey = keyFactory.generateSecret(dk1); | Key secretKey = keyFactory.generateSecret(dk1); | ||
第250行: | 第283行: | ||
Cipher cipher = Cipher.getInstance(DES_ECB); | Cipher cipher = Cipher.getInstance(DES_ECB); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); | cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); | ||
byte[] b1 = cipher.doFinal(s1.getBytes( | byte[] b1 = cipher.doFinal(s1.getBytes(CHARSET)); | ||
// JDK1.7 及以下可以使用 BASE64Encoder | // JDK1.7 及以下可以使用 BASE64Encoder | ||
return Base64.getEncoder().encodeToString(b1); | return Base64.getEncoder().encodeToString(b1); | ||
} catch (Exception e) { | } catch (Exception e) { | ||
logerror(e); | logerror(e); | ||
return | return ""; | ||
} | } | ||
} | } | ||
public static String udes(String s1, String k1) { | public static String udes(String s1, String k1) { | ||
if (s1 | if (isnull(s1) || isnull(k1)) { | ||
logerror("The parameter is NULL."); | |||
return | return ""; | ||
} | } | ||
while (k1.length() < 8) { | while (k1.length() < 8) { | ||
logerror("The parameter is short."); | |||
k1 += "0"; | k1 += "0"; | ||
} | } | ||
第270行: | 第303行: | ||
try { | try { | ||
String DES_ECB = "DES/ECB/PKCS5Padding"; | String DES_ECB = "DES/ECB/PKCS5Padding"; | ||
// get key | // get key | ||
DESKeySpec dk1 = new DESKeySpec(k1.getBytes( | DESKeySpec dk1 = new DESKeySpec(k1.getBytes(CHARSET)); | ||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); | ||
Key secretKey = keyFactory.generateSecret(dk1); | Key secretKey = keyFactory.generateSecret(dk1); | ||
第278行: | 第310行: | ||
Cipher cipher = Cipher.getInstance(DES_ECB); | Cipher cipher = Cipher.getInstance(DES_ECB); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); | cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); | ||
return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), | return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), CHARSET); | ||
} catch (Exception e) { | } catch (Exception e) { | ||
logerror(e); | logerror(e); | ||
return | return ""; | ||
} | } | ||
} | } | ||
第326行: | 第358行: | ||
// reverse a string, like: abc --> cba | // reverse a string, like: abc --> cba | ||
public static String reverse(String s1) { | public static String reverse(String s1) { | ||
if ((s1 | if (isnull(s1)) return ""; | ||
char[] cStr1 = s1.toCharArray(); | char[] cStr1 = s1.toCharArray(); | ||
int len1 = cStr1.length; | int len1 = cStr1.length; | ||
第336行: | 第367行: | ||
cStr1[len1 - 1 - i] = c1; | cStr1[len1 - 1 - i] = c1; | ||
} | } | ||
String rStr1 = new String(cStr1); | //String rStr1 = new String(cStr1); | ||
return rStr1; | //return rStr1; | ||
return String.valueOf(cStr1); | |||
} | } | ||
public static String reverse2(String s1) { | public static String reverse2(String s1) { | ||
if ((s1 | if (isnull(s1)) return ""; | ||
StringBuffer sf1 = new StringBuffer(128); | StringBuffer sf1 = new StringBuffer(128); | ||
sf1.append(s1); | sf1.append(s1); | ||
第348行: | 第379行: | ||
} | } | ||
// left remove | // left remove all substring | ||
public static String ltrim(String | public static String ltrim(String src, String rep) { | ||
return ltrim(src, rep, LOOPMAX); | |||
} | } | ||
// left remove i substrings | // left remove i substrings | ||
public static String ltrim(String | public static String ltrim(String src, String rep, int i) { | ||
if (i <= 0 || isnull(src) || isnull(rep)) return src; | |||
if (i | |||
for (int j=0; j<i; j++) { | for (int j=0; j<i; j++) { | ||
if (src.indexOf(rep) == 0) { | |||
src = src.substring(rep.length()); | |||
} else { | |||
break; | break; | ||
} | |||
} | } | ||
return | return src; | ||
} | } | ||
// right remove all substring | |||
// right remove | public static String rtrim(String src, String rep) { | ||
public static String rtrim(String | return rtrim(src, rep, LOOPMAX); | ||
return rtrim( | |||
} | } | ||
// right remove i substrings | // right remove i substrings | ||
public static String rtrim(String | public static String rtrim(String src, String rep, int i) { | ||
String | if (i <= 0 || isnull(src) || isnull(rep)) return src; | ||
String rsrc, rrep; | |||
rsrc = reverse(src); | |||
rrep = reverse(rep); | |||
return reverse(ltrim(rsrc, rrep, i)); | |||
} | } | ||
// left & right remove | // left & right remove all substring | ||
public static String trim(String | public static String trim(String src, String rep) { | ||
return rtrim(ltrim( | return rtrim(ltrim(src, rep), rep); | ||
} | } | ||
// left & right remove i substring | // left & right remove i substring | ||
public static String trim(String | public static String trim(String src, String rep, int i) { | ||
return rtrim(ltrim( | return rtrim(ltrim(src, rep, i), rep, i); | ||
} | } | ||
}</small></small> | }</small></small> |
2024年4月19日 (五) 20:29的版本
Java 自定义函数 - BASE
No | Method | Explain | Example |
---|---|---|---|
1 | isnull | (null, "", [], {}) = true | |
2 | log, logerror, logwarn | log | |
3 | dt | date, time | |
4 | hash, md5 | sha, sha-256, md5, etc hash | |
5 | base64, ubase64 | BASE64 | |
6 | des, udes | DES crypt | |
7 | str2map | String to MAP | |
8 | list2map | List to MAP | |
9 | reverse | reverse string, abc -> cba | |
10 | trim | remove substring, left & right | |
11 | at | CAS, (1, -1, n) |
package com.udf; /* ------------------------------------------------------------------------------ Name : Udf.base.BASE Purpose : string crypt, hash, datetime, isnull, trim, etc... Author : Adam Revisions: Ver Date Author Description --------- ---------- --------------- ------------------------------------ 1.0 2024/02/20 Adam 1.1 2024/02/27 Adam merge STR 1.2 2024/02/29 Adam remove commons-codec, replace util 1.21 2024/03/20 Adam Add log, reverse2 1.22 2024/03/28 Adam Add isnull, log4j --> slf4j 1.23 2024/04/09 Adam Add at(Atomicinteger) format: object : property: VERSION, json, UDEFLOGOFF, LOOPMAX method : at, isnull, log, dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse <!--Json--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version> </dependency> ------------------------------------------------------------------------------ */ import com.google.gson.Gson; import org.apache.log4j.PropertyConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.math.BigInteger; import java.security.Key; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class BASE { // Property public static final String VERSION = "v1.23"; public static boolean UDEFLOGOFF = false; public static String CHARSET = "UTF-8"; public static int LOOPMAX = 1024; public static Gson json = new Gson(); // Private private static final AtomicInteger _UDEFAT = new AtomicInteger(0); static { PropertyConfigurator.configure("config/log4j.properties"); } // Method public static int at() { return _UDEFAT.get(); } public static int at(int flag) { switch(flag) { case 1: return _UDEFAT.getAndIncrement(); case -1: return _UDEFAT.getAndDecrement(); default : _UDEFAT.set(flag); return flag; } } // Default behavior for unknown types (e.g., return "null") public static String type(Object obj1) { if (obj1 == null) return "null"; if (obj1 instanceof String) return "String"; else if (obj1 instanceof List) return "List"; else if (obj1 instanceof Map) return "Map"; else if (obj1 instanceof Properties) return "Properties"; else return "null"; } // Default behavior for unknown types (e.g., return default1[false]) public static boolean isnull(Object obj1) { return isnull(obj1, false); } public static boolean isnull(Object obj1, boolean default1) { if (obj1 == null) return true; switch (type(obj1)) { case "String": return ((String)obj1).isEmpty(); case "List": return ((List)obj1).isEmpty(); case "Map": return ((Map)obj1).isEmpty(); case "Properties": return ((Properties)obj1).isEmpty(); default: return default1; } } public static void log(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.info(""); else logger.info(log1.toString()); } public static void logwarn(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.warn(""); else logger.warn(log1.toString()); } public static void logerror(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.error(""); else logger.error(log1.toString()); } // get sysdate, "-:" --> 2024-02-22 00:00:00.000 public static String dt(String p, int len) { SimpleDateFormat df = new SimpleDateFormat(); if (isnull(p)) df.applyPattern("yyyyMMddHHmmssSSS"); else df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"+"."+"SSS"); Date date1 = new Date(); p = df.format(date1); len = (len<1 || len>p.length()) ? p.length() : len; return p.substring(0, len); } public static String dt(String p) { return dt(p, 0); } public static String dt(int len) { return dt(null, len); } // get sysdate, 20240222000000 public static String dt() { return dt(14); } public static String hash(String s1, String type) { if (isnull(s1)) return ""; if (isnull(type)) type = "md5"; String hash1 = ""; try { MessageDigest md = MessageDigest.getInstance(type); md.update(s1.getBytes()); hash1 = new BigInteger(1, md.digest()).toString(16); } catch (NoSuchAlgorithmException e) { logerror(e); } return hash1; } public static String md5(String s1) { return hash(s1, "md5"); } public static String base64(String s1) { if (isnull(s1)) return ""; try { String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes(CHARSET)); return b64s1; } catch (Exception e){ logerror(e); return null; } } public static String ubase64(String s1) { if (isnull(s1)) return ""; try { byte[] b1 = Base64.getDecoder().decode(s1); return new String(b1, CHARSET); } catch (Exception e){ logerror(e); return ""; } } public static String des(String s1, String k1) { if (isnull(s1) || isnull(k1)) { logerror("The parameter is NULL."); return ""; } while (k1.length() < 8){ logerror("The parameter is short."); k1 += "0"; } String DES_ECB = "DES/ECB/PKCS5Padding"; try { // get key DESKeySpec dk1 = new DESKeySpec(k1.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dk1); Cipher cipher = Cipher.getInstance(DES_ECB); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); byte[] b1 = cipher.doFinal(s1.getBytes(CHARSET)); // JDK1.7 及以下可以使用 BASE64Encoder return Base64.getEncoder().encodeToString(b1); } catch (Exception e) { logerror(e); return ""; } } public static String udes(String s1, String k1) { if (isnull(s1) || isnull(k1)) { logerror("The parameter is NULL."); return ""; } while (k1.length() < 8) { logerror("The parameter is short."); k1 += "0"; } try { String DES_ECB = "DES/ECB/PKCS5Padding"; // get key DESKeySpec dk1 = new DESKeySpec(k1.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dk1); Cipher cipher = Cipher.getInstance(DES_ECB); cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), CHARSET); } catch (Exception e) { logerror(e); return ""; } } public static String des(String s1) { return des(s1, "DeiPd8ltuAE3"); } public static String udes(String s1) { return udes(s1, "DeiPd8ltuAE3"); } // String(a=1 b=c...) --> map({a=1, b=c, ...}) public static HashMap<String, String> str2map (String[] s1) { HashMap<String, String> para = new HashMap<>(); String s11, key, val; String[] s2; for (int i=0; i<s1.length; i++) { try { s11 = s1[i]; s2 = s11.split("="); key = s2[0]; val = ltrim(s11, key+"="); if (null == key || "".equals(key)) { key = "" + i; } else if (key.equals(s11)) { val = key; key = "" + i; } para.put(key, val); } catch (Exception e) { para.put("" + i, s1[i].substring(1)); } } return para; } // list1, list2 --> map(list1, list2) // if list2 < list1, set to null. public static HashMap<String, String> list2map(ArrayList<String> ky1, ArrayList<String> val1) { HashMap<String, String> map1 = new HashMap<>(); for (int i=0; i<ky1.size(); i++) { map1.put(ky1.get(i), i>=val1.size()?null:val1.get(i)); } return map1; } // reverse a string, like: abc --> cba public static String reverse(String s1) { if (isnull(s1)) return ""; char[] cStr1 = s1.toCharArray(); int len1 = cStr1.length; for (int i = 0; i < len1 / 2; i++) { char c1 = cStr1[i]; cStr1[i] = cStr1[len1 - 1 - i]; cStr1[len1 - 1 - i] = c1; } //String rStr1 = new String(cStr1); //return rStr1; return String.valueOf(cStr1); } public static String reverse2(String s1) { if (isnull(s1)) return ""; StringBuffer sf1 = new StringBuffer(128); sf1.append(s1); return sf1.reverse().toString(); } // left remove all substring public static String ltrim(String src, String rep) { return ltrim(src, rep, LOOPMAX); } // left remove i substrings public static String ltrim(String src, String rep, int i) { if (i <= 0 || isnull(src) || isnull(rep)) return src; for (int j=0; j<i; j++) { if (src.indexOf(rep) == 0) { src = src.substring(rep.length()); } else { break; } } return src; } // right remove all substring public static String rtrim(String src, String rep) { return rtrim(src, rep, LOOPMAX); } // right remove i substrings public static String rtrim(String src, String rep, int i) { if (i <= 0 || isnull(src) || isnull(rep)) return src; String rsrc, rrep; rsrc = reverse(src); rrep = reverse(rep); return reverse(ltrim(rsrc, rrep, i)); } // left & right remove all substring public static String trim(String src, String rep) { return rtrim(ltrim(src, rep), rep); } // left & right remove i substring public static String trim(String src, String rep, int i) { return rtrim(ltrim(src, rep, i), rep, i); } }