1. package com.tw.str.util;  
  2.  
  3.  
  4. import java.io.File;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import java.util.Properties;  
  8.  
  9.  
  10. /**  
  11. * 字符串工具箱  
  12.  * @author tw 2009-07-16  
  13. */ 
  14. public final class StringToolkit {  
  15.   /**  
  16.   * 将一个字符串的首字母改为大写或者小写  
  17.   *  
  18.   * @param srcString 源字符串  
  19.   * @param flag     大小写标识,ture小写,false大些  
  20.   * @return 改写后的新字符串  
  21.   */ 
  22.   public static String toLowerCaseInitial(String srcString, boolean flag) {  
  23.     StringBuilder sb = new StringBuilder();  
  24.     if (flag) {  
  25.         sb.append(Character.toLowerCase(srcString.charAt(0)));  
  26.     } else {  
  27.         sb.append(Character.toUpperCase(srcString.charAt(0)));  
  28.     }  
  29.     sb.append(srcString.substring(1));  
  30.     return sb.toString();  
  31.   }  
  32.  
  33.   /**  
  34.   * 将一个字符串按照句点(.)分隔,返回最后一段  
  35.   *  
  36.   * @param clazzName 源字符串  
  37.   * @return 句点(.)分隔后的最后一段字符串  
  38.   */ 
  39.   public static String getLastName(String clazzName) {  
  40.     String[] ls = clazzName.split("\\.");  
  41.     return ls[ls.length - 1];  
  42.   }  
  43.  
  44.   /**  
  45.   * 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的"/"符号。  
  46.   *  
  47.   * @param path 文件路径  
  48.   * @return 格式化后的文件路径  
  49.   */ 
  50.   public static String formatPath(String path) {  
  51.     String reg0 = "\\\\+";  
  52.     String reg = "\\\\+|/+";  
  53.     String temp = path.trim().replaceAll(reg0, "/");  
  54.     temp = temp.replaceAll(reg, "/");  
  55.     if (temp.endsWith("/")) {  
  56.         temp = temp.substring(0, temp.length() - 1);  
  57.     }  
  58.     if (System.getProperty("file.separator").equals("\\")) {  
  59.       temp= temp.replace('/','\\');  
  60.     }  
  61.     return temp;  
  62.   }  
  63.  
  64.   /**  
  65.   * 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的"/"符号(适用于FTP远程文件路径或者Web资源的相对路径)。  
  66.   *  
  67.   * @param path 文件路径  
  68.   * @return 格式化后的文件路径  
  69.   */ 
  70.   public static String formatPath4Ftp(String path) {  
  71.     String reg0 = "\\\\+";  
  72.     String reg = "\\\\+|/+";  
  73.     String temp = path.trim().replaceAll(reg0, "/");  
  74.     temp = temp.replaceAll(reg, "/");  
  75.     if (temp.endsWith("/")) {  
  76.         temp = temp.substring(0, temp.length() - 1);  
  77.     }  
  78.     return temp;  
  79.   }  
  80.  
  81.   public static void main(String[] args) {  
  82.     System.out.println(System.getProperty("file.separator"));  
  83.     Properties p = System.getProperties();  
  84.     System.out.println(formatPath("C:///\\xxxx\\\\\\\\\\///\\\\R5555555.txt"));  
  85.  
  86. //     List<String> result = series2List("asdf | sdf|siii|sapp|aaat| ", "\\|");  
  87. //     System.out.println(result.size());  
  88. //     for (String s : result) {  
  89. //         System.out.println(s);  
  90. //     }  
  91.   }  
  92.  
  93.   /**  
  94.   * 获取文件父路径  
  95.   *  
  96.   * @param path 文件路径  
  97.   * @return 文件父路径  
  98.   */ 
  99.   public static String getParentPath(String path) {  
  100.     return new File(path).getParent();  
  101.   }  
  102.  
  103.   /**  
  104.   * 获取相对路径  
  105.   *  
  106.   * @param fullPath 全路径  
  107.   * @param rootPath 根路径  
  108.   * @return 相对根路径的相对路径  
  109.   */ 
  110.   public static String getRelativeRootPath(String fullPath, String rootPath) {  
  111.     String relativeRootPath = null;  
  112.     String _fullPath = formatPath(fullPath);  
  113.     String _rootPath = formatPath(rootPath);  
  114.  
  115.     if (_fullPath.startsWith(_rootPath)) {  
  116.         relativeRootPath = fullPath.substring(_rootPath.length());  
  117.     } else {  
  118.         throw new RuntimeException("要处理的两个字符串没有包含关系,处理失败!");  
  119.     }  
  120.     if (relativeRootPath == nullreturn null;  
  121.     else 
  122.         return formatPath(relativeRootPath);  
  123.   }  
  124.  
  125.   /**  
  126.   * 获取当前系统换行符  
  127.   *  
  128.   * @return 系统换行符  
  129.   */ 
  130.   public static String getSystemLineSeparator() {  
  131.     return System.getProperty("line.separator");  
  132.   }  
  133.  
  134.   /**  
  135.   * 将用“|”分隔的字符串转换为字符串集合列表,剔除分隔后各个字符串前后的空格  
  136.   *  
  137.   * @param series 将用“|”分隔的字符串  
  138.   * @return 字符串集合列表  
  139.   */ 
  140.   public static List<String> series2List(String series) {  
  141.     return series2List(series, "\\|");  
  142.   }  
  143.  
  144.   /**  
  145.   * 将用正则表达式regex分隔的字符串转换为字符串集合列表,剔除分隔后各个字符串前后的空格  
  146.   *  
  147.   * @param series 用正则表达式分隔的字符串  
  148.   * @param regex 分隔串联串的正则表达式  
  149.   * @return 字符串集合列表  
  150.   */ 
  151.   private static List<String> series2List(String series, String regex) {  
  152.     List<String> result = new ArrayList<String>();  
  153.     if (series != null && regex != null) {  
  154.         for (String s : series.split(regex)) {  
  155.           if (s.trim() != null && !s.trim().equals("")) result.add(s.trim());  
  156.         }  
  157.     }  
  158.     return result;  
  159.   }  
  160.  
  161.   /**  
  162.   * @param strList 字符串集合列表  
  163.   * @return 通过“|”串联为一个字符串  
  164.   */ 
  165.   public static String list2series(List<String> strList) {  
  166.     StringBuffer series = new StringBuffer();  
  167.     for (String s : strList) {  
  168.         series.append(s).append("|");  
  169.     }  
  170.     return series.toString();  
  171.   }  
  172.  
  173.   /**  
  174.   * 将字符串的首字母转为小写  
  175.   *  
  176.   * @param resStr 源字符串  
  177.   * @return 首字母转为小写后的字符串  
  178.   */ 
  179.   public static String firstToLowerCase(String resStr) {  
  180.     if (resStr == null) {  
  181.         return null;  
  182.     } else if ("".equals(resStr.trim())) {  
  183.         return "";  
  184.     } else {  
  185.         StringBuffer sb = new StringBuffer();  
  186.         Character c = resStr.charAt(0);  
  187.         if (Character.isLetter(c)) {  
  188.           if (Character.isUpperCase(c))  
  189.             c = Character.toLowerCase(c);  
  190.           sb.append(resStr);  
  191.           sb.setCharAt(0, c);  
  192.           return sb.toString();  
  193.         }  
  194.     }  
  195.     return resStr;  
  196.   }  
  197.  
  198.   /**  
  199.   * 将字符串的首字母转为大写  
  200.   *  
  201.   * @param resStr 源字符串  
  202.   * @return 首字母转为大写后的字符串  
  203.   */ 
  204.   public static String firstToUpperCase(String resStr) {  
  205.     if (resStr == null) {  
  206.         return null;  
  207.     } else if ("".equals(resStr.trim())) {  
  208.         return "";  
  209.     } else {  
  210.         StringBuffer sb = new StringBuffer();  
  211.         Character c = resStr.charAt(0);  
  212.         if (Character.isLetter(c)) {  
  213.           if (Character.isLowerCase(c))  
  214.             c = Character.toUpperCase(c);  
  215.           sb.append(resStr);  
  216.           sb.setCharAt(0, c);  
  217.           return sb.toString();  
  218.         }  
  219.     }  
  220.     return resStr;  
  221.   }  
  222.