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. Linux 网络编程(多路复用)

    服务器端代码 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/soc ...

  2. 最少知识原则(Least Knowledge Principle)

    最少知识原则(Least Knowledge Principle),或者称迪米特法则(Law of Demeter),是一种面向对象程序设计的指导原则,它描述了一种保持代码松耦合的策略.其可简单的归纳 ...

  3. gtest 1.7编译错误:std:tr1:tuple模板参数过多的解决方案

    在gtest/gtest.h文件中添加如下代码 #define _VARIADIC_MAX 10

  4. Windows Azure Service Bus Topics实现系统松散耦合

    前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...

  5. java提高篇(十八)-----数组之一:认识JAVA数组

          噢,它明白了,河水既没有牛伯伯说的那么浅,也没有小松鼠说的那么深,只有自己亲自试过才知道!道听途说永远只能看到表明现象,只有亲自试过了,才知道它的深浅!!!!! 一.什么是数组      ...

  6. Unity3D热更新全书-脚本(二) 两级分化

    上篇明确了我们探讨的脚本是什么:是写在文本文件里面的代码,可以作为资源加载,取得字符串再执行. 可是为什么世界上会有那么多的脚本?而其使用方法完全看起来不一样呢?这是因为每种脚本都有自己的定位,在不同 ...

  7. Linux内核TCP/IP参数分析与调优

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0225/167.html?1456482565 如下图展示的是TCP的三个阶段.1,TCP三次握手. ...

  8. Oracle动态执行语句

      一.为什么要使用动态执行语句? 由于在PL/SQL 块或者存储过程中只支持DML语句及控制流语句,并不支持DDL语句,所以Oracle动态执行语句便应允而生了.关于DDL与DML的区别,请参见:D ...

  9. Laravel5.0学习--02 实例进阶

    本文以laravel5.0.22为例. 本节以新建一个简单的博客作为实例. 准备工作 数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhos ...

  10. Atitit 面向对象  封装的实现原理

    Atitit 面向对象  封装的实现原理 1.1. 动态对象的模拟使用map+函数接口可以实现1 1.2. 在用结构体 + 函数指针 模拟 对象 1 1.3. This指针..1 1.4. " ...