**
/// 字符串操作辅助类
///
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;
}
/// 字符串操作辅助类
///
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;
}