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. [Xamarin] 從Xamarin中呼叫 *.jar 的 library - 呼叫篇 (转帖)

    上篇文章我們建立一個很簡單的Library : com.example.blackfactory.UtilFunc 現在我們要在Xamarin 中呼叫囉! 首先我們要先成立一個橋接的專案 JARBri ...

  2. Java中的受检异常

    Java中的受检异常 Java提供了三种异常类型,受检异常(checked exception).运行时异常(runtime exception).错误(error).那么这受检异常在实际开发中又有什 ...

  3. 从点击Button到弹出一个MessageBox, 背后发生了什么

    思考一个最简单的程序行为:我们的Dialog上有一个Button, 当用户用鼠标点击这个Button时, 我们弹出一个MessageBox. 这个看似简单的行为, 谁能说清楚它是如何运行起来的,背后究 ...

  4. 十分钟搞定CSS选择器

    在最近的web开发中是不是就会用到一些选择器,发现很多尤其是CSS3新增的不太熟悉,在此总结一下. 优先级 不同级别 1. 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式. ...

  5. Android学习笔记50:使用WebView控件浏览网页

    在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...

  6. AngularJS快速入门指南12:模块

    AngularJS模块定义了一个application. 模块是一个application中不同部分的容器. application中的所有控制器都应该属于一个模块. 带有一个控制器的模块 下面这个a ...

  7. JVM的SNMP监控配置

    近期看了一下JVM对监控的支持,除了常规的JMX外居然还有SNMP, 有点意思, 这个网管协议适配的地方还真多,那么就先测试一下. 先随便找一个能在后台持续运行的java小程序,如我手头的BIO的so ...

  8. bianma 水平 技巧

    能够写出这样的代码, 其实体现了水平 switch (state) { case 0: break; case 3: return; // already connected case 4: stat ...

  9. Redis操作命令总结

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/118.html?1455860089 一.key pattern 查询相应 ...

  10. MVVM架构~knockoutjs系列之扩展ajax验证~验证数据是否存在

    返回目录 在大部分网站里,用户名都是唯一的,即当用户注册时,如果用户输入的名字不合法,我们需要提示用户,让用户再起个新名字,而这种复杂的验证一般是通过JS来实现的,如果把它集成到ko里,那就完美了.有 ...