系列文章
C#底层库–记录日志帮助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124187709
C#底层库–MySQLBuilder脚本构建类(select、insert、update、in、带条件的SQL自动生成)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129179216
C#底层库–MySQL数据库访问操作辅助类(推荐阅读)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126886379
C#底层库–XML配置参数读写辅助类(推荐阅读)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129175304
C#底层库–获取文件版本和MD5值
本文链接:https://blog.csdn.net/youcheng_ge/article/details/112513871
C#底层库–FilesHelper文件辅助类(删除目录文件、复制文件到指定目录)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887161
C#底层库–操作Excel帮助类(读取、导出表格)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887445
C#底层库–随机数生成类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126888812
C#底层库–RegexHelper正则表达式辅助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/109745286
C#底层库–CSV和DataTable相互转换
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128804367
C#底层库–Image图片操作类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128805298
C#底层库–JSON帮助类_详细(序列化、反序列化、list、datatable)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128805705
C#底层库–cookie操作辅助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128816347
C#底层库–Session操作辅助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128817096
C#底层库–Image图片操作类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128805298
C#底层库–数据库类型与程序类型转换类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128817610
C#底层库–StringExtension字符串扩展类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129520428
前言
本专栏为【底层库】,主要介绍编程过程中 通用函数。我们将这些通用固化的源码,进行重写、封装、拓展,再进行单元测试、集成测试、beta测试,最终形成通用化模板,这里我们称为“底层库”。
作为研发人员的你,并不需要花大量时间,研究“底层库”的含义,及“底层库”的实现方法。你只需要几行调用代码,就可以解决项目上碰到的难题。而底层库使用方法,本专栏均有详细介绍,也有项目应用场景。
底层库已实现功能:MySQL脚本构建器、MySQL数据库访问操作、参数配置文件读写、加解密算法、日志记录、HTTP通信、Socket通信、API前后端交互、邮件发送、文件操作、配置参数存储、Excel导入导出、CSV和DataTable转换、压缩解压、自动编号、Session操作等。
本专栏会持续更新,不断优化【底层库】,大家有任何问题,可以私信我。本专栏之间关联性较强(我会使用到某些底层库,某些文章可能忽略介绍),如果您对本专栏感兴趣,欢迎关注,我将带你用最简洁的代码,实现最复杂的功能。
一、底层库作用
StringExtension字符串扩展类,用于给字符串两边添加单引号、给字符串两边添加括号、使用And连接两个SQL条件、去掉末尾字符、字符串加指定字符包含、字符串比较、判断字符串是否为空、向右补充字符串长度、向左补充字符串长度、按指定间隔符,连接字符串、按指定间隔符,连接字符串、字符串拆分成list、按指定分割符,字符串拆分成list、截取中间字符串、删除最后结尾的逗号、删除最后结尾指定字符、首字母大写、驼峰命名法(第一个单字以小写字母+第二个单字大写首字母)等。
二、底层库源码
2.1 创建字符串扩展类StringExt.cs
创建字符串扩展类StringExt.cs,复制以下代码。
using System;
using System.Collections.Generic;
using System.Text;
namespace WBF.Utils
{
/// <summary>
/// 字符串扩展类
/// 创建人:gyc
/// 创建人:2010-01-27
/// 说明:用于给字符串两边添加单引号、给字符串两边添加括号、使用And连接两个SQL条件、去掉末尾字符
/// </summary>
public static class StringExtension
{
#region 字符串连接
/// <summary>
/// 给字符串两边添加单引号(同时会将内部单引号替换成双单引号)
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
public static string QuotedStr(this string S)
{
return "'" + S.Replace("'", "''") + "'";
}
/// <summary>
/// 给字符串两边添加括号
/// </summary>
/// <param name="str">字符串</param>
/// <returns>返回带括号的字符串</returns>
public static string BracketStr(this string S)
{
return "(" + S + ")";
}
/// <summary>
/// 字符串加指定字符包含
/// </summary>
/// <param name="s"></param>
/// <param name="a_quot"></param>
/// <returns></returns>
public static string QuotedWith(this string s, string a_quot)
{
return string.Concat(new string[]
{
"'",
a_quot,
s.Replace("'", "''"),
a_quot,
"'"
});
}
/// <summary>
/// 使用And连接两个SQL条件
/// </summary>
/// <param name="strCondition1"></param>
/// <param name="strCondition2"></param>
/// <returns></returns>
public static string ConcatSQL(this string S, string strCondition2)
{
if (S.Trim() == "" || strCondition2.Trim() == "")
{
return S + strCondition2;
}
return S + " and " + strCondition2;
}
#endregion
#region 字符串比较填充
/// <summary>
/// 字符串比较
/// </summary>
/// <param name="s">被比较字符串</param>
/// <param name="s2">比较字符串</param>
/// <returns></returns>
public static bool SameText(this string s, string s2)
{
return string.Compare(s, s2, true) == 0;
}
/// <summary>
/// 判断字符串是否为空
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(this string s)
{
return string.IsNullOrEmpty(s);
}
/// <summary>
/// 向右补充字符串长度
/// </summary>
/// <param name="s"></param>
/// <param name="a_intTotalWidth"></param>
/// <returns></returns>
public static string PadRightCN(this string s, int a_intTotalWidth)
{
int num = 0;
checked
{
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
int num2 = (int)c;
if (num2 > 127)
{
num++;
}
}
return s.PadRight(a_intTotalWidth - num);
}
}
/// <summary>
/// 向左补充字符串长度
/// </summary>
/// <param name="s"></param>
/// <param name="a_intTotalWidth"></param>
/// <returns></returns>
public static string PadLeftCN(this string s, int a_intTotalWidth)
{
int num = 0;
checked
{
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
int num2 = (int)c;
if (num2 > 127)
{
num++;
}
}
return s.PadLeft(a_intTotalWidth - num);
}
}
#endregion
#region 字符串截取
/// <summary>
/// 按指定间隔符,连接字符串
/// </summary>
/// <param name="array">数组</param>
/// <param name="a_strSeparator">间隔符</param>
/// <returns></returns>
public static string BuildWith(this string[] array, string a_strSeparator)
{
string text = "";
string str = "";
for (int i = 0; i < array.Length; i++)
{
string str2 = array[i];
text = text + str + str2;
str = a_strSeparator;
}
return text;
}
/// <summary>
/// 按指定间隔符,连接字符串
/// </summary>
/// <param name="list">list列表</param>
/// <param name="a_strSeparator">间隔符</param>
/// <returns></returns>
public static string BuildWith(this List<string> list, string a_strSeparator)
{
string text = "";
string str = "";
foreach (string current in list)
{
text = text + str + current;
str = a_strSeparator;
}
return text;
}
/// <summary>
/// 字符串拆分成list
/// </summary>
/// <param name="a_strSource">源字段串</param>
/// <returns></returns>
public static List<string> StringSplitToList(string a_strSource)
{
List<string> listField = new List<string>();
string[] array = a_strSource.Split(new char[]
{
',',
';'
});
foreach (string str in array)
{
if (str.Length > 0 && !listField.Contains(str))
{
listField.Add(str);
}
}
return listField;
}
/// <summary>
/// 按指定分割符,字符串拆分成list
/// </summary>
/// <param name="a_strSource">源字段串</param>
/// <param name="a_strSplit">分割字符</param>
/// <returns></returns>
public static List<string> StringSplitToList(string a_strSource, char a_strSplit)
{
List<string> listField = new List<string>();
string[] array = a_strSource.Split(a_strSplit);
foreach (string str in array)
{
if (str.Length > 0 && !listField.Contains(str))
{
listField.Add(str);
}
}
return listField;
}
/// <summary>
/// 截取中间字符串
/// </summary>
/// <param name="a_source">源字符串</param>
/// <param name="a_strStart">开始字符串</param>
/// <param name="a_strEnd">末尾字符串</param>
/// <returns></returns>
public static string SubMiddleString(string a_source, string a_strStart, string a_strEnd)
{
string l_ret = "";
int i = a_source.IndexOf(a_strStart);//空为0
if (i >= 0)
{
int j = a_source.IndexOf(a_strEnd, i + a_strStart.Length);
if (j > 0)
{
l_ret = a_source.Substring(i + a_strStart.Length, j - i - a_strStart.Length);
}
}
return l_ret;
}
#endregion
#region 删除字符串
/// <summary>
/// 删除最后结尾的逗号
/// </summary>
/// <param name="a_strSource">源字符串</param>
/// <returns>string</returns>
public static string DelLastComma(string a_strSource)
{
return a_strSource.Substring(0, a_strSource.LastIndexOf(","));
}
/// <summary>
/// 删除最后结尾指定字符
/// </summary>
/// <param name="a_strSource">源字符串</param>
/// <param name="a_char">字符</param>
/// <returns>string</returns>
public static string DelLastComma(string a_strSource, char a_char)
{
return a_strSource.Substring(0, a_strSource.LastIndexOf(a_char));
}
#endregion
#region 字符串重命名
/// <summary>
/// 首字母大写
/// </summary>
/// <param name="a_strValue"></param>
/// <returns></returns>
public static string ToUpperFirstword(string a_strValue)
{
if (string.IsNullOrEmpty(a_strValue))
{
return string.Empty;
}
else
{
return a_strValue.Substring(0, 1).ToUpper() + a_strValue.Substring(1).ToLower();
}
}
/// <summary>
/// 驼峰命名法(第一个单字以小写字母+第二个单字大写首字母)
/// </summary>
/// <param name="a_strValue"></param>
/// <returns></returns>
public static string ToCamelName(string a_strValue)
{
StringBuilder result = new StringBuilder();
if (string.IsNullOrEmpty(a_strValue))
{
return string.Empty;
}
else if (!a_strValue.Contains("_"))// 不含下划线,仅将首字母大写
{
return ToUpperFirstword(a_strValue);
}
else
{
// 用下划线将原始字符串分割
string[] camels = a_strValue.Split('_');
foreach (string camel in camels)
{
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (string.IsNullOrEmpty(camel))
{
continue;
}
if (result.Length == 0) // 第一个驼峰片段,全部字母都小写
{
result.Append(camel.ToLower());
}
else
{
// 其他的驼峰片段,首字母大写,其它小写
result.Append(camel.Substring(0, 1).ToUpper());
result.Append(camel.Substring(1).ToLower());
}
}
}
return result.ToString();
}
#endregion
}
}
三、调用方法
见《【C#】单号生成器(定义编号规则、流水号、产生业务单号)》文章。
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129129787
四、项目展示
后期补充
五、资源链接
后期补充