我想知道在.NET中是否有内置的解析字符串位的方法。
例如,我有以下字符串:
"bsarbirthd0692"
由以下将与数据交叉引用的部分组成:
Indexes Purpose
0-3 (name)
4-9 (description)
10-13 (date mm-yy)
我希望有一些像土生土长的东西:
string name, desc, date;
string.ParseFormat("{0:4}{1:5}{2:4}", "bsarbirthd0692", out name, out desc, out date);
在.NET或流行的库中是否有以本机方式执行此操作?
答案 0 :(得分:50)
由于格式已知,因此不应更改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;
}
}
用法1:
string d = "bsarbirthd0692";
string[] pieces = d.ParseFormat(4,6,4);
结果:
用法2:
string d = "bsarbirthd0692";
string[] pieces = d.ParseFormat(4,6,4,1,2,3);
结果:
答案 1 :(得分:16)
您可以将Regexp用于此
string str= "bsarbirthd0692";
var regex = "(?<name>.{4})(?<desc>.{6})(?<date>.{4})";
MatchCollection matches = Regex.Matches(str, regex);
foreach(Match m in matches){
Console.WriteLine(m.Groups["name"].ToString());
Console.WriteLine(m.Groups["desc"].ToString());
Console.WriteLine(m.Groups["date"].ToString());
}
答案 2 :(得分:11)
没有类似的东西,但是要写一些东西来实现:
IEnumerable<string> inputString.BreakIntoLengths(4, 6, 4)
带签名的:
public IEnumerable<string> BreakIntoLengths(this string input, params int[] lengths);
非常简单:
public IEnumerable<string> BreakIntoLengths(this string input, params int[] lengths) {
var pos = 0;
foreach (var len in lengths) {
yield return input.Substring(pos, len);
pos += len;
}
}
(实际的实现有一些错误检查。)
NB。我已经删除了类似接口的格式字符串:它似乎没有提供任何价值。返回集合后,很容易按索引分配条目。
答案 3 :(得分:2)
更好的是什么?像这样的东西?
var listStrings = new List<string>();
var tempIndex = 0;
var indexList = new List<int>{4, 6, 4}
foreach(var length in indexList)
{
listStrings.Add(string.Substring(tempIndex , length);
tempIndex += length
}
答案 4 :(得分:2)
不,没有内置方式。我会使用像Substring
这样的字符串方法:
string name, desc, date;
if (input.Length >= 4)
{
name = input.Substring(0, 4);
if (input.Length >= 10)
{
desc = input.Substring(4, 6);
if (input.Length >= 14)
{
date = input.Substring(10, 4);
}
}
}