zoukankan      html  css  js  c++  java
  • C#自定义的字符串操作增强类的代码

    把做工程过程经常用到的一些代码做个收藏,如下的代码段是关于C#自定义的字符串操作增强类的代码。
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    namespace DotNet.Utilities
    {
    public class StringPlus
    {
    public static List<string> GetStrArray(string str, char speater, bool toLower)
    {
    List<string> list = new List<string>();
    string[] ss = str.Split(speater);
    foreach (string s in ss)
    {
    if (!string.IsNullOrEmpty(s) && s != speater.ToString())
    {
    string strVal = s;
    if (toLower)
    {
    strVal = s.ToLower();
    }
    list.Add(strVal);
    }
    }
    return list;
    }
    public static string[] GetStrArray(string str)
    {
    return str.Split(new Char[] { ',' });
    }
    public static string GetArrayStr(List<string> list, string speater)
    {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < list.Count; i++)
    {
    if (i == list.Count - 1)
    {
    sb.Append(list[i]);
    }
    else
    {
    sb.Append(list[i]);
    sb.Append(speater);
    }
    }
    return sb.ToString();
    }
    public static string GetArrayStr(List<int> list)
    {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < list.Count; i++)
    {
    if (i == list.Count - 1)
    {
    sb.Append(list[i].ToString());
    }
    else
    {
    sb.Append(list[i]);
    sb.Append(",");
    }
    }
    return sb.ToString();
    }
    public static string GetArrayValueStr(Dictionary<int, int> list)
    {
    StringBuilder sb = new StringBuilder();
    foreach (KeyValuePair<int, int> kvp in list)
    {
    sb.Append(kvp.Value + ",");
    }
    if (list.Count > 0)
    {
    return DelLastComma(sb.ToString());
    }
    else
    {
    return "";
    }
    }


    #region 删除最后一个字符之后的字符

    public static string DelLastComma(string str)
    {
    return str.Substring(0, str.LastIndexOf(","));
    }

    public static string DelLastChar(string str, string strchar)
    {
    return str.Substring(0, str.LastIndexOf(strchar));
    }

    #endregion


    public static string ToSBC(string input)
    {
    char[] c = input.ToCharArray();
    for (int i = 0; i < c.Length; i++)
    {
    if (c[i] == 32)
    {
    c[i] = (char)12288;
    continue;
    }
    if (c[i] < 127)
    c[i] = (char)(c[i] + 65248);
    }
    return new string(c);
    }

    public static string ToDBC(string input)
    {
    char[] c = input.ToCharArray();
    for (int i = 0; i < c.Length; i++)
    {
    if (c[i] == 12288)
    {
    c[i] = (char)32;
    continue;
    }
    if (c[i] > 65280 && c[i] < 65375)
    c[i] = (char)(c[i] - 65248);
    }
    return new string(c);
    }

    public static List<string> GetSubStringList(string o_str, char sepeater)
    {
    List<string> list = new List<string>();
    string[] ss = o_str.Split(sepeater);
    foreach (string s in ss)
    {
    if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
    {
    list.Add(s);
    }
    }
    return list;
    }


    #region 将字符串样式转换为纯字符串
    public static string GetCleanStyle(string StrList, string SplitString)
    {
    string RetrunValue = "";
    if (StrList == null)
    {
    RetrunValue = "";
    }
    else
    {
    string NewString = "";
    NewString = StrList.Replace(SplitString, "");
    RetrunValue = NewString;
    }
    return RetrunValue;
    }
    #endregion

    #region 将字符串转换为新样式
    public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
    {
    string ReturnValue = "";
    if (StrList == null)
    {
    ReturnValue = "";
    Error = "请输入需要划分格式的字符串";
    }
    else
    {
    int strListLength = StrList.Length;
    int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
    if (strListLength != NewStyleLength)
    {
    ReturnValue = "";
    Error = "样式格式的长度与输入的字符长度不符,请重新输入";
    }
    else
    {
    string Lengstr = "";
    for (int i = 0; i < NewStyle.Length; i++)
    {
    if (NewStyle.Substring(i, 1) == SplitString)
    {
    Lengstr = Lengstr + "," + i;
    }
    }
    if (Lengstr != "")
    {
    Lengstr = Lengstr.Substring(1);
    }
    string[] str = Lengstr.Split(',');
    foreach (string bb in str)
    {
    StrList = StrList.Insert(int.Parse(bb), SplitString);
    }
    ReturnValue = StrList;
    Error = "";
    }
    }
    return ReturnValue;
    }
    #endregion

    public static string[] SplitMulti(string str, string splitstr)
    {
    string[] strArray = null;
    if ((str != null) && (str != ""))
    {
    strArray = new Regex(splitstr).Split(str);
    }
    return strArray;
    }
    public static string SqlSafeString(string String, bool IsDel)
    {
    if (IsDel)
    {
    String = String.Replace("'", "");
    String = String.Replace(""", "");
    return String;
    }
    String = String.Replace("'", "&#39;");
    String = String.Replace(""", "&#34;");
    return String;
    }

    #region 获取正确的Id,如果不是正整数,返回0
    public static int StrToId(string _value)
    {
    if (IsNumberId(_value))
    return int.Parse(_value);
    else
    return 0;
    }
    #endregion
    #region 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
    public static bool IsNumberId(string _value)
    {
    }
    #endregion
    #region 快速验证一个字符串是否符合指定的正则表达式。
    public static bool QuickValidate(string _express, string _value)
    {
    if (_value == null) return false;
    Regex myRegex = new Regex(_express);
    if (_value.Length == 0)
    {
    return false;
    }
    return myRegex.IsMatch(_value);
    }
    #endregion
    }
    }





  • 相关阅读:
    php函数注释
    组件化开发
    7.哪些工具可以帮助查找bug或进行静态分析
    6.Python中内存是如何管理的?
    5.Python是怎么解释的?
    4.pickling 和unpickling是什么?
    3.PEP 8是什么?
    2.Python是什么?使用Python的好处是什么?
    Redis介绍及字符串操作
    字符串转换为二进制
  • 原文地址:https://www.cnblogs.com/codeoldman/p/10318950.html
Copyright © 2011-2022 走看看