C# 半角?全角
/// <summary> /// 将资料表中已修改的资料行数据去左右空格、全角转半角 /// </summary> public sealed class FieldFitString { const string _SEPERATOR = "&^*"; /// <summary> /// 将资料表中已修改的资料行数据去左右空格、全角转半角 /// </summary> /// <param name="dt"></param> public static void FitString(DataTable dt) { DataTable tmp = dt.GetChanges(); if (tmp == null) return; //枚举Columns,仅处理字符类型的Field //枚举DataRow,仅处理新增、修改状态的记录 string tmpFileNames = string.Empty; //取出字段 for (int i = 0; i < dt.Columns.Count; i++) { if (dt.Columns[i].DataType == Type.GetType("System.String")) { tmpFileNames += _SEPERATOR + dt.Columns[i].Caption; } } string[] FileNames = tmpFileNames.Split(new string[] { _SEPERATOR }, StringSplitOptions.RemoveEmptyEntries); //仅处理已修改的资料行 foreach (DataRow dr in dt.Rows) { if (dr.RowState == DataRowState.Added || dr.RowState == DataRowState.Modified) { foreach (string filename in FileNames) { dr[filename] = RemoveChar(ConvertEx.ToString(dr[filename])); } } } } /// <summary> /// 将资料表中已修改的资料行数据去左右空格、全角转半角 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string RemoveChar(string str) { if (String.IsNullOrEmpty(str)) return str; try { string NewStr = string.Empty; char[] c = str.ToCharArray(); for (int i = 0; i < c.Length; i++) { byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1); if (b.Length == 2) { //全角 if (b[1] == 255 || b[1] == 48) { b[0] = (byte)(b[0] + 32); b[1] = 0; NewStr += System.Text.Encoding.Unicode.GetChars(b)[0]; } else { NewStr += c[i]; } } } return NewStr.Trim();//去左右空格 } catch { return str; } } }
慎于行,敏于思!GGGGGG