首页 > 编程 > .NET > 正文

asp.net字符串处理类代码

2024-07-10 12:41:23
字体:
来源:转载
供稿:网友
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace StringClass
{
public class StringHelper
{
/// <summary>
/// 去掉字符串中的所有空格
/// </summary>
/// <param name="_str"></param>
/// <returns></returns>
public static string ReMoveBlank(string _str)
{
string strTemp = "";
CharEnumerator CEnumerator = _str.GetEnumerator();
while (CEnumerator.MoveNext())
{
byte[] array = new byte[1];
array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
int asciicode = (short)(array[0]);
if (asciicode != 32)
{
strTemp += CEnumerator.Current.ToString();
}
}
return strTemp;
}
/// <summary>
/// 截取字符串并限制字符串长度,多于给定的长度+。。。
/// </summary>
/// <param name="str">待截取的字符串</param>
/// <param name="len">每行的长度,多于这个长度自动换行</param>
/// <param name="max">输出字符串最大的长度</param>
/// <returns></returns>
public static string CutStr(string str, int len, int max)
{
string s = "";
string sheng = "";
if (str.Length > max)
{
str = str.Substring(0, max);
sheng = "";
}
for (int i = 0; i < str.Length; i++)
{
int r = i % len;
int last = (str.Length / len) * len;
if (i != 0 && i <= last)
{
if (r == 0)
{
s += str.Substring(i - len, len) + "<br>";
}
}
else if (i > last)
{
s += str.Substring(i - 1);
break;
}
}
return s + sheng;
}
/// <summary>
/// 截取字符串,不限制字符串长度
/// </summary>
/// <param name="str">待截取的字符串</param>
/// <param name="len">每行的长度,多于这个长度自动换行</param>
/// <returns></returns>
public static string CutStr(string str, int len)
{
string s = "";
for (int i = 0; i < str.Length; i++)
{
int r = i % len;
int last = (str.Length / len) * len;
if (i != 0 && i <= last)
{
if (r == 0)
{
s += str.Substring(i - len, len) + "<br>";
}
}
else if (i > last)
{
s += str.Substring(i - 1);
break;
}
}
return s;
}
public static string PartSubString(string str, int len)
{
if (str.Length > len)
{
return str.Substring(0, len) + "...";
}
return str;
}
/// <summary>
///这个方法确保用户的输入不是恶毒的
/// </summary>
/// <param name="text">输入字符串</param>
/// <param name="maxLength">最大长度</param>
/// <returns>转换后的字符串</returns>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表