public static string MakeSafeSql(this string s)
{
string t = s;
t = t.Replace("'", "''");
t = t.Replace("[", "[[]");
t = t.Replace("%", "[%]");
t = t.Replace("_", "[_]");
return t;
} public static string ReplaceUnsafeSqlParameter(this string s)
{
string t = s;
t = t.Replace("[", "[[]");
t = t.Replace("%", "[%]");
t = t.Replace("_", "[_]");
return t;
} /// <summary>
/// 比较2个字符串对象是否相等,区分大小写。
/// <remarks>2个字符串转换为小写字符进行比较</remarks>
/// </summary>
/// <param name="compareWith"></param>
/// <returns>若相等,则为True;反之为False</returns>
public static bool IsEqual(this string s, string compareWith)
{
if (compareWith == null)
{
return false;
}
if (s.ToLower().Trim() == compareWith.ToLower().Trim())
{
return true;
}
return false;
} /// <summary>
/// 返回一个布尔值,指定两个字符串是否相等,不区分大小写。
/// </summary>
/// <param name="compareWith"></param>
/// <returns>若相等,则为True;反之为False。</returns>
public static bool IsEqualIgnoreCase(this string s, string compareWith)
{
return (s == compareWith) || StringComparer.OrdinalIgnoreCase.Compare(s, compareWith) == 0;
} /// <summary>
/// 将指定字符串的首字母转换为大写字符
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string UpperFirstChar(this string s)
{
if (s.Trim().Length == 0)
{
return s;
} string result = string.Empty;
string[] tmp = s.Split(' ');
for (int i = 0; i < tmp.Length; i++)
{
result += Upper(tmp[i]);
if (tmp.Length == 1 || i == tmp.Length - 1)
{
}
else
{
result += " ";
}
}
return result;
} private static string Upper(this string s)
{
if (s.Length == 0) return s; char[] array = s.ToCharArray();
string result = string.Empty;
for (int i = 0; i < s.Length; i++)
{
if (i == 0)
{
result += array[i].ToString().ToUpper();
}
else
{
result += array[i].ToString().ToLower();
}
}
return result;
}
[Obsolete("These helper methods have been merged in to string, please use string.MakeSafeSql() to instead")]
public static string MakeSafeSql(string inputSQL)
{
string s = inputSQL;
s = inputSQL.Replace("'", "''");
s = s.Replace("[", "[[]");
s = s.Replace("%", "[%]");
s = s.Replace("_", "[_]");
return s;
} /// <summary>
/// 由于系统提供比较字符串只有一个空格时,会认为比较的字符串不为空。
/// 该方法是对系统方法的一个补充,即传入字符串有且只有一个空格时,验证字符串为空;
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(string input)
{
if (input == null)
return true;
if (input.Trim().Length == 0 )
{
return true;
}
return false;
} /// <summary>
/// 比较2个字符串对象是否相等,区分大小写。
/// <remarks>2个字符串转换为小写字符进行比较</remarks>
/// </summary>
/// <param name="input1"></param>
/// <param name="input2"></param>
/// <returns>若相等,则为True;反之为False</returns>
public static bool AreEqual(string input1, string input2)
{
if (input1 == null && input2 == null)
{
return true;
}
if (input1 == null || input2 == null)
{
return false;
}
if (input1.ToLower().Trim() == input2.ToLower().Trim())
{
return true;
}
return false;
} /// <summary>
/// 返回一个布尔值,指定两个字符串是否相等,不区分大小写。
/// </summary>
/// <param name="strA"></param>
/// <param name="strB"></param>
/// <returns>若相等,则为True;反之为False。</returns>
public static bool AreEqualIgnoreCase(string input1, string input2)
{
return (input1 == input2) || StringComparer.OrdinalIgnoreCase.Compare(input1, input2) == 0;
} /// <summary>
/// 将指定字符串的首字母转换为大写字符
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
[Obsolete("These helper methods have been merged in to string, please use string.UpperFirstChar() to instead")]
public static string UpperFirstChar(string s)
{
if (TrimString(s) == null || s.Length == 0)
return s;
string result = string.Empty;
string[] tmp = s.Split(' ');
for (int i = 0; i < tmp.Length; i++)
{
result += Upper(tmp[i]);
if (tmp.Length == 1 || i == tmp.Length - 1)
{
}
else
{
result += " ";
}
}
return result;
} [Obsolete("These helper methods have been merged in to string, please use string.Upper() to instead")]
private static string Upper(string s)
{
if (s == null || s.Length == 0)
return s;
char[] array = s.ToCharArray();
string result = string.Empty;
for (int i = 0; i < s.Length; i++)
{
if (i == 0)
{
result += array[i].ToString().ToUpper();
}
else
{
result += array[i].ToString().ToLower();
}
}
return result;
} /// <summary>
/// 去掉字符串的前后空格。当字符串为null时,返回null
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string TrimString(string s)
{
return s == null ? null : s.Trim();
}

  

  

