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. 我的ORM之十一 -- 缓存

    我的ORM索引 对某一个查询频繁重复,应该使用缓存. 缓存应该是可以配置. 配置 Web.config: <configuration> <configSections> &l ...

  2. C#中的线程一(委托中的异步)

    C#中的线程一(委托中的异步) 一.同步委托 我们平时所用的委托以同步居多,我们编写一个方法和相关委托进行演示: publicdelegatevoid DoSomethingDelegate(stri ...

  3. Java设计模式7:适配器模式

    适配器模式 适配器模式说的是,可以把一个类的接口变换成客户端所期待的另一种接口,使得原本因接口不匹配而无法在一起工作的两个类可以一起工作. 适配器模式的用途 适配器模式的用途,在网上找了一幅图,挺形象 ...

  4. Etag缓存在PHP和NodeJS中的实现

    HTTP 提供了许多页面缓存的方案,其中属 Etag 和 Last-Modified 应用最广.本文会先介绍 Etag 的应用场景,然后说说他在 php 和 node 中的使用. 本文地址:http: ...

  5. json-smart 使用示例(推荐fastjson)

    关于json库,请使用fastjson,这是我用过的最好用的json库! 地址:https://github.com/alibaba/fastjson ======================== ...

  6. 可拖动的DIV

    在做WEB UI设计的时候,拖动某个HTML元素已经成为一种不能忽视的用户界面模式,比较典型的应用例子就是Dialog,一个元素是怎么实现拖动的呢?其实原理非常简单,要想实现首先得了解几个基本知识. ...

  7. 哪些JavaScript IDE最好用?

    阅读本文之前,分享大家一张图片,看图会发现JavaScript开发需求最高,占比达到42.84%,因此掌握JavaScript语言好工作就不愁啦,工欲善其事必先利其器,那么选择IDE来开发是至关重要的 ...

  8. spring源码分析(一)IoC、DI

    创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...

  9. PHP基础 创建

    注意:1.网页文件放在wamp中的www文件下: 2.www文件下不能出现中文: 网页浏览的方法: 1.没有建立站点:localhost/文件所在位置 2.建立站点: (1)站点-新建站点-打开对话框 ...

  10. Atitit blend mode COLOR_DODGE 混合模式  “颜色减淡”模式

    Atitit blend mode COLOR_DODGE 混合模式  "颜色减淡"模式 1.1. 混合模式是图像处理技术中的一个技术名词1 1.2. 目录1 1.3. 颜色减淡C ...