4 Star 4 Fork 1

小王子1987 / Log日志类库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
StringExt.cs 5.70 KB
一键复制 编辑 原始数据 按行查看 历史
小王子1987 提交于 2017-05-18 16:22 . 全部代码文件,平台VS2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using CommonLib.Log;
namespace CommonLib.Log
{
public static class StringExt
{
private static string logPath = ConfigurationManager.AppSettings["CommonLog"] == null ? string.Empty : ConfigurationManager.AppSettings["CommonLog"].ToString();
public static bool IsNullOrEmpty(this string s)
{
return string.IsNullOrEmpty(s);
}
public static string FormatWith(this string format, params object[] args)
{
string ret = string.Empty;
try
{
ret = string.Format(format, args);
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "FormatWith => 将值[{0}]进行格式失败[{1}]".FormatWith(format, ex.Message), logPath);
}
return ret;
}
public static bool IsMatch(this string s, string pattern)
{
if (s == null) return false;
else return Regex.IsMatch(s, pattern);
}
public static string Match(this string s, string pattern)
{
if (s == null) return "";
return Regex.Match(s, pattern).Value;
}
public static bool IsInt(this string s)
{
int i;
return int.TryParse(s, out i);
}
public static int ToInt(this string s)
{
int ret = -1;
try
{
ret = int.Parse(s);
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "ToInt => 将值[{0}]转换为整形失败[{1}]".FormatWith(s, ex.Message), logPath);
}
return ret;
}
public static decimal ToDecimal(this string s)
{
decimal ret = -1;
try
{
ret = decimal.Parse(s);
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "ToDecimal => 将值[{0}]转换为小数失败[{1}]".FormatWith(s, ex.Message), logPath);
}
return ret;
}
public static DateTime ToDateTime(this string s)
{
DateTime dt = DateTime.Now;
try
{
dt = DateTime.Parse(s);
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "ToDateTime => 将值[{0}]转换为日期失败[{1}]".FormatWith(s, ex.Message), logPath);
}
return dt;
}
public static string ToCamel(this string s)
{
if (IsNullOrEmpty(s)) return s;
return s[0].ToString().ToLower() + s.Substring(1);
}
public static string ToPascal(this string s)
{
if (IsNullOrEmpty(s)) return s;
return s[0].ToString().ToUpper() + s.Substring(1);
}
public static string Left(this string s, int length)
{
string result = s.Substring(0, length);
return result;
}
public static string Right(this string s, int length)
{
string result = s.Substring(s.Length - length, length);
return result;
}
public static string Mid(this string s, int startIndex, int length)
{
string result = s.Substring(startIndex, length);
return result;
}
public static string Mid(this string s, int startIndex)
{
string result = s.Substring(startIndex);
return result;
}
public static string StringToJson(this string s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
switch (c)
{
case '\"':
sb.Append("\\\"");
break;
case '/':
sb.Append("\\/");
break;
case '\b': //退格
sb.Append("\\b");
break;
case '\f': //走纸换页
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n"); //换行
break;
case '\r': //回车
sb.Append("\\r");
break;
case '\t': //横向跳格
sb.Append("\\t");
break;
default:
sb.Append(c);
break;
}
}
return sb.ToString();
}
public static string StringDanYinToJSON(this string ors)
{
ors = ors == null ? "" : ors;
StringBuilder buffer = new StringBuilder(ors);
int i = 0;
while (i < buffer.Length)
{
if (buffer[i] == '\'' || buffer[i] == '\\')
{
buffer.Insert(i, '\\');
i += 2;
}
else
{
i++;
}
}
return buffer.ToString();
}
}
}
C#
1
https://gitee.com/xiaowangzi_1987/logrizhileiku.git
git@gitee.com:xiaowangzi_1987/logrizhileiku.git
xiaowangzi_1987
logrizhileiku
Log日志类库
master

搜索帮助