- 浏览: 2159361 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1878)
- [网站分类]ASP.NET (141)
- [网站分类]C# (80)
- [随笔分类]NET知识库 (80)
- [随笔分类]摘抄文字[非技术] (3)
- [随笔分类]养生保健 (4)
- [网站分类]读书区 (16)
- [随笔分类]赚钱 (7)
- [网站分类].NET新手区 (233)
- [随笔分类]网站 (75)
- [网站分类]企业信息化其他 (4)
- [网站分类]首页候选区 (34)
- [网站分类]转载区 (12)
- [网站分类]SQL Server (16)
- [网站分类]程序人生 (7)
- [网站分类]WinForm (2)
- [随笔分类]错误集 (12)
- [网站分类]JavaScript (3)
- [随笔分类]小说九鼎记 (69)
- [随笔分类]技术文章 (15)
- [网站分类]求职面试 (3)
- [网站分类]其他技术区 (6)
- [网站分类]非技术区 (10)
- [发布至博客园首页] (5)
- [网站分类]jQuery (6)
- [网站分类].NET精华区 (6)
- [网站分类]Html/Css (10)
- [随笔分类]加速及SEO (10)
- [网站分类]Google开发 (4)
- [随笔分类]旅游备注 (2)
- [网站分类]架构设计 (3)
- [网站分类]Linux (23)
- [随笔分类]重要注册 (3)
- [随笔分类]Linux+PHP (10)
- [网站分类]PHP (11)
- [网站分类]VS2010 (2)
- [网站分类]CLR (1)
- [网站分类]C++ (1)
- [网站分类]ASP.NET MVC (2)
- [网站分类]项目与团队管理 (1)
- [随笔分类]个人总结 (1)
- [随笔分类]问题集 (3)
- [网站分类]代码与软件发布 (1)
- [网站分类]Android开发 (1)
- [网站分类]MySQL (1)
- [网站分类]开源研究 (6)
- ddd (0)
- 好久没写blog了 (0)
- sqlserver (2)
最新评论
-
JamesLiuX:
博主,能组个队么,我是Freelancer新手。
Freelancer.com(原GAF – GetAFreelancer)帐户里的钱如何取出? -
yw10260609:
我认为在混淆前,最好把相关代码备份一下比较好,不然项目完成后, ...
DotFuscator 小记 -
日月葬花魂:
大哥 能 加我个QQ 交流一下嘛 ?51264722 我Q ...
web应用程序和Web网站区别 -
iaimg:
我想问下嵌入delphi写的程序总是出现窗体后面感觉有个主窗体 ...
C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部 -
iaimg:
代码地址下不了啊!
C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
namespace Jiaen.Components.Utility
{
/// <summary>
/// Helper functions for String not already found in C#.
/// Inspired by PHP String Functions that are missing in .Net.
/// </summary>
public static class StringHelper
{
/// <summary>
/// Base64 encodes a string.
/// </summary>
/// <param name="input">A string</param>
/// <returns>A base64 encoded string</returns>
public static string Base64StringEncode(string input)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(encbuff);
}
/// <summary>
/// Base64 decodes a string.
/// </summary>
/// <param name="input">A base64 encoded string</param>
/// <returns>A decoded string</returns>
public static string Base64StringDecode(string input)
{
byte[] decbuff = Convert.FromBase64String(input);
return System.Text.Encoding.UTF8.GetString(decbuff);
}
/// <summary>
/// A case insenstive replace function.
/// </summary>
/// <param name="input">The string to examine.</param>
/// <param name="newValue">The value to replace.</param>
/// <param name="oldValue">The new value to be inserted</param>
/// <returns>A string</returns>
public static string CaseInsenstiveReplace(string input, string newValue, string oldValue)
{
Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regEx.Replace(input, newValue);
}
/// <summary>
/// Removes all the words passed in the filter words parameters. The replace is NOT case
/// sensitive.
/// </summary>
/// <param name="input">The string to search.</param>
/// <param name="filterWords">The words to repace in the input string.</param>
/// <returns>A string.</returns>
public static string FilterWords(string input, params string[] filterWords)
{
return StringHelper.FilterWords(input, char.MinValue, filterWords);
}
/// <summary>
/// Removes all the words passed in the filter words parameters. The replace is NOT case
/// sensitive.
/// </summary>
/// <param name="input">The string to search.</param>
/// <param name="mask">A character that is inserted for each letter of the replaced word.</param>
/// <param name="filterWords">The words to repace in the input string.</param>
/// <returns>A string.</returns>
public static string FilterWords(string input, char mask, params string[] filterWords)
{
string stringMask = mask == char.MinValue ? string.Empty : mask.ToString();
string totalMask = stringMask;
foreach (string s in filterWords)
{
Regex regEx = new Regex(s, RegexOptions.IgnoreCase | RegexOptions.Multiline);
if (stringMask.Length > 0)
{
for (int i = 1; i < s.Length; i++)
totalMask += stringMask;
}
input = regEx.Replace(input, totalMask);
totalMask = stringMask;
}
return input;
}
/// <summary>
/// Checks the passed string to see if has any of the passed words. Not case-sensitive.
/// </summary>
/// <param name="input">The string to check.</param>
/// <param name="hasWords">The words to check for.</param>
/// <returns>A collection of the matched words.</returns>
public static MatchCollection HasWords(string input, params string[] hasWords)
{
StringBuilder sb = new StringBuilder(hasWords.Length + 50);
//sb.Append("[");
foreach (string s in hasWords)
{
sb.AppendFormat("({0})|", StringHelper.HtmlSpecialEntitiesEncode(s.Trim()));
}
string pattern = sb.ToString();
pattern = pattern.TrimEnd('|'); // +"]";
Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regEx.Matches(input);
}
/// <summary>
/// A wrapper around HttpUtility.HtmlEncode
/// </summary>
/// <param name="input">The string to be encoded</param>
/// <returns>An encoded string</returns>
public static string HtmlSpecialEntitiesEncode(string input)
{
return HttpUtility.HtmlEncode(input);
}
/// <summary>
/// A wrapper around HttpUtility.HtmlDecode
/// </summary>
/// <param name="input">The string to be decoded</param>
/// <returns>The decode string</returns>
public static string HtmlSpecialEntitiesDecode(string input)
{
return HttpUtility.HtmlDecode(input);
}
/// <summary>
/// MD5 encodes the passed string
/// </summary>
/// <param name="input">The string to encode.</param>
/// <returns>An encoded string.</returns>
public static string MD5String(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
/// <summary>
/// Verified a string against the passed MD5 hash.
/// </summary>
/// <param name="input">The string to compare.</param>
/// <param name="hash">The hash to compare against.</param>
/// <returns>True if the input and the hash are the same, false otherwise.</returns>
public static bool MD5VerifyString(string input, string hash)
{
// Hash the input.
string hashOfInput = StringHelper.MD5String(input);
// Create a StringComparer an comare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Left pads the passed input using the HTML non-breaking string entity ( )
/// for the total number of spaces.
/// </summary>
/// <param name="input">The string to pad.</param>
/// <param name="totalSpaces">The total number to pad the string.</param>
/// <returns>A padded string.</returns>
public static string PadLeftHtmlSpaces(string input, int totalSpaces)
{
string space = " ";
return PadLeft(input, space, totalSpaces * space.Length);
}
/// <summary>
/// Left pads the passed input using the passed pad string
/// for the total number of spaces. It will not cut-off the pad even if it
/// causes the string to exceed the total width.
/// </summary>
/// <param name="input">The string to pad.</param>
/// <param name="pad">The string to uses as padding.</param>
/// <param name="totalSpaces">The total number to pad the string.</param>
/// <returns>A padded string.</returns>
public static string PadLeft(string input, string pad, int totalWidth)
{
return StringHelper.PadLeft(input, pad, totalWidth, false);
}
/// <summary>
/// Left pads the passed input using the passed pad string
/// for the total number of spaces. It will cut-off the pad so that
/// the string does not exceed the total width.
/// </summary>
/// <param name="input">The string to pad.</param>
/// <param name="pad">The string to uses as padding.</param>
/// <param name="totalSpaces">The total number to pad the string.</param>
/// <returns>A padded string.</returns>
public static string PadLeft(string input, string pad, int totalWidth, bool cutOff)
{
if (input.Length >= totalWidth)
return input;
int padCount = pad.Length;
string paddedString = input;
while (paddedString.Length < totalWidth)
{
paddedString += pad;
}
// trim the excess.
if (cutOff)
paddedString = paddedString.Substring(0, totalWidth);
return paddedString;
}
/// <summary>
/// Right pads the passed input using the HTML non-breaking string entity ( )
/// for the total number of spaces.
/// </summary>
/// <param name="input">The string to pad.</param>
/// <param name="totalSpaces">The total number to pad the string.</param>
/// <returns>A padded string.</returns>
public static string PadRightHtmlSpaces(string input, int totalSpaces)
{
string space = " ";
return PadRight(input, space, totalSpaces * space.Length);
}
/// <summary>
/// Right pads the passed input using the passed pad string
/// for the total number of spaces. It will not cut-off the pad even if it
/// causes the string to exceed the total width.
/// </summary>
/// <param name="input">The string to pad.</param>
/// <param name="pad">The string to uses as padding.</param>
/// <param name="totalSpaces">The total number to pad the string.</param>
/// <returns>A padded string.</returns>
public static string PadRight(string input, string pad, int totalWidth)
{
return StringHelper.PadRight(input, pad, totalWidth, false);
}
/// <summary>
/// Right pads the passed input using the passed pad string
/// for the total number of spaces. It will cut-off the pad so that
/// the string does not exceed the total width.
/// </summary>
/// <param name="input">The string to pad.</param>
/// <param name="pad">The string to uses as padding.</param>
/// <param name="totalSpaces">The total number to pad the string.</param>
/// <returns>A padded string.</returns>
public static string PadRight(string input, string pad, int totalWidth, bool cutOff)
{
if (input.Length >= totalWidth)
return input;
string paddedString = string.Empty;
while (paddedString.Length < totalWidth - input.Length)
{
paddedString += pad;
}
// trim the excess.
if (cutOff)
paddedString = paddedString.Substring(0, totalWidth - input.Length);
paddedString += input;
return paddedString;
}
/// <summary>
/// Removes the new line (\n) and carriage return (\r) symbols.
/// </summary>
/// <param name="input">The string to search.</param>
/// <returns>A string</returns>
public static string RemoveNewLines(string input)
{
return StringHelper.RemoveNewLines(input, false);
}
/// <summary>
/// Removes the new line (\n) and carriage return (\r) symbols.
/// </summary>
/// <param name="input">The string to search.</param>
/// <param name="addSpace">If true, adds a space (" ") for each newline and carriage
/// return found.</param>
/// <returns>A string</returns>
public static string RemoveNewLines(string input, bool addSpace)
{
string replace = string.Empty;
if (addSpace)
replace = " ";
string pattern = @"[\r|\n]";
Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regEx.Replace(input, replace);
}
/// <summary>
/// Reverse a string.
/// </summary>
/// <param name="input">The string to reverse</param>
/// <returns>A string</returns>
public static string Reverse(string input)
{
if (input.Length <= 1)
return input;
char[] c = input.ToCharArray();
StringBuilder sb = new StringBuilder(c.Length);
for (int i = c.Length - 1; i > -1; i--)
sb.Append(c[i]);
return sb.ToString();
}
/// <summary>
/// Converts a string to sentence case.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>A string</returns>
public static string SentenceCase(string input)
{
if (input.Length < 1)
return input;
string sentence = input.ToLower();
return sentence[0].ToString().ToUpper() + sentence.Substring(1);
}
/// <summary>
/// Converts all spaces to HTML non-breaking spaces
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>A string</returns>
public static string SpaceToNbsp(string input)
{
string space = " ";
return input.Replace(" ", space);
}
/// <summary>
/// Removes all HTML tags from the passed string
/// </summary>
/// <param name="input">The string whose values should be replaced.</param>
/// <returns>A string.</returns>
public static string StripTags(string input)
{
Regex stripTags = new Regex("<(.|\n)+?>");
return stripTags.Replace(input, "");
}
/// <summary>
/// Converts a string to title case.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>A string.</returns>
public static string TitleCase(string input)
{
return TitleCase(input, true);
}
/// <summary>
/// Converts a string to title case.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <param name="ignoreShortWords">If true, does not capitalize words like
/// "a", "is", "the", etc.</param>
/// <returns>A string.</returns>
public static string TitleCase(string input, bool ignoreShortWords)
{
List<string> ignoreWords = null;
if (ignoreShortWords)
{
//TODO: Add more ignore words?
ignoreWords = new List<string>();
ignoreWords.Add("a");
ignoreWords.Add("is");
ignoreWords.Add("was");
ignoreWords.Add("the");
}
string[] tokens = input.Split(' ');
StringBuilder sb = new StringBuilder(input.Length);
foreach (string s in tokens)
{
if (ignoreShortWords == true
&& s != tokens[0]
&& ignoreWords.Contains(s.ToLower()))
{
sb.Append(s + " ");
}
else
{
sb.Append(s[0].ToString().ToUpper());
sb.Append(s.Substring(1).ToLower());
sb.Append(" ");
}
}
return sb.ToString().Trim();
}
/// <summary>
/// Removes multiple spaces between words
/// </summary>
/// <param name="input">The string to trim.</param>
/// <returns>A string.</returns>
public static string TrimIntraWords(string input)
{
Regex regEx = new Regex(@"[\s]+");
return regEx.Replace(input, " ");
}
/// <summary>
/// Converts new line(\n) and carriage return(\r) symbols to
/// HTML line breaks.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>A string.</returns>
public static string NewLineToBreak(string input)
{
Regex regEx = new Regex(@"[\n|\r]+");
return regEx.Replace(input, "<br />");
}
/// <summary>
/// Wraps the passed string up the
/// until the next whitespace on or after the total charCount has been reached
/// for that line. Uses the environment new line
/// symbol for the break text.
/// </summary>
/// <param name="input">The string to wrap.</param>
/// <param name="charCount">The number of characters per line.</param>
/// <returns>A string.</returns>
public static string WordWrap(string input, int charCount)
{
return StringHelper.WordWrap(input, charCount, false, Environment.NewLine);
}
/// <summary>
/// Wraps the passed string up the total number of characters (if cuttOff is true)
/// or until the next whitespace (if cutOff is false). Uses the environment new line
/// symbol for the break text.
/// </summary>
/// <param name="input">The string to wrap.</param>
/// <param name="charCount">The number of characters per line.</param>
/// <param name="cutOff">If true, will break in the middle of a word.</param>
/// <returns>A string.</returns>
public static string WordWrap(string input, int charCount, bool cutOff)
{
return StringHelper.WordWrap(input, charCount, cutOff, Environment.NewLine);
}
/// <summary>
/// Wraps the passed string up the total number of characters (if cuttOff is true)
/// or until the next whitespace (if cutOff is false). Uses the passed breakText
/// for lineBreaks.
/// </summary>
/// <param name="input">The string to wrap.</param>
/// <param name="charCount">The number of characters per line.</param>
/// <param name="cutOff">If true, will break in the middle of a word.</param>
/// <param name="breakText">The line break text to use.</param>
/// <returns>A string.</returns>
public static string WordWrap(string input, int charCount, bool cutOff,
string breakText)
{
StringBuilder sb = new StringBuilder(input.Length + 100);
int counter = 0;
if (cutOff)
{
while (counter < input.Length)
{
if (input.Length > counter + charCount)
{
sb.Append(input.Substring(counter, charCount));
sb.Append(breakText);
}
else
{
sb.Append(input.Substring(counter));
}
counter += charCount;
}
}
else
{
string[] strings = input.Split(' ');
for (int i = 0; i < strings.Length; i++)
{
counter += strings[i].Length + 1; // the added one is to represent the inclusion of the space.
if (i != 0 && counter > charCount)
{
sb.Append(breakText);
counter = 0;
}
sb.Append(strings[i] + ' ');
}
}
return sb.ToString().TrimEnd(); // to get rid of the extra space at the end.
}
/// <summary>
/// 处理null值
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string convertStr(string text)
{
if (text == null)
{
text = "";
}
return text.Trim();
}
/// <summary>
/// 处理长度值
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string subStr(int length,string text)
{
if (text.Length >= length)
{
text = text.Substring(0,length)+" ...";
}
return text;
}
}
}
相关推荐
`CString`类是Microsoft Visual C++的一个非常重要的字符串处理类,它提供了丰富的字符串操作方法,类似于C++标准库中的`std::string`。这个类在Windows环境下被广泛使用,但描述中提到,这个版本的`CString`实现了...
易语言字符串操作源码,字符串操作,字符串_取长度,字符串_取中间,字符串_取左边,字符串_取右边,字符串_替换,到宽字符,到多字节,取文本数据地址,取字节集数据地址,MultiByteToWideChar,WideCharToMultiByte
ABAP 中的字符串操作是开发者日常工作中不可或缺的一部分,本文将对 ABAP 中常用的字符串操作进行收集和整理,包括字符串连接、字符串分隔、字符串查找、字符串替换、去前导 0 等操作。 1. 字符串连接 CONCATENATE...
java 字符串工具类 java 字符串工具类java 字符串工具类 java 字符串工具类java 字符串工具类 java 字符串工具类java 字符串工具类 java 字符串工具类java 字符串工具类 java 字符串工具类java 字符串工具类 java ...
在C语言中,字符串操作是编程实践中不可或缺的一部分。C语言本身并不直接支持字符串类型,而是通过字符数组来处理字符串。本教程将深入探讨C语言中的字符串处理,包括基本概念、常用函数以及查找常用字符串的方法。 ...
本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用。 一、用strtok函数进行字符串分割 原型: char *...
`System.String`是C#中最常用的字符串操作类,它提供了丰富的功能,几乎能满足所有字符串处理的需求。接下来,我们将详细介绍如何使用`String`类进行字符串比较、定位以及更多操作。 #### 三、比较字符串 在程序...
下面是pb函数库中的一些常用字符串操作函数: 1. Fill()函数:Fill()函数可以建立一个由指定字符串填充的指定长度的字符串。其语法为Fill(chars, n),其中chars是指定用于重复填充的字符串,n是指定由该函数返回的...
C#-字符串操作类(替换字符串中危险字符、指定位置替换字符串、指定长度缩减字段并加...、指定字符串分割字符串、指定字符串位置获取字符串、过滤SQL中非法字符、检查SQL语句中是否有非法关键字、随机字符串生成、...
本压缩包"一个C语言常用字符串操作函数库.zip"中可能包含了一系列常用的C语言字符串处理函数的实现,这些函数可以帮助我们更高效、方便地进行字符串的创建、修改、比较和查找等操作。下面将详细解释一些常见的C语言...
字符串工具类,格式字符串、随机数生成、ip区间判断!
在C++编程中,字符串处理是常见的任务,MFC(Microsoft Foundation Classes)提供了一系列的类和函数来方便开发者处理字符串。以下是一些常用的字符串处理函数的详细解释: 1. **stpcpy**: 这个函数用于将源字符串`...
- 可以使用`new String()`构造函数创建一个字符串对象,但这不是常用的方式。 2. **转义字符**: - 在字符串中,某些特殊字符需要使用转义字符来表示,如`\n`(换行)、`\t`(制表符)等。 - 示例:`var example = ...
下面我们就基于题目中的信息,详细探讨一下如何在PB程序中实现一个常用的字符串替换函数。 ### PB程序中常用的字符串替换函数 #### 背景介绍 PowerBuilder是一种广泛应用于企业级应用开发的快速应用开发工具。尽管...
以下是Objective-C中字符串操作的详细总结,包含常用的方法和概念。 ### 不可变字符串NSString 1. **声明和初始化**: 使用`@”Hello”`这样的字面量方式是声明并初始化NSString对象的简便方法。 2. **字符串...
今天,我们将对字符串操作函数大全进行讲解,包括字符串拷贝、字符串连接、字符串比较等多种操作。 字符串拷贝 在字符串操作中,字符串拷贝是最基本的一种操作。C 语言提供了多种字符串拷贝函数,例如 strdup、...
本文将深入探讨Java中的字符串操作,包括创建、比较、拼接、查找与替换、分割以及格式化。 1. 创建字符串: Java中有两种方式创建字符串:通过`new`关键字或使用字符串字面量。 - 使用`new`关键字:`String str =...