天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

383 lines
11 KiB

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace QM.Common
{
///<summary>
/// 模块编号:QM.Common.FuncString
/// 作 用:共通字符串处理函数
/// 作 者:
/// 编写日期:2014-05-05
///</summary>
public class FuncString
{
#region 字符串验证
/// <summary>
/// 修改特殊字符
/// </summary>
/// <param name="pString">要替换的字符串</param>
/// <returns></returns>
public static string CheckStr(string pString)
{
#region
if (pString == null)
{
return "";
}
return pString.Replace("'", "''");
#endregion
}
/// <summary>
/// 恢复特殊字符
/// </summary>
/// <param name="pString">要替换的字符串</param>
/// <returns></returns>
public static string UnCheckStr(string pString)
{
#region
if (pString == null)
{
return "";
}
return pString.Replace("''", "'");
#endregion
}
/// <summary>
/// 过滤特殊字符
/// "'.\r\n"
/// </summary>
/// <param name="pString">要替换的字符串</param>
/// <returns></returns>
public static string FilterStr(string pString)
{
return pString.Replace("'", " ").Replace(".", "").Replace("\r\n", " ");
}
#endregion
#region 字符串截取
/// <summary>
/// 截取字符串函数
/// </summary>
/// <param name="pString">所要截取的字符串</param>
/// <param name="pStartIndex">启始位置</param>
/// <returns></returns>
public static string GetSubString(string pString, int pStartIndex)
{
#region
return (pString.Length > pStartIndex) ? pString.Substring(pStartIndex) : pString;
#endregion
}
/// <summary>
/// 截取字符串函数
/// </summary>
/// <param name="pString">所要截取的字符串</param>
/// <param name="pStartIndex">启始位置</param>
/// <param name="pNum">截取字符串的长度</param>
/// <returns></returns>
public static string GetSubString(string pString, int pStartIndex, int pNum)
{
#region
return (pString.Length > pStartIndex) ? pString.Substring(pStartIndex, pNum) : pString;
#endregion
}
#endregion
#region 替换 组合
/// <summary>
/// 按字符串位数补0
/// </summary>
/// <param name="pString">字符串</param>
/// <param name="pStringLen">返回字符长度</param>
/// <returns></returns>
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
}
/// <summary>
/// 数值按指定位数补0
/// </summary>
/// <param name="pValue">整型数值</param>
/// <param name="pStringLen">返回字符长度</param>
/// <returns></returns>
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
}
/// <summary>
/// 生成指定长度的字符串,即生成strLong个str字符串
/// </summary>
/// <param name="pLong">生成的长度</param>
/// <param name="pString">以pString生成字符串</param>
/// <returns></returns>
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
}
/// <summary>
/// 将 Stream 转化成 string
/// </summary>
/// <param name="pStream"></param>
/// <returns></returns>
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
}
/// <summary>
/// 获取数组元素的合并字符串
/// </summary>
/// <param name="pStringArray">字符串数组</param>
/// <returns></returns>
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
}
/// <summary>
/// 分隔字符串
/// </summary>
/// <param name="pSplitString">要分隔的字符串</param>
/// <param name="pSplitFlag">分隔标识</param>
/// <returns></returns>
public static string[] StringSplit(string pSplitString, string pSplitFlag)
{
#region
return pSplitString.Split(new string[] { pSplitFlag }, StringSplitOptions.RemoveEmptyEntries);
#endregion
}
#endregion
#region 中文 处理
/// <summary>
/// 获取汉字第一个拼音
/// </summary>
/// <param name="pInpString"></param>
/// <returns></returns>
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
}
/// <summary>
/// 获取汉字第一个拼音中使用
/// </summary>
/// <param name="pCnString"></param>
/// <returns></returns>
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
}
/// <summary>
/// 半角转全角
/// </summary>
/// <param name="pBJString"></param>
/// <returns></returns>
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
}
/// <summary>
/// 全角转半角
/// </summary>
/// <param name="pQJString"></param>
/// <returns></returns>
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
}
}