字符串操作【转】
字符串操作
from:http://www.opent.cn/a/094/1182.shtml
from:http://www.opent.cn/a/094/1182.shtml
1
2
3 using System;
4 using System.Text;
5 using System.Text.RegularExpressions;
6
7 namespace Example
8 {
9 /// <summary>
10 /// 替换字符串
11 /// </summary>
12 public class StringRepStrs
13 {
14
15
16
17 public StringRepStrs()
18 {
19 }
20
21 #region 普通替换字符串
22
23 /// <summary>
24 /// 普通替换字符串
25
26 /// </summary>
27 /// <param name="src">源字符串</param>
28 /// <param name="pattern">要匹配的正则表达式模式</param>
29 /// <param name="replacement">替换字符串</param>
30 /// <returns>已修改的字符串</returns>
31 public static string Replace(string src, string pattern, string replacement)
32 {
33 return Replace(src, pattern, replacement, RegexOptions.None);
34 }
35
36 #endregion
37
38 #region 正则替换字符串
39
40 /// <summary>
41 /// 正则替换字符串
42
43 /// </summary>
44 /// <param name="src">要修改的字符串</param>
45 /// <param name="pattern">要匹配的正则表达式模式</param>
46 /// <param name="replacement">替换字符串</param>
47 /// <param name="options">匹配模式</param>
48 /// <returns>已修改的字符串</returns>
49 public static string Replace(string src, string pattern, string replacement, RegexOptions options)
50 {
51 Regex regex = new Regex(pattern, options | RegexOptions.Compiled);
52
53 return regex.Replace(src, replacement);
54 }
55
56 #endregion
57
58 #region 不区分大小写替换字符串
59
60 /// <summary>
61 /// 不区分大小写替换字符串
62
63 /// </summary>
64 /// <param name="src">源字符串</param>
65 /// <param name="pattern">要匹配的正则表达式模式</param>
66 /// <param name="replacement">替换字符串</param>
67 /// <returns>已修改的字符串</returns>
68 public static string ReplaceIgnoreCase(string src, string pattern, string replacement)
69 {
70 return Replace(src, pattern, replacement, RegexOptions.IgnoreCase);
71 }
72
73 #endregion
74
75
76
77 /// <summary>
78 /// 删除字符串中指定的内容
79 /// </summary>
80 /// <param name="src">要修改的字符串</param>
81 /// <param name="pattern">要删除的正则表达式模式</param>
82 /// <returns>已删除指定内容的字符串</returns>
83 public static string Drop(string src, string pattern)
84 {
85 return Replace(src, pattern, "");
86 }
87
88
89
90 /// <summary>
91 /// 删除字符串中指定的内容,不区分大小写
92 /// </summary>
93 /// <param name="src">要修改的字符串</param>
94 /// <param name="pattern">要删除的正则表达式模式</param>
95 /// <returns>已删除指定内容的字符串</returns>
96 public static string DropIgnoreCase(string src, string pattern)
97 {
98 return ReplaceIgnoreCase(src, pattern, "");
99 }
100
101 /// <summary>
102 /// 替换字符串到数据库可输入模式
103 /// </summary>
104 /// <param name="src">待插入数据库的字符串</param>
105 /// <returns>可插入数据库的字符串</returns>
106 public static string ToSQL(string src)
107 {
108 if (src == null)
109 {
110 return null;
111 }
112 return Replace(src, "", "");
113 }
114
115 /// <summary>
116 /// 去掉html内容中的指定的html标签
117 /// </summary>
118 /// <param name="content">html内容</param>
119 /// <param name="tagName">html标签</param>
120 /// <returns>去掉标签的内容</returns>
121 public static string DropHtmlTag(string content, string tagName)
122 {
123 //去掉<tagname>和</tagname>
124 return DropIgnoreCase(content, "<[/]{0,1}" + tagName + "[^\\>]*\\>");
125 }
126
127 /// <summary>
128 /// 去掉html内容中全部标签
129 /// </summary>
130 /// <param name="content">html内容</param>
131 /// <returns>去掉html标签的内容</returns>
132 public static string DropHtmlTag(string content)
133 {
134 //去掉<*>
135 return Drop(content, "<[^\\>]*>");
136 }
137
138 /// <summary>
139 /// 判断一个数据是不是数字
140 /// </summary>
141 /// <param name="inputData">字符串</param>
142 /// <returns>结果</returns>
143 public static bool IsNumeric(string inputData)
144 {
145 Regex _isNumber = new Regex(@"^\d+{$article$}quot;");
146 Match m = _isNumber.Match(inputData);
147 return m.Success;
148 }
149
150 /// <summary>
151 /// 转换html标签为web页可见内容
152 /// </summary>
153 /// <param name="src"></param>
154 /// <returns></returns>
155 public static string EscapeHtml(string src)
156 {
157 if (src == null)
158 {
159 return null;
160 }
161 string s = src;
162 s = Replace(s, ">", ">");
163 s = Replace(s, "<", "<");
164 return s;
165 }
166
167 /// <summary>
168 /// 将字符串格式化成HTML代码
169 /// </summary>
170 /// <param name="str">要格式化的字符串</param>
171 /// <returns>格式化后的字符串</returns>
172 public static String ToHtml(string str)
173 {
174 if (str == null || str.Equals(""))
175 {
176 return str;
177 }
178
179 StringBuilder sb = new StringBuilder(str);
180 sb.Replace("&", "&");
181 sb.Replace("<", "<");
182 sb.Replace(">", ">");
183 sb.Replace("\r\n", "<br>");
184 sb.Replace("\n", "<br>");
185 sb.Replace("\t", " ");
186 sb.Replace(" ", " ");
187 return sb.ToString();
188 }
189
190
191 /// <summary>
192 /// 将HTML代码转化成文本格式
193 /// </summary>
194 /// <param name="str">要格式化的字符串</param>
195 /// <returns>格式化后的字符串</returns>
196 public static String ToTxt(String str)
197 {
198 if (str == null || str.Equals(""))
199 {
200 return str;
201 }
202
203 StringBuilder sb = new StringBuilder(str);
204 sb.Replace(" ", " ");
205 sb.Replace("<br>", "\r\n");
206 sb.Replace("<", "<");
207 sb.Replace(">", ">");
208 sb.Replace("&", "&");
209 return sb.ToString();
210 }
211 }
212 }
213
2
3 using System;
4 using System.Text;
5 using System.Text.RegularExpressions;
6
7 namespace Example
8 {
9 /// <summary>
10 /// 替换字符串
11 /// </summary>
12 public class StringRepStrs
13 {
14
15
16
17 public StringRepStrs()
18 {
19 }
20
21 #region 普通替换字符串
22
23 /// <summary>
24 /// 普通替换字符串
25
26 /// </summary>
27 /// <param name="src">源字符串</param>
28 /// <param name="pattern">要匹配的正则表达式模式</param>
29 /// <param name="replacement">替换字符串</param>
30 /// <returns>已修改的字符串</returns>
31 public static string Replace(string src, string pattern, string replacement)
32 {
33 return Replace(src, pattern, replacement, RegexOptions.None);
34 }
35
36 #endregion
37
38 #region 正则替换字符串
39
40 /// <summary>
41 /// 正则替换字符串
42
43 /// </summary>
44 /// <param name="src">要修改的字符串</param>
45 /// <param name="pattern">要匹配的正则表达式模式</param>
46 /// <param name="replacement">替换字符串</param>
47 /// <param name="options">匹配模式</param>
48 /// <returns>已修改的字符串</returns>
49 public static string Replace(string src, string pattern, string replacement, RegexOptions options)
50 {
51 Regex regex = new Regex(pattern, options | RegexOptions.Compiled);
52
53 return regex.Replace(src, replacement);
54 }
55
56 #endregion
57
58 #region 不区分大小写替换字符串
59
60 /// <summary>
61 /// 不区分大小写替换字符串
62
63 /// </summary>
64 /// <param name="src">源字符串</param>
65 /// <param name="pattern">要匹配的正则表达式模式</param>
66 /// <param name="replacement">替换字符串</param>
67 /// <returns>已修改的字符串</returns>
68 public static string ReplaceIgnoreCase(string src, string pattern, string replacement)
69 {
70 return Replace(src, pattern, replacement, RegexOptions.IgnoreCase);
71 }
72
73 #endregion
74
75
76
77 /// <summary>
78 /// 删除字符串中指定的内容
79 /// </summary>
80 /// <param name="src">要修改的字符串</param>
81 /// <param name="pattern">要删除的正则表达式模式</param>
82 /// <returns>已删除指定内容的字符串</returns>
83 public static string Drop(string src, string pattern)
84 {
85 return Replace(src, pattern, "");
86 }
87
88
89
90 /// <summary>
91 /// 删除字符串中指定的内容,不区分大小写
92 /// </summary>
93 /// <param name="src">要修改的字符串</param>
94 /// <param name="pattern">要删除的正则表达式模式</param>
95 /// <returns>已删除指定内容的字符串</returns>
96 public static string DropIgnoreCase(string src, string pattern)
97 {
98 return ReplaceIgnoreCase(src, pattern, "");
99 }
100
101 /// <summary>
102 /// 替换字符串到数据库可输入模式
103 /// </summary>
104 /// <param name="src">待插入数据库的字符串</param>
105 /// <returns>可插入数据库的字符串</returns>
106 public static string ToSQL(string src)
107 {
108 if (src == null)
109 {
110 return null;
111 }
112 return Replace(src, "", "");
113 }
114
115 /// <summary>
116 /// 去掉html内容中的指定的html标签
117 /// </summary>
118 /// <param name="content">html内容</param>
119 /// <param name="tagName">html标签</param>
120 /// <returns>去掉标签的内容</returns>
121 public static string DropHtmlTag(string content, string tagName)
122 {
123 //去掉<tagname>和</tagname>
124 return DropIgnoreCase(content, "<[/]{0,1}" + tagName + "[^\\>]*\\>");
125 }
126
127 /// <summary>
128 /// 去掉html内容中全部标签
129 /// </summary>
130 /// <param name="content">html内容</param>
131 /// <returns>去掉html标签的内容</returns>
132 public static string DropHtmlTag(string content)
133 {
134 //去掉<*>
135 return Drop(content, "<[^\\>]*>");
136 }
137
138 /// <summary>
139 /// 判断一个数据是不是数字
140 /// </summary>
141 /// <param name="inputData">字符串</param>
142 /// <returns>结果</returns>
143 public static bool IsNumeric(string inputData)
144 {
145 Regex _isNumber = new Regex(@"^\d+{$article$}quot;");
146 Match m = _isNumber.Match(inputData);
147 return m.Success;
148 }
149
150 /// <summary>
151 /// 转换html标签为web页可见内容
152 /// </summary>
153 /// <param name="src"></param>
154 /// <returns></returns>
155 public static string EscapeHtml(string src)
156 {
157 if (src == null)
158 {
159 return null;
160 }
161 string s = src;
162 s = Replace(s, ">", ">");
163 s = Replace(s, "<", "<");
164 return s;
165 }
166
167 /// <summary>
168 /// 将字符串格式化成HTML代码
169 /// </summary>
170 /// <param name="str">要格式化的字符串</param>
171 /// <returns>格式化后的字符串</returns>
172 public static String ToHtml(string str)
173 {
174 if (str == null || str.Equals(""))
175 {
176 return str;
177 }
178
179 StringBuilder sb = new StringBuilder(str);
180 sb.Replace("&", "&");
181 sb.Replace("<", "<");
182 sb.Replace(">", ">");
183 sb.Replace("\r\n", "<br>");
184 sb.Replace("\n", "<br>");
185 sb.Replace("\t", " ");
186 sb.Replace(" ", " ");
187 return sb.ToString();
188 }
189
190
191 /// <summary>
192 /// 将HTML代码转化成文本格式
193 /// </summary>
194 /// <param name="str">要格式化的字符串</param>
195 /// <returns>格式化后的字符串</returns>
196 public static String ToTxt(String str)
197 {
198 if (str == null || str.Equals(""))
199 {
200 return str;
201 }
202
203 StringBuilder sb = new StringBuilder(str);
204 sb.Replace(" ", " ");
205 sb.Replace("<br>", "\r\n");
206 sb.Replace("<", "<");
207 sb.Replace(">", ">");
208 sb.Replace("&", "&");
209 return sb.ToString();
210 }
211 }
212 }
213
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!