C# 一些常用的字符串扩展方法
以下可能是常用的.net扩展方法,记录下
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 ;
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 ;
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 ;
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 ;
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 ;
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 ;
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 == )
{
return new int[] { };
} string[] ArrStr = t.Split(pSplit, StringSplitOptions.None);
int[] iStr = new int[ArrStr.Length]; for (int i = ; 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 = ; i < ArrStr.Length; i++)
{
string lsStr = ArrStr[i]; if (lsStr.IsInt())
sStr += lsStr + pSplit;
else
continue;
} if (sStr.Length > )
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 = ; 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 == )
return string.Empty;
pLen *= ;
int i = , j = ;
foreach (char c in t)
{
if (c > )
{
i += ;
}
else
{
i++;
} if (i > pLen)
{
return t.Substring(, 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(, 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.Replace(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 ;
}
#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 = ; i < nums.Length; i++)
{
string number = nums[i].ToString();
int num = Int32.Parse(number, System.Globalization.NumberStyles.HexNumber); BinOne = Convert.ToString(num, ).PadLeft(, '');
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 = , indexC = ;
for (int i = arrc.Length - ; i >= ; i--)
{
if (arrc[i] == '')
{
all += indexC;
}
indexC = indexC * ;
} 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 = ;
char c;
int j = ;
//倒序获取位
for (int i = arrc.Length - ; i >= ; i--)
{
c = arrc[i]; if (c == '')
{
n += Convert.ToByte(Math.Pow(, j));
}
j++; if (j % == )
{
list.Add(n);
j = ;
n = ;
}
} //剩余最高位
if (n > )
list.Add(n); byte[] arrb = new byte[list.Count]; int j1 = ;
//倒序
for (int i = list.Count - ; i >= ; 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 = ; //倒序获取位
for (int i = arrc.Length - ; i >= ; i--)
{
j++;
c = arrc[i]; if (c == '')
{
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# 一些常用的字符串扩展方法的更多相关文章
- ES6 模版字符串及常用的es6扩展方法
1.ES6 模版字符串es6 模版字符串主要用于简化字符串的拼接 <script type="text/javascript"> let obj={name:'rdb' ...
- python学习笔记(四)- 常用的字符串的方法
一.常用的字符串方法(一):(字符串是不能被修改的) 1)a.strip() #默认去掉字符串两边的空格和换行符 a = ' 字符串 \n\n ' c = a.strip() a.lstrip() ...
- ES6之字符串扩展方法(常用)
es6这个String对象倒是扩展了不少方法,但是很多都是跟字符编码相关,个人选了几个感觉比较常用的方法: includes 搜索字符的神器 还记得我们之前如何判断某个字符串对象是否包含特地字符的吗? ...
- C#常用的字符串处理方法
1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...
- JS 学习笔记(一)常用的字符串去重方法
要求:从输入框中输入一串字符,按回车后输出去重后的字符串 方法一: <body> <input type="text" id="input" ...
- PythonStudy——字符串扩展方法 String extension method
')) ')) print('***000123123***'.lstrip('*')) print('***000123123***'.rstrip('*')) print('华丽分割线'.cent ...
- .NET 简单的扩展方法使用。
写代码时,我们经常会碰到dll中提供的方法,不够用或者不好用的情况.而且我们也不方便去更改dll本身的源码. 这时候我们可以使用.NET提供的"扩展方法"去解决这个问题. 下面我写 ...
- WebAPi添加常用扩展方法及思维发散
前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...
- 从js的repeat方法谈js字符串与数组的扩展方法
js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...
随机推荐
- AttributeError: 'NoneType' object has no attribute 'append'
大多数是这个原因: gongzi = [] for p in [1,2,3]: gongzi = gongzi.append(p) #改为如下即可 gongzi = [] for p in [1,2, ...
- Windows使用中的一些小技巧
1.网站保存在桌面 在桌面新建一个快捷方式,然后输入网址即可.
- CListCtrl控件使用方法总结
今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtr ...
- Jmeter(二)参数化
参数化是自动化测试脚本的一种常用技巧.简单来说,参数化的一般用法就是将脚本中的某些输入使用参数来代替,在脚本运行时指定参数的取值范围和规则:这样,脚本在运行时就可以根据需要选取不同的参数值作为输入.这 ...
- Spark 2.2 DataFrame的一些算子操作
Spark Session中的DataFrame类似于一张关系型数据表.在关系型数据库中对单表或进行的查询操作,在DataFrame中都可以通过调用其API接口来实现. 可以参考,Scala提供的Da ...
- POJ3176:Cow Bowling(数字三角形问题)
地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...
- TensorFlow学习笔记(七)Tesnor Board
为了更好的管理.调试和优化神经网络的训练过程,TensorFlow提供了一个可视化工具TensorBoard.TensorBoard可以有效的展示TensorFlow在运行过程中的计算图..各种指标随 ...
- opencv:vs2015添加了包含目录依然无法打开‘opencv2/core/core.hpp’ 解决方法
安装环境 win10 vs2015 出错和改错 按网上的教程,配置好opencv后,包括已经把以下内容添加到'包含目录'了: E:\openCV\opencv\build\include E:\ope ...
- Linux 最常用的20条命令
1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切 ...
- #if defined(__cplusplus)
由于C++编译器需要支持函数的重载,会改变函数的名称,因此dll的导出函数通常是标准C定义的.这就使得C和C++的互相调用变得很常见.但是有时可能又会直接用C来调用,不想重新写代码,让标准C编写的dl ...