using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace QM.Common
{
///
/// 模块编号:QM.Common.FuncString
/// 作 用:共通字符串处理函数
/// 作 者:
/// 编写日期:2014-05-05
///
public class FuncString
{
#region 字符串验证
///
/// 修改特殊字符
///
/// 要替换的字符串
///
public static string CheckStr(string pString)
{
#region
if (pString == null)
{
return "";
}
return pString.Replace("'", "''");
#endregion
}
///
/// 恢复特殊字符
///
/// 要替换的字符串
///
public static string UnCheckStr(string pString)
{
#region
if (pString == null)
{
return "";
}
return pString.Replace("''", "'");
#endregion
}
///
/// 过滤特殊字符
/// "'.\r\n"
///
/// 要替换的字符串
///
public static string FilterStr(string pString)
{
return pString.Replace("'", " ").Replace(".", "").Replace("\r\n", " ");
}
#endregion
#region 字符串截取
///
/// 截取字符串函数
///
/// 所要截取的字符串
/// 启始位置
///
public static string GetSubString(string pString, int pStartIndex)
{
#region
return (pString.Length > pStartIndex) ? pString.Substring(pStartIndex) : pString;
#endregion
}
///
/// 截取字符串函数
///
/// 所要截取的字符串
/// 启始位置
/// 截取字符串的长度
///
public static string GetSubString(string pString, int pStartIndex, int pNum)
{
#region
return (pString.Length > pStartIndex) ? pString.Substring(pStartIndex, pNum) : pString;
#endregion
}
#endregion
#region 替换 组合
///
/// 按字符串位数补0
///
/// 字符串
/// 返回字符长度
///
public static string FillZero(string pString, int pStringLen)
{
#region
if (pString.Length < pStringLen)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pStringLen - pString.Length; i++)
{
sb.Append("0");
}
sb.Append(pString);
return sb.ToString();
}
else
{
return pString;
}
#endregion
}
///
/// 数值按指定位数补0
///
/// 整型数值
/// 返回字符长度
///
public static string FillZero(int pValue, int pStringLen)
{
#region
string strTemp = pValue.ToString();
if (strTemp.Length < pStringLen)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pStringLen - strTemp.Length; i++)
{
sb.Append("0");
}
sb.Append(strTemp);
return sb.ToString();
}
else
{
return strTemp;
}
#endregion
}
///
/// 生成指定长度的字符串,即生成strLong个str字符串
///
/// 生成的长度
/// 以pString生成字符串
///
public static string StringOfChar(int pLong, string pString)
{
#region
string ReturnStr = string.Empty;
for (int i = 0; i < pLong; i++)
{
ReturnStr += pString;
}
return ReturnStr;
#endregion
}
///
/// 将 Stream 转化成 string
///
///
///
public static string ConvertStreamToString(Stream pStream)
{
#region
string strResult = "";
StreamReader sr = new StreamReader(pStream, Encoding.UTF8);
Char[] read = new Char[256];
// Read 256 charcters at a time.
int count = sr.Read(read, 0, 256);
while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
string str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
// 释放资源
sr.Close();
return strResult;
#endregion
}
///
/// 获取数组元素的合并字符串
///
/// 字符串数组
///
public static string GetArrayString(string[] pStringArray)
{
#region
string totalString = null;
for (int i = 0; i < pStringArray.Length; i++)
{
totalString = totalString + pStringArray[i];
}
return totalString;
#endregion
}
///
/// 分隔字符串
///
/// 要分隔的字符串
/// 分隔标识
///
public static string[] StringSplit(string pSplitString, string pSplitFlag)
{
#region
return pSplitString.Split(new string[] { pSplitFlag }, StringSplitOptions.RemoveEmptyEntries);
#endregion
}
#endregion
#region 中文 处理
///
/// 获取汉字第一个拼音
///
///
///
public static string getSpells(string pInpString)
{
#region
int intLen = pInpString.Length;
string strVal = "";
for (int intCnt = 0; intCnt < intLen; intCnt++)
{
strVal += getSpell(pInpString.Substring(intCnt, 1));
}
return strVal;
#endregion
}
///
/// 获取汉字第一个拼音中使用
///
///
///
public static string getSpell(string pCnString)
{
#region
byte[] bytArrCN = Encoding.Default.GetBytes(pCnString);
if (bytArrCN.Length > 1)
{
int intArea = (short)bytArrCN[0];
int intPos = (short)bytArrCN[1];
int code = (intArea << 8) + intPos;
int[] intArrAreaCode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119,
48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387,
51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
for (int intCnt = 0; intCnt < 26; intCnt++)
{
int intMax = 55290;
if (intCnt != 25)
{
intMax = intArrAreaCode[intCnt + 1];
}
if (intArrAreaCode[intCnt] <= code && code < intMax)
{
return Encoding.Default.GetString(new byte[] { (byte)(65 + intCnt) });
}
}
return "?";
}
else
{
return pCnString;
}
#endregion
}
///
/// 半角转全角
///
///
///
public static string GetQuanJiao(string pBJString)
{
#region
char[] chrArrBJStr = pBJString.ToCharArray();
for (int intCnt = 0; intCnt < chrArrBJStr.Length; intCnt++)
{
byte[] bytArrEn = System.Text.Encoding.Unicode.GetBytes(chrArrBJStr, intCnt, 1);
if (bytArrEn.Length == 2)
{
if (bytArrEn[1] == 0)
{
bytArrEn[0] = (byte)(bytArrEn[0] - 32);
bytArrEn[1] = 255;
chrArrBJStr[intCnt] = System.Text.Encoding.Unicode.GetChars(bytArrEn)[0];
}
}
}
string strNew = new string(chrArrBJStr);
return strNew;
#endregion
}
///
/// 全角转半角
///
///
///
public static string GetBanJiao(string pQJString)
{
#region
char[] chrArrQJ = pQJString.ToCharArray();
for (int intCnt = 0; intCnt < chrArrQJ.Length; intCnt++)
{
byte[] bytArrEn = System.Text.Encoding.Unicode.GetBytes(chrArrQJ, intCnt, 1);
if (bytArrEn.Length == 2)
{
if (bytArrEn[1] == 255)
{
bytArrEn[0] = (byte)(bytArrEn[0] + 32);
bytArrEn[1] = 0;
chrArrQJ[intCnt] = System.Text.Encoding.Unicode.GetChars(bytArrEn)[0];
}
}
}
string strNew = new string(chrArrQJ);
return strNew;
#endregion
}
#endregion
}
}