String处理工具类--Java自学网

本文提供了一系列字符串处理函数,包括截取、转换字符集、特殊字符替换等,适用于各种编程场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.app.javalearns.util.FileNoUtils;

/**
 * 字符串常用操作
 * 
 * @author ccj
 */
public class StringUtils {

 public static void main(String[] args) {
  System.out.println(StringUtils.getLimitLengthString(
    "中华人民共和国 One World!  One Dream!!!!", 15));

  System.out.println(StringUtils.ToDBC("对于 档 案"));
 }

 public static String isExistChar(String s){
  boolean flag = false;
  String str = "";
  for(String t : FileNoUtils.charList){
   flag = s.indexOf(t) != -1;
   if(flag){
    str = t;
    break;
   }
  }
  return str;
 }
 
 
 //中文字符转换数字字符
 public static String chinese2Number(String s) {
  s = ReplaceAll.replace(s, "一", "1");
  s = ReplaceAll.replace(s, "二", "2");
  s = ReplaceAll.replace(s, "三", "3");
  s = ReplaceAll.replace(s, "四", "4");
  s = ReplaceAll.replace(s, "五", "5");
  s = ReplaceAll.replace(s, "六", "6");
  s = ReplaceAll.replace(s, "七", "7");
  s = ReplaceAll.replace(s, "八", "8");
  s = ReplaceAll.replace(s, "九", "9");
  s = ReplaceAll.replace(s, "零", "0");
  return s;
 }
 
 
 
 // 中文字符转换英文字符
 public static String chinese2English(String s) {
  s = ReplaceAll.replace(s, ",", ",");
  s = ReplaceAll.replace(s, "。", ".");
  s = ReplaceAll.replace(s, "〈", "<");
  s = ReplaceAll.replace(s, "〉", ">");
  s = ReplaceAll.replace(s, "‖", "|");
  s = ReplaceAll.replace(s, "《", "<");
  s = ReplaceAll.replace(s, "》", ">");
  s = ReplaceAll.replace(s, "〔", "[");
  s = ReplaceAll.replace(s, "〕", "]");
  s = ReplaceAll.replace(s, "﹖", "?");
  s = ReplaceAll.replace(s, "?", "?");
  s = ReplaceAll.replace(s, "“", "\"");
  s = ReplaceAll.replace(s, "”", "\"");
  s = ReplaceAll.replace(s, ":", ":");
  s = ReplaceAll.replace(s, "、", ",");
  s = ReplaceAll.replace(s, "(", "(");
  s = ReplaceAll.replace(s, ")", ")");
  s = ReplaceAll.replace(s, "【", "[");
  s = ReplaceAll.replace(s, "】", "]");
  s = ReplaceAll.replace(s, "—", "-");
  s = ReplaceAll.replace(s, "~", "~");
  s = ReplaceAll.replace(s, "!", "!");
  s = ReplaceAll.replace(s, "‵", "'");
  s = ReplaceAll.replace(s, "①", "1");
  s = ReplaceAll.replace(s, "②", "2");
  s = ReplaceAll.replace(s, "③", "3");
  s = ReplaceAll.replace(s, "④", "4");
  s = ReplaceAll.replace(s, "⑤", "5");
  s = ReplaceAll.replace(s, "⑥", "6");
  s = ReplaceAll.replace(s, "⑦", "7");
  s = ReplaceAll.replace(s, "⑧", "8");
  s = ReplaceAll.replace(s, "⑨", "9");
  return s;
 }

