文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>string method extension

string method extension

时间:2010-09-22  来源:IT老民工

代码
/// <summary>
/// Summary description for StringExtensions
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Will return false if str is null or empty.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool SmtContains(this string str1, string str)
{
return (string.IsNullOrEmpty(str)) ? false : str1.Contains(str);
}

/// <summary>
/// Retrieves a substring from this instance. The substring starts at 0
/// and has a specified length.
/// </summary>
/// <param name="length">The number of characters in the substring.</param>
/// <returns>A System.String equivalent to the substring of length length that begins
/// at startIndex in this instance.</returns>
public static string FitSubstring(this string str, int length)
{
return (str.Length > length) ? str.Substring(0, length) : str;
}

/// <summary>
/// Remove characters in string.
/// </summary>
/// <param name="str">Like "-'|/?*"</param>
/// <returns></returns>
public static string RemoveChars(this string str1, string str)
{
string sRtn = str1;
char[] aChar = str.ToCharArray();
string sItem = string.Empty;
foreach (char item in aChar)
{
sItem
= item.ToString();
sRtn
= sRtn.Contains(sItem) ? sRtn.Replace(sItem, string.Empty) : sRtn;
}
return sRtn;
//return Regex.Replace(str1, str, string.Empty, RegexOptions.Compiled);
}

/// <summary>
/// add css for string pattern.
/// </summary>
/// <param name="sPattern"></param>
/// <param name="sCss">like "color: red;"</param>
/// <returns></returns>
public static string ChangeStyle(this string str, string sPattern, string sCss)
{
if (!str.Contains(sPattern)) { return str; }
string sTmp = string.Format("<span style='{1}'>{0}</span>", sPattern, sCss);
return str.Replace(sPattern, sTmp);
}

///
/// Summary:
/// Indicates whether the specified System.String object is null or an System.String.Empty
/// string.
/// Returns:
/// true if the value parameter is null or an empty string (""); otherwise, false.
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载