CloudBuilder/CloudBuilder.Core/Utility/QuotedString.cs
owenchen 1c0c83af5f ow
2026-05-12 10:09:31 +08:00

127 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System
{
public class QuotedString
{
public static string ToQuotedString(string[] vs)
{
if (vs == null || vs.Length == 0) return null;
return ToQuotedDelimitedList(vs);
}
public static string ToQuotedString(int[] vs)
{
if (vs == null || vs.Length == 0) return null;
return ToQuotedDelimitedList(vs.Select(x => x.ToString()).ToArray()); ;
}
public const char DELIMITER_CHAR = ',';
/// <summary>
/// 字符串安全转数组,分隔符','
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string[] DelimiteredListToArray(string t)
{
return DelimiteredListToArray(t, DELIMITER_CHAR);
}
public static string[] DelimiteredListToArray(string t, char delim)
{
if (string.IsNullOrEmpty(t)) return null;
return t.Split(delim);
}
/// <summary>
/// 字符串安全转数组,分隔符','
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static List<string> DelimiteredListToList(string t)
{
return DelimiteredListToList(t, DELIMITER_CHAR);
}
public static List<string> DelimiteredListToList(string t, char delim)
{
if (string.IsNullOrEmpty(t)) return null;
List<string> l = new List<string>();
l.AddRange(DelimiteredListToArray(t, delim));
return l;
}
/// <summary>
/// 数组变成字符串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToDelimiteredList(string[] s)
{
return ToDelimiteredList(s, DELIMITER_CHAR);
}
public static string ToQuotedDelimitedList(string[] s)
{
StringBuilder b = new StringBuilder();
foreach (string t in s)
{
if (b.Length > 0) b.Append(DELIMITER_CHAR);
b.Append('\'' + t + '\'');
}
return b.ToString();
}
public static string ToDelimiteredList(string[] s, char delim)
{
StringBuilder b = new StringBuilder();
foreach (string t in s)
{
b.Append(delim + t);
}
string r = b.ToString();
if (r.StartsWith(delim.ToString())) r = r.Substring(1);
return r;
}
public static string CreateRandomNumber(int NumCount)
{
string text = "0,1,2,3,4,5,6,7,8,9";
string[] array = text.Split(',');
string text2 = "";
int num = -1;
Random random = new Random();
for (int i = 0; i < NumCount; i++)
{
if (num != -1)
{
random = new Random(i * num * (int)DateTime.Now.Ticks);
}
int num2 = random.Next(array.Length - 1);
if (num == num2)
{
return CreateRandomNumber(NumCount);
}
num = num2;
text2 += array[num2];
}
return text2;
}
}
}