 // 半角转全角
 public static String BQchange(String QJstr) {
  String outStr = "";
  String Tstr = "";
  byte[] b = null;

  for (int i = 0; i < QJstr.length(); i++) {
   try {
    Tstr = QJstr.substring(i, i + 1);
    b = Tstr.getBytes("unicode");
   } catch (java.io.UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   if (b[3] != -1) {
    b[2] = (byte) (b[2] - 32);
    b[3] = -1;
    try {
     outStr = outStr + new String(b, "unicode");
    } catch (java.io.UnsupportedEncodingException e) {
     e.printStackTrace();
    }
   } else
    outStr = outStr + Tstr;
  }
  return outStr;
 }

 // 全角转半角
 public static String QBchange(String QJstr) {
  String outStr = "";
  String Tstr = "";
  byte[] b = null;

  for (int i = 0; i < QJstr.length(); i++) {
   try {
    Tstr = QJstr.substring(i, i + 1);
    b = Tstr.getBytes("unicode");
   } catch (java.io.UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   if (b[3] == -1) {
    b[2] = (byte) (b[2] + 32);
    b[3] = 0;
    try {
     outStr = outStr + new String(b, "unicode");
    } catch (java.io.UnsupportedEncodingException e) {
     e.printStackTrace();
    }
   } else
    outStr = outStr + Tstr;
  }
  return outStr;
 }

http://www.javalearns.com/

 /**
  * 字符截取
  * 
  * @param str
  * @param len
  * @return
  */
 public static String getLimitLengthString(String str, int len) {
  try {
   int counterOfDoubleByte = 0;
   byte[] b = str.getBytes("gb2312");
   if (b.length <= len)
    return str;
   for (int i = 0; i < len; i++) {
    if (b[i] < 0)
     counterOfDoubleByte++;
   }
   if (counterOfDoubleByte % 2 == 0)
    return new String(b, 0, len, "gb2312") + "...";
   else
    return new String(b, 0, len - 1, "gb2312") + "...";
  } catch (Exception ex) {
   return "";
  }
 }

 /**
  * 半角转全角
  * 
  * @param input
  *            String.
  * @return 全角字符串.
  */
 public static String ToSBC(String input) {
  char c[] = input.toCharArray();
  for (int i = 0; i < c.length; i++) {
   if (c[i] == ' ') {
    c[i] = '\u3000';
   } else if (c[i] < '\177') {
    c[i] = (char) (c[i] + 65248);

   }
  }
  return new String(c);
 }

 /**
  * 全角转半角
  * 
  * @param input
  *            String.
  * @return 半角字符串
  */
 public static String ToDBC(String input) {

  char c[] = input.toCharArray();
  for (int i = 0; i < c.length; i++) {
   if (c[i] == '[') {
    c[i] = '[';
   }
   if (c[i] == ']') {
    c[i] = ']';
   }
  }
  String returnString = new String(c);

  return returnString;
 }

 /**
  * 特殊字符转为Html
  */
 public static String toHtml(String s) {
  s = ReplaceAll.replace(s, "&", "&amp;");
  s = ReplaceAll.replace(s, "<", "&lt;");
  s = ReplaceAll.replace(s, ">", "&gt;");
  s = ReplaceAll.replace(s, "\t", "    ");
  s = ReplaceAll.replace(s, "\r\n", "\n");
  s = ReplaceAll.replace(s, "\r", "\n");
  s = ReplaceAll.replace(s, "\n", "<br>");
  s = ReplaceAll.replace(s, " ", "&nbsp;");
  s = ReplaceAll.replace(s, "'", "&#39;");
  s = ReplaceAll.replace(s, "\\", "&#92;");
  s = ReplaceAll.replace(s, "\"", "&quot;");
  return s;
 }

 /**
  * 特殊字符转为Html
  */
 public static String toHtml_nbsp(String s) {
  s = ReplaceAll.replace(s, "&", "&amp;");
  s = ReplaceAll.replace(s, "<", "&lt;");
  s = ReplaceAll.replace(s, ">", "&gt;");
  s = ReplaceAll.replace(s, "\t", "    ");
  s = ReplaceAll.replace(s, "\r\n", "\n");
  s = ReplaceAll.replace(s, "\r", "\n");
  s = ReplaceAll.replace(s, "\n", "<br>");
  s = ReplaceAll.replace(s, "'", "&#39;");
  s = ReplaceAll.replace(s, "\\", "&#92;");
  s = ReplaceAll.replace(s, "\"", "&quot;");
  return s;
 }
 
 /**
  * 特殊字符转为Html
  */
 public static String toHtml_nbsp1(String s) {
  s = ReplaceAll.replace(s, "&", "&amp;");
  s = ReplaceAll.replace(s, "<", "&lt;");
  s = ReplaceAll.replace(s, ">", "&gt;");
  s = ReplaceAll.replace(s, "\t", "    ");
  s = ReplaceAll.replace(s, "'", "&#39;");
  s = ReplaceAll.replace(s, "\\", "&#92;");
  s = ReplaceAll.replace(s, "\"", "&quot;");
  return s;
 }

 /**
  * 将html数据,返回textarea可修改的数据库
  * 
  * @param s
  * @return
  */
 public static String unHtml(String s) {
  s = ReplaceAll.replace(s, "<br>", "\n");
  s = ReplaceAll.replace(s, "&nbsp;", " ");
  return s;
 }

 /**
  * 字符分割函数
  * 
  * @param source
  * @param div
  * @return
  */
 public static String[] split(String source, String div) {
  int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
  if (source.compareTo("") != 0) {
   if (source.indexOf(div) != -1) {
    intIdx = source.indexOf(div);
    for (int intCount = 1;; intCount++) {
     if (source.indexOf(div, intIdx + div_length) != -1) {
      intIdx = source.indexOf(div, intIdx + div_length);
      arynum = intCount;
     } else {
      arynum += 2;
      break;
     }
    }
   } else {
    arynum = 1;
   }
  } else {
   arynum = 0;

  }
  intIdx = 0;
  intIdex = 0;
  String[] returnStr = new String[arynum];

  if (source.compareTo("") != 0) {
   if (source.indexOf(div) != -1) {
    intIdx = (int) source.indexOf(div);
    returnStr[0] = (String) source.substring(0, intIdx);
    for (int intCount = 1;; intCount++) {
     if (source.indexOf(div, intIdx + div_length) != -1) {
      intIdex = (int) source
        .indexOf(div, intIdx + div_length);
      returnStr[intCount] = (String) source.substring(intIdx
        + div_length, intIdex);
      intIdx = (int) source.indexOf(div, intIdx + div_length);
     } else {
      returnStr[intCount] = (String) source.substring(intIdx
        + div_length, source.length());
      break;
     }
    }
   } else {
    returnStr[0] = (String) source.substring(0, source.length());
    return returnStr;
   }
  } else {
   return returnStr;
  }
  return returnStr;
 }

 /**
  * 返回不为NULL的String值
  * 
  * @param src
  * @return
  */
 public static String getNotNullString(String src) {
  if (src != null && !"null".equals(src)) {
   return src;
  } else {
   return "";
  }
 }

 /**
  * 返回不为NULL的Integer值
  * 
  * @param i
  * @return
  */
 public static String getNotNullInt(Integer i) {
  if (i != null) {
   return String.valueOf(i.intValue());
  } else {
   return "";
  }
 }


 public static long getNotNULLOfLong(Long i)
 {
  if (i != null)
   return i.longValue();
  else
   return 0L;
 }
 
 /**
  * 返回为0的Integer值
  */
 public static int getNotNULLOfInt(Integer i) {
  if (i != null) {
   return i.intValue();
  } else {
   return 0;
  }
 }

 /**
  * 返回为0的String数值
  */
 public static int getNotNULLOfInt(String i) {
  if (i != null) {
   try {
    return Integer.parseInt(i);
   } catch (Exception e) {
    return 0;
   }
  } else {
   return 0;
  }
 }

 /**
  * 返回不为NULL的Long值
  * 
  * @param l
  * @return
  */
 public static String getNotNullLong(Long l) {
  if (l != null) {
   return String.valueOf(l.longValue());
  } else {
   return "";
  }
 }

 /**
  * 将Long转为long形,如果为NULL,返回0
  * 
  * @param l
  * @return
  */
 public static long getNotNullLong1(Long l) {
  if (l != null) {
   return l.longValue();
  } else {
   return 0;
  }
 }

 /**
  * 将float类型转为string类型
  * 
  * @param f
  * @return
  */
 public static String getNotNullFloat(Float f) {
  if (f != null) {
   float tempFloat = f.floatValue();
   int tempInt = (int) tempFloat;
   if ((float) tempInt == tempFloat) {
    return String.valueOf(tempInt);
   } else {
    return String.valueOf(f.floatValue());
   }
  } else {
   return "";
  }
 }

 /**
  * 将BigDecimal转为string类型
  * 
  * @param bd
  * @return
  */
 public static String getNotNullDecimal(BigDecimal bd) {
  if (bd == null) {
   return "0.00";
  }
  return bd.toString();
 }

 /**
  * 将BigDecimal转为string类型
  * 
  * @param bd
  * @return
  */
 public static String getNotNullDecimalStr(BigDecimal bd) {
  if (bd == null) {
   return "";
  }
  String t = bd.toString();
  if (t.endsWith(".00")) {
   return bd.intValue() + "";
  } else {
   return t;
  }
  /*
   * int temp = bd.intValue(); if(temp==0){ // return ""; return temp+"";
   * }else{ return temp+""; }
   */
 }

 /**http://www.javalearns.com/
  * 将double返回int类
  * 
  * @param d
  * @return
  */
 public static int getDoubleValue(Double d) {
  int v = 0;
  if (d != null) {
   v = (int) d.intValue();
  }
  return v;
 }

 /**
  * 高亮
  * 
  * @param fromStr
  * @param replStr
  * @param split
  * @return
  */
 public static String high_light(String fromStr, String replStr, String split) {
  /*
   * if ((fromStr != null && fromStr.trim().length() > 0) && (replStr !=
   * null && replStr.trim().length() > 0) && (split != null &&
   * split.length() > 0)){ Map map = new HashMap(); for (int i = 0; i <
   * replStr.length(); i++){ if (!split.equals(replStr.charAt(i) + "")){
   * map.put(replStr.charAt(i), replStr.charAt(i)); } } for (Object key :
   * map.keySet()){ if (key != null && key.toString().trim().length() > 0 &&
   * !key.toString().equals(split)){ fromStr =
   * fromStr.replaceAll(key.toString().trim(), "<font color='red'>" +
   * key.toString() + "</font>"); } } } return fromStr;
   */

  if ((fromStr != null && fromStr.trim().length() > 0)
    && (replStr != null && replStr.trim().length() > 0)
    && (split != null && split.length() > 0)) {
   String[] data = replStr.split(split);
   if (data.length > 0) {
    for (String key : data) {
     if (key != null && key.trim().length() > 0) {
      fromStr = fromStr.replaceAll(key.trim(),
        "<font color='red'>" + key + "</font>");
     }
    }
   }
  }
  return fromStr;
 }

http://www.javalearns.com/

 /**
  * 去掉字符串中的空格
  * 
  * @param str
  * @return String
  */
 public static String removeBlank(String str) {
  StringBuilder sb = new StringBuilder();
  char c = ' ';
  for (int i = 0; i < str.length(); i++) {
   char ch = str.charAt(i);
   if (ch != c) {
    sb.append(ch);
   }
  }
  return sb.toString();
 }

 public static String replaceBlank(String s) {
  Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  // String str="I am a, I am Hello ok, \n new line ffdsa!";
  String str = s;
  System.out.println("before:" + str);
  Matcher m = p.matcher(str);
  String after = m.replaceAll("");
  System.out.println("after:" + after);
  return after;
 }

}

文章转载自  http://www.javalearns.com/Html/?1532.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值