StringHelper
来源:互联网 发布:淘宝怎么写追评 编辑:程序博客网 时间:2024/05/21 09:30
using System;using System.Collections.Generic;using System.Globalization;using System.Runtime.InteropServices;using System.Text;using System.Text.RegularExpressions;using Microsoft.VisualBasic;namespace FmallExternal.Helper{ /// <summary> /// 字符串操作类 /// </summary> public static class StringHelper { /// <summary> /// Sql字符串过滤 /// </summary> public static string SqlSafeString(string vlaue, bool isdel) { if (!string.IsNullOrWhiteSpace(vlaue)) { if (isdel) { vlaue = vlaue.Replace("'", ""); vlaue = vlaue.Replace("\"", ""); return vlaue; } else { vlaue = vlaue.Replace("'", "'"); vlaue = vlaue.Replace("\"", """); return vlaue; } } else { return vlaue; } } /// <summary> /// 得到字符串长度,一个汉字长度为2 /// </summary> /// <param name="value">参数字符串</param> /// <returns></returns> public static int StrLength(string value) { ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; byte[] s = ascii.GetBytes(value); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) tempLen += 2; else tempLen += 1; } return tempLen; } /// <summary> /// 要处理的字符串 /// </summary> /// <param name="value">要处理的字符串</param> /// <param name="len">指定字节长度</param> /// <param name="isShowFix">是否显示神略号</param> /// <returns></returns> public static string ClipString(string value, int len, bool isShowFix) { if (!string.IsNullOrEmpty(value)) { ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(value); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) tempLen += 2; else tempLen += 1; if (i < value.Length) { tempString += value.Substring(i, 1); } else { break; } if (tempLen > len) break; } byte[] mybyte = Encoding.Default.GetBytes(value); if (isShowFix && mybyte.Length > len) tempString += "…"; return tempString; } else { return value; } } /// <summary> /// 截取指定长度字符串 /// </summary> /// <param name="value">要处理的字符串</param> /// <param name="len">指定字节长度</param> /// <returns></returns> public static string ClipString(string value, int len) { return ClipString(value, len, true); } /// <summary> /// HTML转行成TEXT /// </summary> /// <param name="strHtml"></param> /// <returns></returns> public static string SimpleHtmlToText(string strHtml) { Regex regex1 = new Regex("<.+?>", RegexOptions.IgnoreCase); Regex regex2 = new Regex("\\s+", RegexOptions.IgnoreCase); string strOutput = ""; if (strHtml != null) { strOutput = regex1.Replace(strHtml, ""); strOutput = regex2.Replace(strOutput, ""); } return strOutput; } /// <summary> /// HTML转行成TEXT,完全过滤 /// </summary> /// <param name="strHtml"></param> /// <returns></returns> public static string HtmlToText(string strHtml) { string[] aryReg ={ @"<script[^>]*?>.*?</script>", @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);", @"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n" }; //string newReg = aryReg[0]; string strOutput = strHtml; for (int i = 0; i < aryReg.Length; i++) { Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase); strOutput = regex.Replace(strOutput, string.Empty); } strOutput = strOutput.Replace("<", ""); strOutput = strOutput.Replace(">", ""); strOutput = strOutput.Replace("\r\n", ""); return strOutput; } /// <summary> /// 按字母表位序输出字母 /// </summary> /// <param name="index"></param> /// <returns></returns> public static char NumberToLetter(int index) { if (index >= 0 && index <= 25) { return Convert.ToChar(index + 65); } else { throw new ArgumentOutOfRangeException("index"); } } #region 繁简互转 /// <summary> /// 将字符串转换成简体中文 /// </summary> /// <param name="source">要转换的字符串</param> /// <returns>转换完成后的字符串</returns> public static string Big5ToGB(string source) { if (string.IsNullOrEmpty(source)) return string.Empty; source = source.Replace("麼", "么"); String target = new String(' ', source.Length); if (source.Length == NativeMethods.LCMapString(0x0800, 0x02000000, source, source.Length, target, source.Length)) return target; else throw new FormatException("无法转换成简体中文"); } /// <summary> /// 将字符串转换为繁体中文 /// </summary> /// <param name="source">要转换的字符串</param> /// <returns>转换完成后的字符串</returns> public static string GBToBig5(string source) { if (string.IsNullOrEmpty(source)) return string.Empty; String target = new String(' ', source.Length); if (source.Length == NativeMethods.LCMapString(0x0800, 0x04000000, source, source.Length, target, source.Length)) return target; else throw new FormatException("无法转换成繁体中文"); } #endregion #region 提取汉字首字母 /// <summary> /// GB2312中的汉字边界值 /// </summary> private static int[] areacode = new int[27] { 45217,45253,45761,46318,46826, 47010,47297,47614,47614,48119, 49062,49324,49896,50371,50614, 50622,50906,51387,51446,52218, 52218,52218,52698,52980,53689,54481, 63487}; /// <summary> /// 取出汉字的编码 cn 汉字 /// </summary> /// <param name="myChar"></param> /// <returns></returns> private static int GetGbValue(char myChar) { byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(new char[] { myChar }); if (bytes.Length < 2) return 0; return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff); } /// <summary> /// 获取单个汉字的首拼音 /// </summary> /// <param name="myChar">需要转换的字符</param> /// <returns>转换结果</returns> private static char getSpell(char myChar) { if (myChar >= 'a' && myChar <= 'z') return (char)(myChar - 'a' + 'A'); if (myChar >= 'A' && myChar <= 'Z') return myChar; int gb = GetGbValue(myChar); if ((gb < areacode[0]) || (gb > areacode[26]))// 在码表区间之前,直接返回 return myChar; switch (myChar) { case '吖': case '嗷': return 'A'; case '浣': case '桦': return 'H'; case '晟': return 'S'; case '鑫': return 'X'; default: for (int i = 0; i < 26; i++) { if (areacode[i] <= gb && gb < areacode[i + 1]) { return Encoding.GetEncoding("gb2312").GetString(new byte[] { (byte)(65 + i) })[0]; } } break; } return '?'; } /// <summary> /// 提取汉字首字母 /// </summary> /// <param name="strText">需要转换的字</param> /// <returns>转换结果</returns> public static string GetChineseSpell(string strText) { strText = Big5ToGB(strText); int len = strText.Length; string myStr = ""; for (int i = 0; i < len; i++) { myStr += getSpell(strText[i]); } return myStr; } #endregion } /// <summary> /// 原生方法 /// </summary> internal static class NativeMethods { [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true, ThrowOnUnmappableChar = true)] internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest); }}
0 0
- StringHelper
- StringHelper
- StringHelper
- StringHelper
- StringHelper
- StringHelper.cs
- StringHelper类
- StringHelper类
- C# StringHelper
- StringHelper类的代码
- 字符串操作类 StringHelper
- Common下StringHelper.cs
- StringHelper-字符串辅助类
- Common下StringHelper.cs
- String工具类StringHelper
- yii2.0 StringHelper
- (转)StringHelper类的代码
- C#字符串操作类StringHelper源码
- HDU 1584 蜘蛛牌
- CentOS 7 下挂载NTFS文件系统并实行开机自动挂载
- == 和equals
- ValueHelper
- LIBSVM在MATLAB中的使用
- StringHelper
- 二维数组查找
- 设置修改CentOS系统时区
- Best Cow Line(POJ-3617)
- cocos2d-x3.2 在Windows下打包Android平台APK
- 只允许ajax请求
- android控件之ListView 全教程
- 1.14
- 深入Spark内核:任务调度(3)-TaskScheduler