public string GetFormatString(string dayUnit, string hourUnit, string minuteUnit, string secondUnit) { using (var scope = new StringBuilderScope()) { if (this.day != 0) { scope.stringBuilder.Append(this.day + dayUnit); } if (scope.stringBuilder.Length != 0 || this.hour != 0) { scope.stringBuilder.Append(this.hour + hourUnit); } if (scope.stringBuilder.Length != 0 || this.minute != 0) { scope.stringBuilder.Append(this.minute + minuteUnit); } if (scope.stringBuilder.Length != 0 || this.second != 0) { scope.stringBuilder.Append(this.second + secondUnit); } var result = scope.stringBuilder.ToString(); return(result); } }
/// <summary> /// 将seconds转为hh:mm:ss /// 倒计时经常使用 /// </summary> /// <param name="seconds"></param> /// <param name="hCount">小时那位需要至少保留多少位,即1的时候显不显示为01</param> /// <param name="isZeroIgnore">是否小时或分钟或秒为0的时候忽视该位,高位有的话还是会保留地位的,即使低位为0</param> /// <returns></returns> public static string SecondToStringHHmmss(long seconds, int hCount = 2, bool isZeroIgnore = false) { using (var scope = new StringBuilderScope()) { long HH = seconds / 3600; isZeroIgnore = isZeroIgnore && HH == 0; if (!isZeroIgnore) { scope.stringBuilder.Append(HH.ToString().FillHead(hCount, CharConst.Char_0) + StringConst.String_Colon); } long mm = (seconds % 3600) / 60; isZeroIgnore = isZeroIgnore && mm == 0; if (isZeroIgnore) { scope.stringBuilder.Append(mm.ToString().FillHead(2, CharConst.Char_0) + StringConst.String_Colon); } long ss = seconds % 60; isZeroIgnore = isZeroIgnore && ss == 0; if (isZeroIgnore) { scope.stringBuilder.Append(ss.ToString().FillHead(2, CharConst.Char_0)); } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 将s从fromIndex开始,到toIndex(不包括toIndex)结束的元素转为字符串连接起来,元素之间用n连接(最后的元素后面不加n) /// 例如:object[] s={"aa","bb","cc"} split="DD" return "aaDDbbDDcc" /// </summary> public static string Concat <T>(this IList <T> self, int fromIndex, int toIndex, string separator) { if (fromIndex < 0 || toIndex > self.Count || toIndex - fromIndex < 0) { throw new IndexOutOfRangeException(); } using (var scope = new StringBuilderScope()) { if (toIndex - fromIndex <= 0) { return(scope.stringBuilder.ToString()); } for (int i = fromIndex; i < self.Count && i <= toIndex; i++) { string value = self[i].ToString(); if (i == fromIndex) { scope.stringBuilder.Append(value); } else { scope.stringBuilder.Append(separator + value); } } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 将s重复n次返回 /// </summary> public static string Join(this string self, int n) { using (var scope = new StringBuilderScope()) { for (var i = 0; i < n; i++) { scope.stringBuilder.Append(self); } return(scope.stringBuilder.ToString()); } }
/// <summary> /// MD5加密 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string Encrypt(string s) { var md5Hash = MD5.Create(); var datas = md5Hash.ComputeHash(s.GetBytes()); using (var scope = new StringBuilderScope()) { for (var i = 0; i < datas.Length; i++) { var data = datas[i]; scope.stringBuilder.Append(data.ToString(StringConst.String_x2)); } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 后补齐字符串.若src长度不足len,则在src后面用c补足len长度,否则直接返回src /// </summary> public static string FillEnd(this string self, int len, char c) { if (self.Length >= len) { return(self); } using (var scope = new StringBuilderScope()) { scope.stringBuilder.Append(self); for (var i = 0; i < len - self.Length; i++) { scope.stringBuilder.Append(c); } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 从根物体到当前物体的全路径, 以/分隔 /// </summary> public static string GetFullPath(Transform transform, Transform rootTransform = null, string separator = StringConst.String_Slash) { using (var scope = new StringBuilderScope()) { scope.stringBuilder.Append(transform.name); Transform iterator = transform.parent; while (iterator != rootTransform || iterator != null) { scope.stringBuilder.Insert(0, separator); scope.stringBuilder.Insert(0, iterator.name); iterator = iterator.parent; } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 由末尾向前,获取src的第一个数字(可能由多个字符组成) /// 如:fg125abc456,则得出来是"456";fg125abc456fd,则得出来是"" /// </summary> public static string GetDigitEnd(this string self) { self = self.Trim(); using (var scope = new StringBuilderScope(self.Length)) { for (var i = self.Length - 1; i >= 0; i--) { if (!char.IsDigit(self[i])) { break; } scope.stringBuilder.Insert(0, self[i]); } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 获取src的第一个数字(可能由多个字符组成) /// 如:123df58f,则返回"123";abc123则返回"" /// </summary> public static string GetDigitStart(this string self) { self = self.Trim(); using (var scope = new StringBuilderScope(self.Length)) { foreach (var t in self) { if (!char.IsDigit(t)) { break; } scope.stringBuilder.Append(t); } return(scope.stringBuilder.ToString()); } }
public static string ToString(params object[] objs) { using (var scope = new StringBuilderScope()) { for (int i = 0; i < objs.Length; i++) { var obj = objs[i]; if (i == objs.Length - 1) { scope.stringBuilder.Append(obj); } else { scope.stringBuilder.Append(obj + StringConst.String_Space); } } return(scope.stringBuilder.ToString()); } }
/// <summary> /// 读取文件file,返回字符串内容 /// </summary> /// <param name="self"></param> /// <returns></returns> public static string ReadTextFile(this FileInfo self) { var fr = new StreamReader(self.FullName); using (var scope = new StringBuilderScope()) { var chars = new char[1024]; try { int n; while ((n = fr.Read(chars, 0, chars.Length)) != 0) { scope.stringBuilder.Append(chars, 0, n); } return(scope.stringBuilder.ToString()); } finally { fr.Close(); } } }
public static (bool, string) GetRelativePath(Transform transform, Transform parentTransform = null) { using (var scope = new StringBuilderScope()) { scope.stringBuilder.Append(transform.name); if (transform == parentTransform) { return(true, scope.stringBuilder.ToString()); } Transform parentNode = transform.parent; while (!(parentNode == null || parentNode == parentTransform)) { scope.stringBuilder.Insert(0, parentNode.name + StringConst.String_Slash); parentNode = parentNode.parent; } bool isFound = parentTransform == parentNode; if (isFound && parentNode != null) { scope.stringBuilder.Insert(0, parentNode.name + StringConst.String_Slash); } return(isFound, scope.stringBuilder.ToString()); } }
public static string ReplaceAll(this string s, string pattern, Func <string, string> replaceFunc = null) { using (var scope = new StringBuilderScope()) { MatchCollection matchCollection = Regex.Matches(s, pattern); int lastEndIndex = -1; for (int i = 0; i < matchCollection.Count; i++) { int startIndex = matchCollection[i].Index; string value = matchCollection[i].Value; int endIndex = startIndex + value.Length - 1; value = replaceFunc == null ? value : replaceFunc(value); scope.stringBuilder.Append(s.Substring(lastEndIndex + 1, startIndex - (lastEndIndex + 1))); scope.stringBuilder.Append(value); lastEndIndex = endIndex; } if (lastEndIndex != s.Length - 1) { scope.stringBuilder.Append(s.Substring(lastEndIndex + 1)); } return(scope.stringBuilder.ToString()); } }
public static string ToString2(this ICollection self, bool isFillStringWithDoubleQuote = false) { bool isFirst = true; using (var scope = new StringBuilderScope()) { switch (self) { case Array _: scope.stringBuilder.Append(StringConst.String_LeftRoundBrackets); break; case IList _: scope.stringBuilder.Append(StringConst.String_LeftSquareBrackets); break; case IDictionary _: scope.stringBuilder.Append(StringConst.String_LeftCurlyBrackets); break; } if (self is IDictionary dictionary) { foreach (var key in dictionary.Keys) { if (isFirst) { isFirst = false; } else { scope.stringBuilder.Append(StringConst.String_Comma); } scope.stringBuilder.Append(key.ToString2(isFillStringWithDoubleQuote)); scope.stringBuilder.Append(StringConst.String_Colon); object value = dictionary[key]; scope.stringBuilder.Append(value.ToString2(isFillStringWithDoubleQuote)); } } else //list { foreach (var o in self) { if (isFirst) { isFirst = false; } else { scope.stringBuilder.Append(StringConst.String_Comma); } scope.stringBuilder.Append(o.ToString2(isFillStringWithDoubleQuote)); } } switch (self) { case Array _: scope.stringBuilder.Append(StringConst.String_RightRoundBrackets); break; case IList _: scope.stringBuilder.Append(StringConst.String_RightSquareBrackets); break; case IDictionary _: scope.stringBuilder.Append(StringConst.String_RightCurlyBrackets); break; } return(scope.stringBuilder.ToString()); } }