由于格式是已知的,不应该改变 Substring 应该适合你
string data = "bsarbirthd0692";
string name, desc, date;
name = data.Substring(0, 4);
desc = data.Substring(4, 6);
date = data.SubString(10);
编辑
您还可以创建扩展方法来做任何您想做的事情。这显然比之前的建议更复杂
public static class StringExtension
{
/// <summary>
/// Returns a string array of the original string broken apart by the parameters
/// </summary>
/// <param name="str">The original string</param>
/// <param name="obj">Integer array of how long each broken piece will be</param>
/// <returns>A string array of the original string broken apart</returns>
public static string[] ParseFormat(this string str, params int[] obj)
{
int startIndex = 0;
string[] pieces = new string[obj.Length];
for (int i = 0; i < obj.Length; i++)
{
if (startIndex + obj[i] < str.Length)
{
pieces[i] = str.Substring(startIndex, obj[i]);
startIndex += obj[i];
}
else if (startIndex + obj[i] >= str.Length && startIndex < str.Length)
{
// Parse the remaining characters of the string
pieces[i] = str.Substring(startIndex);
startIndex += str.Length + startIndex;
}
// Remaining indexes, in pieces if they're are any, will be null
}
return pieces;
}
}
用法一:
string d = "bsarbirthd0692";
string[] pieces = d.ParseFormat(4,6,4);
结果:
用法2:
string d = "bsarbirthd0692";
string[] pieces = d.ParseFormat(4,6,4,1,2,3);
结果: