StringExtensions
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的更多相关文章
- C#扩展方法类库StringExtensions
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 一个技术汪的开源梦 —— 基于 .Net Core 的公共组件之序列化
一个技术汪的开源梦 —— 目录 想必大家在项目中都接触过 JSON 或者 XML 吧,为了将对象在网络上传输或者将其持久化必须将其序列化为一个字符串然后进行后续操作.常见的就是将其序列化成 JSON ...
- [转]各种移动GPU压缩纹理的使用方法
介绍了各种移动设备所使用的GPU,以及各个GPU所支持的压缩纹理的格式和使用方法.1. 移动GPU大全 目前移动市场的GPU主要有四大厂商系列:1)Imagination Technologies的P ...
- Kooboo CMS - @Html.FrontHtml().Meta()详解。
下面是代码: public virtual IHtmlString Meta() { AggregateHtmlString htmlStrings = new AggregateHtmlString ...
- Kooboo CMS - @Html.FrontHtml().HtmlTitle() 详解
首先我们找到这个类. 这个类有如下的方法: #region Title & meta [Obsolete("Use HtmlTitle")] public IHtmlStr ...
- C# Extension Methods
In C#, extension methods enable you to add methods to existing class without creating a new derived ...
- .NET JSON对象序列化和反序列化
class Program { static void Main(string[] args) { Console.WriteLine("========================== ...
- 从NullObject谈C#6.0改进
前言 本文来聊一聊我们经常会做的空值检查问题,从一个简单的空值检查Any Where,到设计模式的NullObjectPattern,再到C#6.0“可能”会提供的语法,让我们体验一次语言开发上的“持 ...
- .NET开发中经常用到的扩展方法
整理一下自己经常用到的几个扩展方法,在实际项目中确实好用,节省了不少的工作量. 1 匿名对象转化 在WinForm中,如果涉及较长时间的操作,我们一般会用一个BackgroundWorker来做封装 ...
随机推荐
- 跨平台开源通讯组件elastic communication
elastic communication是基于c#开发支持.net和mono的通讯组件(简称EC),EC的主要目的简化mono和.net下的通讯开发难度,通过EC可以非常快速地开发基于mono和.n ...
- ASP.NET 5 入门 (3) – Logging
ASP.NET 5 理解和入门 建立和开发ASP.NET 5 项目 使用自定义配置文件 ASP.NET 5 入门 (3) – Logging 前几天就关注到汤姆大叔的相关文档: 解读ASP.NET 5 ...
- 【腾讯Bugly干货分享】微信读书iOS性能优化
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/578c93ca9644bd524bfcabe8 “8小时内拼工作,8小时外拼成长 ...
- kali linux Python开发环境初始化
kali linux Python 黑客编程1 开发环境初始化 为什么要选择Python? Python作为目前Linux系统下最流行的编程语言之一,对于安全工作者的作用可以和C++相提并论.Pyth ...
- Windows Azure Service Bus Topics实现系统松散耦合
前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...
- node访问iis使用keep-alive设置不当
iis默认的连接超时时间为2分钟 ,因此node程序使用keep-alive访问时,keep-alive的时间不应该超过2分钟,否则在请求完成后,node端继续保持连接,2分钟后iis断开连接,会导致 ...
- Unity3D使用经验总结 编辑器扩展篇
一个引擎,最重要的就是工具,工具除了提升开发速度,提供可视化操作环境以外,还带了容错功能. 它使得大家的工作局限在一定的范围内,比如一个变量的配置,或者是一些类型的选择. 使用编辑器,使得既使不太明白 ...
- Atitit 图像处理知识点 知识体系 知识图谱
Atitit 图像处理知识点 知识体系 知识图谱 图像处理知识点 图像处理知识点体系 v2 qb24.xlsx 基本知识图像金字塔op膨胀叠加混合变暗识别与检测分类肤色检测other验证码生成 基本 ...
- 常用Math 方法
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 11:26:44 * @version $Id$ */ Math.pow ...
- CKFinder_AspDotNet_2.4 破解方法 去版权
CKFinder是一个比较好用的Web端文件管理器,虽然UI不是很好看,但是因为能搞到源码,所以比起网上那些只有付费之后才能下载到源码的Web端文件管理器要好许多,至少你可以在确定该控件是否能用在你的 ...