代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
网站字符串操作类
作者:
brainmao
/ 发布于
2014/6/26
/
760
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Core.Common.Web { /// <summary> /// 网站字符串操作类 /// </summary> public static class String { #region 静态方法 /// <summary> /// 左截取 /// </summary> /// <param name="inputString">字符串</param> /// <param name="length">长度</param> /// <returns>截取后的字符串</returns> public static string Left(string inputString, int length) { if (inputString.Length < length) return inputString; else return inputString.Substring(0, length); } /// <summary> /// 左截取 /// </summary> /// <param name="inputString">字符串</param> /// <param name="length">长度</param> /// <param name="addString">截取后添加的字符串</param> /// <returns>截取后的字符串</returns> public static string Left(string inputString, int length, string addString) { if (inputString.Length < length) return inputString; else return inputString.Substring(0, length) + addString; } /// <summary> /// 右截取 /// </summary> /// <param name="inputString">字符串</param> /// <param name="length">长度</param> /// <returns>截取后的字符串</returns> public static string Right(string inputString, int length) { if (inputString.Length < length) return inputString; else return inputString.Substring(inputString.Length - length, length); } /// <summary> /// 不区分大小写的替换 /// </summary> /// <param name="original">原字符串</param> /// <param name="pattern">需替换字符</param> /// <param name="replacement">被替换内容</param> /// <returns>替换后的字符串</returns> public static string ReplaceEx(string original, string pattern, string replacement) { int count = 0; int position0 = 0; int position1 = 0; string upperString = original.ToUpper(); string upperPattern = pattern.ToUpper(); int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length); char[] chars = new char[original.Length + Math.Max(0, inc)]; while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1) { for (int i = position0; i < position1; ++i) chars[count++] = original[i]; for (int i = 0; i < replacement.Length; ++i) chars[count++] = replacement[i]; position0 = position1 + pattern.Length; } if (position0 == 0) return original; for (int i = position0; i < original.Length; ++i) chars[count++] = original[i]; return new string(chars, 0, count); } /// <summary> /// 替换html中的特殊字符 /// </summary> /// <param name="theString">需要进行替换的文本</param> /// <returns>替换完的文本</returns> public static string HtmlEncode(string theString) { theString = theString.Replace(">", ">"); theString = theString.Replace("<", "<"); theString = theString.Replace(" ", " "); theString = theString.Replace("\"", """); theString = theString.Replace("'", "'"); theString = theString.Replace("\r\n", "<br/> "); return theString; } /// <summary> /// 恢复html中的特殊字符 /// </summary> /// <param name="theString">需要恢复的文本。</param> /// <returns>恢复好的文本。</returns> public static string HtmlDecode(string theString) { theString = theString.Replace(">", ">"); theString = theString.Replace("<", "<"); theString = theString.Replace(" ", " "); theString = theString.Replace(""", "\""); theString = theString.Replace("'", "'"); theString = theString.Replace("<br/> ", "\r\n"); return theString; } /// <summary> /// 将html转成js代码,不完全和原始数据一致 /// </summary> /// <param name="source">html文本</param> /// <returns>js代码</returns> public static string HtmlToJs(string source) { return string.Format("document.write(\"{0}\");", string.Join("\");\r\ndocument.write(\"", source.Replace("\\", "\\\\") .Replace("/", "\\/") .Replace("'", "\\'") .Replace("\"", "\\\"") .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) )); } /// <summary> /// 将html转成可输出的js字符串,不完全和原始数据一致 /// </summary> /// <param name="source">html文本</param> /// <returns>js文本</returns> public static string HtmlToJsString(string source) { return string.Format("{0}", string.Join(" ", source.Replace("\\", "\\\\") .Replace("/", "\\/") .Replace("'", "\\'") .Replace("\"", "\\\"") .Replace("\t", "") .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) )); } /// <summary> /// 过滤所有特殊特号 /// </summary> /// <param name="theString">需过滤的字符串</param> /// <returns>过滤后的字符串</returns> public static string FilterSymbol(string theString) { string[] aryReg = { "'", "\"", "\r", "\n", "<", ">", "%", "?", ",", ".", "=", "-", "_", ";", "|", "[", "]", "&", "/" }; for (int i = 0; i < aryReg.Length; i++) { theString = theString.Replace(aryReg[i], string.Empty); } return theString; } /// <summary> /// 过滤一般特殊特号,如单引、双引和回车和分号等 /// </summary> /// <param name="theString">需过滤的字符串</param> /// <returns>过滤后的字符串</returns> public static string SafetyString(string theString) { string[] aryReg = { "'", ";", "\"", "\r", "\n", "<", ">" }; for (int i = 0; i < aryReg.Length; i++) { theString = theString.Replace(aryReg[i], string.Empty); } return theString; } /// <summary> /// 正则表达式取值 /// </summary> /// <param name="htmlCode">HTML代码</param> /// <param name="regexString">正则表达式</param> /// <param name="groupKey">正则表达式分组关键字</param> /// <param name="rightToLeft">是否从右到左</param> /// <returns>匹配的值</returns> public static string[] GetRegexValue(string htmlCode, string regexString, string groupKey, bool rightToLeft) { MatchCollection m; Regex r; if (rightToLeft == true) { r = new Regex(regexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft); } else { r = new Regex(regexString, RegexOptions.IgnoreCase | RegexOptions.Singleline); } m = r.Matches(htmlCode); string[] MatchValue = new string[m.Count]; for (int i = 0; i < m.Count; i++) { MatchValue[i] = m[i].Groups[groupKey].Value; } return MatchValue; } #endregion } }
试试其它关键字
字符串操作类
字符串
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
brainmao
贡献的其它代码
(
9
)
.
实现网页中查看offic文件,word,ppt等
.
获取当前计算机上所有的驱动器信息
.
验证码类
.
利用隐藏域向页面间数据传递
.
邮件发送类
.
Cookie静态操作类
.
网站字符串操作类
.
项目中图片处理类
.
实现新建,编辑word内容功能
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3