StringExtensions的更多相关文章

  1. C#扩展方法类库StringExtensions

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 一个技术汪的开源梦 —— 基于 .Net Core 的公共组件之序列化

    一个技术汪的开源梦 —— 目录 想必大家在项目中都接触过 JSON 或者 XML 吧,为了将对象在网络上传输或者将其持久化必须将其序列化为一个字符串然后进行后续操作.常见的就是将其序列化成 JSON ...

  3. [转]各种移动GPU压缩纹理的使用方法

    介绍了各种移动设备所使用的GPU,以及各个GPU所支持的压缩纹理的格式和使用方法.1. 移动GPU大全 目前移动市场的GPU主要有四大厂商系列:1)Imagination Technologies的P ...

  4. Kooboo CMS - @Html.FrontHtml().Meta()详解。

    下面是代码: public virtual IHtmlString Meta() { AggregateHtmlString htmlStrings = new AggregateHtmlString ...

  5. Kooboo CMS - @Html.FrontHtml().HtmlTitle() 详解

    首先我们找到这个类. 这个类有如下的方法: #region Title & meta [Obsolete("Use HtmlTitle")] public IHtmlStr ...

  6. C# Extension Methods

    In C#, extension methods enable you to add methods to existing class without creating a new derived ...

  7. .NET JSON对象序列化和反序列化

    class Program { static void Main(string[] args) { Console.WriteLine("========================== ...

  8. 从NullObject谈C#6.0改进

    前言 本文来聊一聊我们经常会做的空值检查问题,从一个简单的空值检查Any Where,到设计模式的NullObjectPattern,再到C#6.0“可能”会提供的语法,让我们体验一次语言开发上的“持 ...

  9. .NET开发中经常用到的扩展方法

    整理一下自己经常用到的几个扩展方法,在实际项目中确实好用,节省了不少的工作量. 1  匿名对象转化 在WinForm中,如果涉及较长时间的操作,我们一般会用一个BackgroundWorker来做封装 ...

随机推荐

  1. System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用”的错误

    还原sql2008数据库时遇到"System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用"的错误,在网上搜索解决方案如下: 1.在" ...

  2. 使用Location对象查询字符串参数

    location是BOM中最有用的对象之一: 1.它提供了与当前窗口中加载的文档有关的信息: 2.他还提供了一些导航功能. location对象的属性有: hash, host, hostname, ...

  3. DBCC DROPCLEANBUFFERS失效了?

    原文出处:http://www.sqlskills.com/blogs/paul/when-dbcc-dropcleanbuffers-doesnt-work/ DBCC DROPCLEANBUFFE ...

  4. Unity3D ogg下载并播放

    那天说了c#不好弄ogg和mp3的解码,然后就发现打脸了. 找到了一些开源的解码器,由于ogg的音质容量比要明显好于mp3,主要弄ogg的播放. 由于自带解码器,可以实现全平台下载并播放,甚至可以实现 ...

  5. C#Light 和 uLua的对比第二弹

    上次的对比大家还有印象否,C#Light和ulua对比各有胜负 今天我们加入一个去反射优化,这是uLua没办法实现的优化,我们也就只能不要脸的胜之不武了 以原生执行同一测试时间为X1,数字越小的越快 ...

  6. AngularJS快速入门指南18:Application

    是时候创建一个真正的AngularJS单页面应用程序了(SPA). 一个AngularJS应用程序示例 你已经了解了足够多的内容来创建第一个AngularJS应用程序: My Note Save Cl ...

  7. SQL——索引

    索引是根据表中一列或若干列按照一定顺序建立的列值与记录行之间的对应关系表.在数据库系统中建立索引主要有以下作用: l快速存取数据: l保证数据记录的唯一性: l实现表与表之间的参照完整性: l在使用O ...

  8. 浏览器 的 session 如何保持?!

    http://qindingsky.blog.163.com/blog/static/3122336200832853116360/ 在谈论session机制的时候,常常听到这样一种误解“只要关闭浏览 ...

  9. 爱上MVC~在Views的多级文件夹

    回到目录 在MVC里,你的控制器对应的视图一般是在Views目录,而如果希望在Views里再分几个模块文件夹默认是不允许的,我们需要做一下设置,就可以实现Views下的多次文件夹层次了,例如,我们有产 ...

  10. Js~(function(){})匿名自执行方法的作用

    匿名自执行方法体(function(){})经常用在设计JS插件上面,它定义相关组件的行为,自动初始化相关属性,而且在页面中可以直接执行,你不需要手动执行它,它被自动被执行! 在设计你的匿名自执行方法 ...