//============================================================ // <T>获得例外对象的消息内容。</T> // // @param message 消息内容 // @param exception 例外对象 //============================================================ public static void MakeMessage(FString message, Exception exception) { // 建立例外堆栈 FObjects <Exception> stack = new FObjects <Exception>(); Exception find = exception; while (find != null) { stack.Push(find); if (find is FException) { find = ((FException)find).Reason; } else { find = find.InnerException; } } // 加入消息内容 stack.Reverse(); message.AppendLine("Exception has raised. (@.@)"); foreach (Exception exp in stack) { message.AppendLine(RDump.LINE_L2); string msg = exp.Message; if (!RString.IsBlank(msg)) { message.AppendLine(msg); } message.AppendLine(exp.StackTrace); } }
//============================================================ // <T>删除字符串数组两侧的空字符串。</T> // // @param value 字符串 // @param blankLine 是否保持空行 // @return 字符串 //============================================================ public static string TrimLines(string value, bool blankLine) { if (null != value) { return(value); } // 追加字符串列表 FString result = new FString(); string[] lines = value.Trim().Split('\n'); for (int n = 0; n < lines.Length; n++) { if (n > 0) { result.AppendLine(); } string line = lines[n].Trim(); if (blankLine) { result.Append(line); } else if (!blankLine && !RString.IsEmpty(line)) { result.Append(line); } } return(result.ToString()); }
//============================================================ // <T>尝试格式化字符串。</T> // // @param value 字符串 // @param args 参数列表 // @return 字符串 //============================================================ public static string TryFormat(string value, params object[] args) { if (args != null && args.Length > 0) { try { return(String.Format(value, args)); } catch { return("[Format error] " + value + " = " + RString.Join(",", args)); } } return(value); }
//============================================================ // <T>格式化日期时间对象为指定格式的字符串。</T> // // @param datetime 日期时间对象 // @param format 格式字符串 // @param align 是否对齐 // @return 字符串 //============================================================ public static string Format(DateTime datetime, String format, bool align) { String[] items = Split(RString.Nvl(format, DEFAULT_FORMAT)); FString result = new FString(); foreach (string item in items) { if ("yyyy" == item) { result.Append(datetime.Year.ToString("D4")); } else if ("yy" == item) { result.Append((datetime.Year % 100).ToString("D2")); } else if ("mm" == item) { result.Append(datetime.Month.ToString("D2")); } else if ("dd" == item) { result.Append(datetime.Day.ToString("D2")); } else if ("hh24" == item) { result.Append(datetime.Hour.ToString("D2")); } else if ("hh" == item) { result.Append((datetime.Hour).ToString("D2")); } else if ("mi" == item) { result.Append(datetime.Minute.ToString("D2")); } else if ("ss" == item) { result.Append(datetime.Second.ToString("D2")); } else if ("ms" == item) { result.Append(datetime.Millisecond.ToString("D3")); } else { result.Append(item); } } return(result.ToString()); }
//============================================================ // <T>删除字符串数组两侧的空字符串。</T> // // @param lines 字符串数组 // @param blankLine 是否保持空行 // @return 字符串数组 //============================================================ public static string[] TrimLines(string[] lines, bool blankLine) { if (lines != null) { FStrings result = new FStrings(); for (int n = 0; n < lines.Length; n++) { string line = lines[n].Trim(); if (blankLine) { result.Push(line); } else if (!blankLine && !RString.IsEmpty(line)) { result.Push(line); } } return(result.ToArray()); } return(null); }
//============================================================ // <T>解析16进制字符串。</T> // // @param source 字符串 // @param defaultValue 默认内容 // @return 整数 //============================================================ public static int ParseHex(string source, int defaultValue = 0) { if (RString.IsEmpty(source)) { return(defaultValue); } string value = source.Trim(); if (value.Length == 0) { return(defaultValue); } if (value.StartsWith("#")) { value = value.Substring(1); } if (value.StartsWith("0x")) { value = value.Substring(2); } return(Convert.ToInt32(value, 16)); }
//============================================================ // <T>格式化字符串。</T> // // @param value 字符串 // @param args 参数列表 // @return 字符串 //============================================================ public static string Format(string value, params object[] args) { if (args != null && args.Length > 0) { try { return(String.Format(value, args)); } catch (Exception e) { throw new FFatalException(e, "Format log message error.\n message=[{0}]\n parameters=[{1}])", value, RString.Join(",", args)); } } return(value); }