C#中常用的字符串处理函数,仅供参考
/*=============================================================
* 常用的字符串处理
* Author : Danny,Li
* E-mail : [email protected]
* Edition: V-101014
*=============================================================*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IRCommon
{
public class StringHelper
{
//键盘信息串(用于字符串加密)
static string pwdstrs = ":3!@XSK#$GyTuVB%2^&C*t(dfD)_+?rFHJs6/>4iop9A5LZ 8a.<,{zUIxc}[]'|ERY\\10qwPeg7hjklNMv=-bnm~`QWO";
/// <summary>
/// 私有构造方法,不允许创建实例
/// </summary>
private StringHelper()
{
// TODO: Add constructor logic here
}
/// <summary>
/// 从字符串左边截取指定字符个数的子串
/// </summary>
/// <param name="argString">欲截取的字符串</param>
/// <param name="argLength">指定截取字符个数</param>
/// <returns>截取后的相应字串/空串</returns>
public static string GetLeftString(string argString, int argLength)
{
if (argString == null || argString.Length == 0)
return String.Empty;
if (argLength <= 0 || argLength > argString.Length)
return String.Empty;
return argString.Substring(0, argLength);
}
/// <summary>
/// 从字符串右边截取指定字符个数的字串
/// </summary>
/// <param name="argString">欲截取的字符串</param>
/// <param name="argLength">指定截取字符个数</param>
/// <returns>截取后的相应字串/空串</returns>
public static string GetRightString(string argString, int argLength)
{
if (argString == null || argString.Length == 0)
return String.Empty;
if (argLength <= 0 || argLength > argString.Length)
return String.Empty;
return argString.Substring(argString.Length - argLength, argLength);
}
/// <summary>
/// 获得指定字符串的字节长度
/// </summary>
/// <param name="argString">指定的字符串</param>
/// <returns>指定字符串的字节长度(英文字母为1个字节,汉字为2个字节)</returns>
public static int GetStringByteLength(string argString)
{
if (argString == null || argString.Length == 0)
return 0;
byte[] bytes = Encoding.Default.GetBytes(argString);
return bytes.Length;
}
/// <summary>
/// 从字符串左边截取指定字节数的子串
/// </summary>
/// <param name="argString">欲截取的字符串</param>
/// <param name="argLength">指定的字节数</param>
/// <returns>截取后的相应字串/空串</returns>
public static string GetLeftStringByte(string argString, int argLength)
{
if (argString == null || argString.Length == 0 || argLength <= 0)
return String.Empty;
if (argLength > GetStringByteLength(argString))
return argString;
int i = argString.Length;
while (GetStringByteLength(GetLeftString(argString, i)) > argLength)
{
i = i - 1;
}
return GetLeftString(argString, i);
}
/// <summary>
/// 判断字符串是否为空
/// </summary>
/// <param name="argText">欲判断的字符串</param>
/// <returns>为空返回True,不为空返回False</returns>
public static bool IsEmpty(string argString)
{
if (argString == null || argString.Trim().Length == 0)
return true;
return false;
}
/// <summary>
/// 获得某个特定字符在指定字符串中出现的次数
/// </summary>
/// <param name="argString">指定字符串</param>
/// <param name="argChar">特定字符</param>
/// <returns>出现的次数</returns>
public static int GetDisplayCount(string argString, char argChar)
{
if (argString == null || argString.Length == 0)
return 0;
int count = 0;
int currentPos = 0;
while (true)
{
currentPos = argString.IndexOf(argChar, currentPos) + 1;
if (currentPos == 0)
break;
else
count = count + 1;
}
return count;
}
/// <summary>
/// 获得某个特定字符串在指定字符串中出现的次数
/// </summary>
/// <param name="argString">指定字符串</param>
/// <param name="argDestString">特定字符串</param>
/// <returns>出现次数</returns>
public static int GetDisplayCount(string argString, string argDestString)
{
if (argString == null || argString.Length == 0 ||
argDestString == null || argDestString.Length == 0)
return 0;
int count = 0;
int currentPos = 0;
while (true)
{
currentPos = argString.IndexOf(argDestString, currentPos) + argDestString.Length;
if (currentPos == argDestString.Length - 1)
break;
else
count = count + 1;
}
return count;
}
/// <summary>
/// 判断指定的字符串是否是整数
/// </summary>
/// <param name="argString">指定字符串</param>
/// <returns>是整数返回True,不是整数返回False</returns>
public static bool IsInteger(string argString)
{
if (IsEmpty(argString))
return false;
try
{
int.Parse(argString);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 判断指定字符串是否全为数字
/// </summary>
/// <param name="argString">指定字符串</param>
/// <returns>全为数字返回True,不全为数字返回False</returns>
public static bool IsNumeric(string argString)
{
if (IsEmpty(argString))
return false;
char[] chars = argString.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] < '0' || chars[i] > '9')
return false;
}
return true;
}
/// <summary>
/// 判断指定的字符串是否为数值
/// </summary>
/// <param name="argString">指定字符串</param>
/// <returns>是数值返回True,不是数值返回False</returns>
public static bool IsDigital(string argString)
{
if (IsEmpty(argString))
return false;
try
{
decimal.Parse(argString);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 判断指定字符串是否为日期
/// </summary>
/// <param name="argString">指定字符串</param>
/// <returns>是日期返回True,不是日期返回False</returns>
public static bool IsDateTime(string argString)
{
if (IsEmpty(argString))
return false;
try
{
DateTime.Parse(argString);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 格式化指定数值的小数位数
/// </summary>
/// <param name="argDecimal">指定的数值</param>
/// <param name="argDecimalDigits">保留小数位数</param>
/// <returns>格式化后的数值字符串</returns>
public static string GetFormatDigital(decimal argDecimal, int argDecimalDigits)
{
string text = argDecimal.ToString();
string format = new string('#', text.Length - 1) + "0." + new string('0', argDecimalDigits);
return argDecimal.ToString(format);
}
/// <summary>
/// 格式化指定数值为货币表达
/// </summary>
/// <param name="argDecimal">指定数值</param>
/// <returns>格式化后的货币表达</returns>
public static string GetCurrency(decimal argDecimal)
{
string sDecimal = argDecimal.ToString();
//分拆
string reghtText = null;
string leftText = null;
int dotPos = sDecimal.IndexOf('.');
if (dotPos == -1)
{
leftText = sDecimal;
reghtText = string.Empty;
}
else
{
leftText = sDecimal.Substring(0, dotPos);
reghtText = sDecimal.Substring(dotPos);
}
//逗号分隔
int paras = (leftText.Length + 2) / 3;
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < paras - 1; i++)
buffer.Append("###,");
buffer.Append("##0");
leftText = decimal.Parse(leftText).ToString(buffer.ToString());
return leftText + reghtText;
}
/// <summary>
/// 用查询字符串的方法对指定字符串加密
/// </summary>
/// <param name="pwd">指定字符串</param>
/// <returns>加密后的字符串</returns>
public static string EncryptStr(string pwd)
{
return EncryptStr(pwd, pwdstrs);
}
/// <summary>
/// 用查询字符串的方法对指定字符串加密
/// </summary>
/// <param name="pwd">指定字符串</param>
/// <param name="sourcestr">键盘信息串</param>
/// <returns>加密后的字符串</returns>
public static string EncryptStr(string pwd, string sourcestr)
{
if (IsEmpty(pwd))
return "";
string strs = sourcestr;
char[] chs = strs.ToCharArray();
char[] pwds = pwd.ToCharArray();
string[] posi = new string[pwds.Length];
for (int x = 0; x < pwds.Length; x++)
{
int n = strs.IndexOf(pwds[x]);
if (n < 0)
return "";
if (n < 10)
posi[x] = "0" + n;
else
posi[x] = "" + n;
}
pwd = "";
for (int x = 0; x < posi.Length; x++)
pwd = pwd + posi[x];
return pwd;
}
/// <summary>
/// 对此类中加密的字符串解密
/// </summary>
/// <param name="pwd">欲解密的字符串</param>
/// <returns>解密后的字符串</returns>
public static string UnEncryptStr(string pwd)
{
return UnEncryptStr(pwd, pwdstrs);
}
/// <summary>
/// 对此类中加密的字符串解密
/// </summary>
/// <param name="pwd">欲解密的字符串</param>
/// <param name="sourcestr">键盘信息串</param>
/// <returns>解密后的字符串</returns>
public static string UnEncryptStr(string pwd, string sourcestr)
{
if (IsEmpty(pwd))
return pwd;
int len = pwd.Length;
if ((len % 2) != 0)
return "";
int m = 0;
string strs = sourcestr;
char[] unpwd = new char[pwd.Length / 2];
for (int x = 0; x < unpwd.Length; x++)
{
string tmps = pwd.Substring(x * 2, 2);
try
{
m = Int32.Parse(tmps);
}
catch (System.Exception)
{
return "";
}
unpwd[x] = strs[m];
}
pwd = "";
for (int x = 0; x < unpwd.Length; x++)
pwd = pwd + unpwd[x];
return pwd;
}
}
}