C# FieldFitString类:将资料表(DataTable)已修改的资料行数据去左右空格、全角转半角
C# FieldFitString类:将资料表(DataTable)已修改的资料行数据去左右空格、全角转半角
扫一扫加作者微信
FieldFitString类
C# Code:
using System;
using System.Data;
namespace CSFrameworkV5.Common
{
/// <summary>
/// 将资料表(DataTable)已修改的资料行数据去左右空格、全角转半角
/// </summary>
public sealed class FieldFitString
{
/// <summary>
/// 将资料表中已修改的资料行数据去左右空格、全角转半角
/// </summary>
/// <param name="dt"></param>
public static void FitString(DataTable dt)
{
//仅处理已修改的资料行
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState == DataRowState.Added || dr.RowState == DataRowState.Modified)
{
foreach (DataColumn C in dt.Columns)
{
if (C.DataType == Type.GetType("System.String"))
dr[C.ColumnName] = RemoveChar(ConvertEx.ToString(dr[C.ColumnName]));
}
}
}
}
/// <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;
}
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
using System.Data;
namespace CSFrameworkV5.Common
{
/// <summary>
/// 将资料表(DataTable)已修改的资料行数据去左右空格、全角转半角
/// </summary>
public sealed class FieldFitString
{
/// <summary>
/// 将资料表中已修改的资料行数据去左右空格、全角转半角
/// </summary>
/// <param name="dt"></param>
public static void FitString(DataTable dt)
{
//仅处理已修改的资料行
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState == DataRowState.Added || dr.RowState == DataRowState.Modified)
{
foreach (DataColumn C in dt.Columns)
{
if (C.DataType == Type.GetType("System.String"))
dr[C.ColumnName] = RemoveChar(ConvertEx.ToString(dr[C.ColumnName]));
}
}
}
}
/// <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;
}
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网