tieuquyngok1995 / ToolSupportUchida

1 stars 1 forks source link

util #6

Closed tieuquyngok1995 closed 3 years ago

tieuquyngok1995 commented 3 years ago

using System; using System.Collections.Generic; using System.Text.RegularExpressions;

namespace ToolDatabase.Utils { static class CUtils {

region Format String

    public static string ReplaceFormat(string str, string oldValue, string newValue, string formatValue = "")
    {
        try
        {
            if (string.IsNullOrEmpty(formatValue))
            {
                return str.Replace(oldValue, newValue);
            }
            else
            {
                return str.Replace(string.Format(oldValue, formatValue), string.Format(newValue, formatValue));
            }
        }
        catch (Exception)
        {
            return str;
        }
    }

    public static string ReplaceFormatText(string str, string oldValue, string oldFormat, string newValue, string newFormat = "")
    {
        try
        {
            if (string.IsNullOrEmpty(newFormat))
            {
                return str.Replace(string.Format(oldFormat, oldValue), string.Format(oldFormat, newValue));
            }
            else
            {
                return str.Replace(string.Format(oldFormat, oldValue), string.Format(newFormat, newValue));
            }
        }
        catch (Exception)
        {
            return str;
        }
    }

    public static bool ConvertStringToBoolean(string text)
    {
        bool result = false;
        string conditional = string.Empty;
        Dictionary<string, string> lstDic = ConvertStringConditionalToLsit(text);

        foreach (KeyValuePair<string, string> item in lstDic)
        {
            if (string.Empty.Equals(item.Key))
            {
                result = bool.Parse(item.Value);
            }
            else
            {
                conditional = Regex.Replace(item.Key, CONSTANTS.CONST_REGEX_KEY_LIST_DIC, string.Empty).Trim();
                result = CompareConditional(result, conditional, bool.Parse(item.Value));
            }
        }

        return result;
    }

    private static Dictionary<string, string> ConvertStringConditionalToLsit(string text)
    {
        text = text.Replace(CONSTANTS.CONST_STRING_CHECK_ELSE_IF, string.Empty)
                   .Replace(CONSTANTS.CONST_STRING_CHECK_IF, string.Empty)
                   .Replace(CONSTANTS.CONST_STRING_CHECK_THEN, string.Empty).Trim();
        string key = string.Empty;
        string resultCompare = string.Empty;

        bool isBrace = false;

        Dictionary<string, string> lstDic = new Dictionary<string, string>();
        lstDic.Add(key, string.Empty);

        string[] arrText = text.Split(CONSTANTS.CONST_CHAR_SPACE);
        int index = 0;

        foreach (string item in arrText)
        {
            index++;

            if (item.Contains("("))
            {
                isBrace = true;
            }

            if (item.ToUpper().Equals(CONSTANTS.CONST_STRING_UPPER_AND))
            {
                if (isBrace)