C#經常使用的字符串擴大辦法匯總。本站提示廣大學習愛好者:(C#經常使用的字符串擴大辦法匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是C#經常使用的字符串擴大辦法匯總正文
本文實例匯總了C#經常使用的字符串擴大辦法,分享給年夜家供年夜家參考。詳細以下:
EString.cs文件以下:
/// <summary>
/// 擴大字符串類
/// </summary>
public static class EString
{
#region 數據轉換
#region 轉Int
/// <summary>
/// 轉Int,掉敗前往0
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static int ToInt(this string t)
{
int n;
if (!int.TryParse(t, out n))
return 0;
return n;
}
/// <summary>
/// 轉Int,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static int ToInt(this string t, int pReturn)
{
int n;
if (!int.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 能否是Int true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsInt(this string t)
{
int n;
return int.TryParse(t, out n);
}
#endregion
#region 轉Int16
/// <summary>
/// 轉Int,掉敗前往0
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static Int16 ToInt16(this string t)
{
Int16 n;
if (!Int16.TryParse(t, out n))
return 0;
return n;
}
/// <summary>
/// 轉Int,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static Int16 ToInt16(this string t, Int16 pReturn)
{
Int16 n;
if (!Int16.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 能否是Int true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsInt16(this string t)
{
Int16 n;
return Int16.TryParse(t, out n);
}
#endregion
#region 轉byte
/// <summary>
/// 轉byte,掉敗前往0
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static byte Tobyte(this string t)
{
byte n;
if (!byte.TryParse(t, out n))
return 0;
return n;
}
/// <summary>
/// 轉byte,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static byte Tobyte(this string t, byte pReturn)
{
byte n;
if (!byte.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 能否是byte true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool Isbyte(this string t)
{
byte n;
return byte.TryParse(t, out n);
}
#endregion
#region 轉Long
/// <summary>
/// 轉Long,掉敗前往0
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static long ToLong(this string t)
{
long n;
if (!long.TryParse(t, out n))
return 0;
return n;
}
/// <summary>
/// 轉Long,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static long ToLong(this string t, long pReturn)
{
long n;
if (!long.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 能否是Long true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsLong(this string t)
{
long n;
return long.TryParse(t, out n);
}
#endregion
#region 轉Double
/// <summary>
/// 轉Int,掉敗前往0
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static double ToDouble(this string t)
{
double n;
if (!double.TryParse(t, out n))
return 0;
return n;
}
/// <summary>
/// 轉Double,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static double ToDouble(this string t, double pReturn)
{
double n;
if (!double.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 能否是Double true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsDouble(this string t)
{
double n;
return double.TryParse(t, out n);
}
#endregion
#region 轉Decimal
/// <summary>
/// 轉Decimal,掉敗前往0
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static decimal ToDecimal(this string t)
{
decimal n;
if (!decimal.TryParse(t, out n))
return 0;
return n;
}
/// <summary>
/// 轉Decimal,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static decimal ToDecimal(this string t, decimal pReturn)
{
decimal n;
if (!decimal.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 能否是Decimal true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsDecimal(this string t)
{
decimal n;
return decimal.TryParse(t, out n);
}
#endregion
#region 轉DateTime
/// <summary>
/// 轉DateTime,掉敗前往以後時光
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static DateTime ToDateTime(this string t)
{
DateTime n;
if (!DateTime.TryParse(t, out n))
return DateTime.Now;
return n;
}
/// <summary>
/// 轉DateTime,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static DateTime ToDateTime(this string t, DateTime pReturn)
{
DateTime n;
if (!DateTime.TryParse(t, out n))
return pReturn;
return n;
}
/// <summary>
/// 轉DateTime,掉敗前往pReturn
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static string ToDateTime(this string t, string pFormat, string pReturn)
{
DateTime n;
if (!DateTime.TryParse(t, out n))
return pReturn;
return n.ToString(pFormat);
}
/// <summary>
/// 轉DateTime,掉敗前往空
/// </summary>
/// <param name="e"></param>
/// <param name="pReturn">掉敗前往的值</param>
/// <returns></returns>
public static string ToDateTime(this string t, string pFormat)
{
return t.ToDateTime(pFormat, string.Empty);
}
public static string ToShortDateTime(this string t)
{
return t.ToDateTime("yyyy-MM-dd", string.Empty);
}
/// <summary>
/// 能否是DateTime true:是 false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsDateTime(this string t)
{
DateTime n;
return DateTime.TryParse(t, out n);
}
#endregion
#region 與int[]相干
/// <summary>
/// 轉int[],字符串以逗號(,)離隔,請確保字符串內容都正當,不然會失足
/// </summary>
/// <param name="pStr"></param>
/// <returns></returns>
public static int[] ToIntArr(this string t)
{
return t.ToIntArr(new char[] { ',' });
}
/// <summary>
/// 轉int[],字符串以逗號(,)離隔,請確保字符串內容都正當,不然會失足
/// </summary>
/// <param name="t"></param>
/// <param name="pSplit">離隔的</param>
/// <returns></returns>
public static int[] ToIntArr(this string t, char[] pSplit)
{
if (t.Length == 0)
{
return new int[] { };
}
string[] ArrStr = t.Split(pSplit, StringSplitOptions.None);
int[] iStr = new int[ArrStr.Length];
for (int i = 0; i < ArrStr.Length; i++)
iStr[i] = int.Parse(ArrStr[i]);
return iStr;
}
#endregion
#region 過濾字符串的非int,從新組分解字符串
/// <summary>
/// 過濾字符串的非int,從新組分解字符串
/// </summary>
/// <param name="t"></param>
/// <param name="pSplit">分隔符</param>
/// <returns></returns>
public static string ClearNoInt(this string t, char pSplit)
{
string sStr = string.Empty;
string[] ArrStr = t.Split(pSplit);
for (int i = 0; i < ArrStr.Length; i++)
{
string lsStr = ArrStr[i];
if (lsStr.IsInt())
sStr += lsStr + pSplit;
else
continue;
}
if (sStr.Length > 0)
sStr = sStr.TrimEnd(pSplit);
return sStr;
}
/// <summary>
/// 過濾字符串的非int,從新組分解字符串
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string ClearNoInt(this string t)
{
return t.ClearNoInt(',');
}
#endregion
#region 能否可以轉換成int[]
/// <summary>
/// 能否可以轉換成int[],true:是,false:否
/// </summary>
/// <param name="t"></param>
/// <param name="pSplit">分隔符</param>
/// <returns></returns>
public static bool IsIntArr(this string t, char pSplit)
{
string[] ArrStr = t.Split(pSplit);
bool b = true;
for (int i = 0; i < ArrStr.Length; i++)
{
if (!ArrStr[i].IsInt())
{
b = false;
break;
}
}
return b;
}
/// <summary>
/// 能否可以轉換成int[],true:是,false:否
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsIntArr(this string t)
{
return t.IsIntArr(',');
}
#endregion
#endregion
#region 載取左字符
/// <summary>
/// 載取左字符
/// </summary>
/// <param name="t"></param>
/// <param name="pLen">字符個數</param>
/// <param name="pReturn">超越時後邊要加的前往的內容</param>
/// <returns></returns>
public static string Left(this string t, int pLen, string pReturn)
{
if (t == null || t.Length == 0)
return string.Empty;
pLen *= 2;
int i = 0, j = 0;
foreach (char c in t)
{
if (c > 127)
{
i += 2;
}
else
{
i++;
}
if (i > pLen)
{
return t.Substring(0, j) + pReturn;
}
j++;
}
return t;
}
public static string Left(this string t, int pLen)
{
return Left(t, pLen, string.Empty);
}
public static string StrLeft(this string t, int pLen)
{
if (t == null)
{
return "";
}
if (t.Length > pLen)
{
return t.Substring(0, pLen);
}
return t;
}
#endregion
#region 刪除文件名或途徑的特別字符
private class ClearPathUnsafeList
{
public static readonly string[] unSafeStr = { "/", "\\", ":", "*", "?", "\"", "<", ">", "|" };
}
/// <summary>
/// 刪除文件名或途徑的特別字符
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string ClearPathUnsafe(this string t)
{
foreach (string s in ClearPathUnsafeList.unSafeStr)
{
t = t.WordStr(s, "");
}
return t;
}
#endregion
#region 字符串真實長度 如:一個漢字為兩個字節
/// <summary>
/// 字符串真實長度 如:一個漢字為兩個字節
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int LengthReal(this string s)
{
return Encoding.Default.GetBytes(s).Length;
}
#endregion
#region 去除小數位最初為0的
/// <summary>
/// 去除小數位最初為0的
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static decimal ClearDecimal0(this string t)
{
decimal d;
if (decimal.TryParse(t, out d))
{
return decimal.Parse(double.Parse(d.ToString("g")).ToString());
}
return 0;
}
#endregion
#region 進制轉換
/// <summary>
/// 16進制轉二進制
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string Change16To2(this string t)
{
String BinOne = string.Empty;
String BinAll = string.Empty;
char[] nums = t.ToCharArray();
for (int i = 0; i < nums.Length; i++)
{
string number = nums[i].ToString();
int num = Int32.Parse(number, System.Globalization.NumberStyles.HexNumber);
BinOne = Convert.ToString(num, 2).PadLeft(4, '0');
BinAll = BinAll + BinOne;
}
return BinAll;
}
/// <summary>
/// 二進制轉十進制
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static Int64 Change2To10(this string t)
{
char[] arrc = t.ToCharArray();
Int64 all = 0, indexC = 1;
for (int i = arrc.Length - 1; i >= 0; i--)
{
if (arrc[i] == '1')
{
all += indexC;
}
indexC = indexC * 2;
}
return all;
}
/// <summary>
/// 二進制轉換byte[]數組
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static byte[] Change2ToBytes(this string t)
{
List<byte> list = new List<byte>();
char[] arrc = t.ToCharArray();
byte n = 0;
char c;
int j = 0;
//倒序獲得位
for (int i = arrc.Length - 1; i >= 0; i--)
{
c = arrc[i];
if (c == '1')
{
n += Convert.ToByte(Math.Pow(2, j));
}
j++;
if (j % 8 == 0)
{
list.Add(n);
j = 0;
n = 0;
}
}
//殘剩最高位
if (n > 0)
list.Add(n);
byte[] arrb = new byte[list.Count];
int j1 = 0;
//倒序
for (int i = list.Count - 1; i >= 0; i--)
{
arrb[j1] = list[i];
j1++;
}
return arrb;
}
/// <summary>
/// 二進制轉化為索引id數據,從右到左
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static int[] Change2ToIndex(this string t)
{
List<int> list = new List<int>();
char[] arrc = t.ToCharArray();
char c;
int j = 0;
//倒序獲得位
for (int i = arrc.Length - 1; i >= 0; i--)
{
j++;
c = arrc[i];
if (c == '1')
{
list.Add(j);
}
}
return list.ToArray();
}
#endregion
#region html url編碼 解碼
/// <summary>
/// Html Encode
/// </summary>
/// <param name="pStr"></param>
/// <returns></returns>
public static string HtmlEncode(this string t)
{
return HttpContext.Current.Server.HtmlEncode(t);
}
/// <summary>
/// Html Decode
/// </summary>
/// <param name="pStr"></param>
/// <returns></returns>
public static string HtmlDecode(this string t)
{
return HttpContext.Current.Server.HtmlDecode(t);
}
/// <summary>
/// URL Encode
/// </summary>
/// <param name="pStr"></param>
/// <returns></returns>
public static string URLEncode(this string t)
{
return HttpContext.Current.Server.UrlEncode(t);
}
/// <summary>
/// URL Decode
/// </summary>
/// <param name="pStr"></param>
/// <returns></returns>
public static string URLDecode(this string t)
{
return HttpContext.Current.Server.UrlDecode(t);
}
#endregion
#region 向客戶端輸入內容
/// <summary>
/// 向客戶端輸入內容
/// </summary>
/// <param name="t"></param>
public static void Write(this string t)
{
HttpContext.Current.Response.Write(t);
}
/// <summary>
/// 向客戶端輸入內容
/// </summary>
/// <param name="t"></param>
public static void WriteLine(this string t)
{
HttpContext.Current.Response.Write(t + "<br />");
}
#endregion
}
願望本文所述對年夜家的C#法式設計有所贊助。