ASP.NET----サイト文字列操作類
9508 ワード
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
", "<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
");
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
document.write(\"", source.Replace("\\", "\\\\")
.Replace("/", "\\/")
.Replace("'", "\\'")
.Replace("\"", "\\\"")
.Split(new char[] { '\r', '
' }, 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', '
' }, StringSplitOptions.RemoveEmptyEntries)
));
}
/// <summary>
///
/// </summary>
/// <param name="theString"> </param>
/// <returns> </returns>
public static string FilterSymbol(string theString)
{
string[] aryReg = { "'", "\"", "\r", "
", "<", ">", "%", "?", ",", ".", "=", "-", "_", ";", "|", "[", "]", "&", "/" };
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", "
", "<", ">" };
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
}
}