天天看點

字元串操作輔助類

  **

  /// 字元串操作輔助類

  ///

  public class StringUtil

  {

  一些基本的符号常量#region 一些基本的符号常量

  /**

  /// 點符号 .

  ///

  public const string Dot = ".";

  /**

  /// 下劃線 _

  ///

  public const string UnderScore = "_";

  /**

  /// 逗号加空格 ,

  ///

  public const string CommaSpace = ", ";

  /**

  /// 逗号 ,

  ///

  public const string Comma = ",";

  /**

  /// 左括号 (

  ///

  public const string OpenParen = "(";

  /**

  /// 右括号 )

  ///

  public const string ClosedParen = ")";

  /**

  /// 單引号 '

  ///

  public const string SingleQuote = "\'";

  /**

  /// 斜線 \

  ///

  public const string Slash = @"\";

  #endregion

  private StringUtil()

  {

  }

  /**

  /// 移除空格并首字母小寫的Camel樣式

  ///

  ///

  ///

  public static string ToCamel(string name)

  {

  string clone = name.TrimStart('_');

  clone = RemoveSpaces(ToProperCase(clone));

  return String.Format("{0}{1}", Char.ToLower(clone[0]), clone.Substring(1, clone.Length - 1));

  }

  /**

  /// 移除空格并首字母大寫的Pascal樣式

  ///

  ///

  ///

  public static string ToCapit(string name)

  {

  string clone = name.TrimStart('_');

  return RemoveSpaces(ToProperCase(clone));

  }

  /**

  /// 移除最後的字元

  ///

  ///

  ///

  public static string RemoveFinalChar(string s)

  {

  if (s.Length > 1)

  {

  s = s.Substring(0, s.Length - 1);

  }

  return s;

  }

  /**

  /// 移除最後的逗号

  ///

  ///

  ///

  public static string RemoveFinalComma(string s)

  {

  if (s.Trim().Length > 0)

  {

  int c = s.LastIndexOf(",");

  if (c > 0)

  {

  s = s.Substring(0, s.Length - (s.Length - c));

  }

  }

  return s;

  }

  /**

  /// 移除字元中的空格

  ///

  ///

  ///

  public static string RemoveSpaces(string s)

  {

  s = s.Trim();

  s = s.Replace(" ", "");

  return s;

  }

  /**

  /// 字元串首字母大寫

  ///

  ///

  ///

  public static string ToProperCase(string s)

  {

  string revised = "";

  if (s.Length > 0)

  {

  if (s.IndexOf(" ") > 0)

  {

  revised = Strings.StrConv(s, VbStrConv.ProperCase, 1033);

  }

  else

  {

  string firstLetter = s.Substring(0, 1).ToUpper(new CultureInfo("en-US"));

  revised = firstLetter + s.Substring(1, s.Length - 1);

  }

  }

  return revised;

  }

  /**

  /// 判斷字元是否NULL或者為空

  ///

  public static bool IsNullOrEmpty(string value)

  {

  if (value == null || value == string.Empty)

  {

  return true;

  }

  return false;

  }

  }

  字元串操作輔助類測試代碼

  public class TestStringUtil

  {

  public static string Execute()

  {

  string value = "test String,";

  string result = string.Empty;

  result += "使用StringUtil字元串操作輔助類:" + "\r\n";

  result += "原字元串為:" + value + "\r\n";

  result += "StringUtil.IsNullOrEmpty:" + StringUtil.IsNullOrEmpty(value) + "\r\n";

  result += "StringUtil.ToCamel:" + StringUtil.ToCamel(value) + "\r\n";

  result += "StringUtil.ToCapit:" + StringUtil.ToCapit(value) + "\r\n";

  result += "StringUtil.RemoveSpaces:" + StringUtil.RemoveSpaces(value) + "\r\n";

  result += "StringUtil.RemoveFinalChar:" + StringUtil.RemoveFinalChar(value) + "\r\n";

  result += "StringUtil.ToProperCase:" + StringUtil.ToProperCase(value) + "\r\n";

  result += "\r\n\r\n";

  return result;

  }