我必须在c#中每16个字符后添加或追加(逗号)。这段代码是.txt格式.plzz帮助 一些示例代码如下:
443322115355524403000000000000001a000000c0604f0060624f0000000000ee3e01000000000050726f626520300050726f6265203000446174612047726f7570000000000000412d5363616e000047723a310000000044656661756c74204265616d0000000047617465204d61696e2028412d5363616e2900000000000000790000407b0000807d0000c07f0000008200004084000080860000c0880000008b0000408d0000808f0000c0910000009400004096000080980000c09a0000009d0000409f000080a10000c0a3000000a6000040a8000080aa0000c0ac000000af000040b1000080b30000c0b5000000b8000040ba000080bc0000c0be000000c1000040c3000080c50000c0c7000000ca000040cc000080ce0000c0d0000000d3000040d5000080d70000c0d9000000dc000040de000080e00000c0e2000000e5000040e7000080e90000c0eb000000ee000040f0000080f20000c0f4000000f7000040f9000080fb0000c0fd0000000001004002010080040100c006010000090100400b0100800d0100c00f0100001201004014010080160100c0180100001b0100401d0100801f0100c0210100002401004026010080280100c02a0100002d0100402f010080310100c03301000036010040380100803a0100c03c0100003f01004041010080430100c045010000480 100404a0100804c0100c04e0100005101004053010080550100c0570100005a0100405c0100805e0100c06001000"
我试过了
using (StreamReader reader = new StreamReader(fileStream))
{
string linesFromFile = String.Concat(bin.Select(b => b.ToString("x2")));
linesFromFile.Split(',');
richTextBox1.Text = linesFromFile;
}
答案 0 :(得分:1)
string input = "12345678";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (i % 3 == 0)
sb.Append(',');
sb.Append(input[i]);
}
string formatted = sb.ToString();
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=VS.80).aspx
答案 1 :(得分:1)
您可以将StringBuilder用于此
s = "Your text";
StringBuilder result = new StringBuilder();
for (var i = 0; i<s.Length; i++)
{
char currLetter = s[i];
result = (i % 16 == 0) ? result.Append(string.Format(",{0}", currLetter)) : result.Append(currLetter);
}
答案 2 :(得分:0)
一种简单的方法是:
string result = string.Empty;
for (int i = 0; i < text.ToArray().Length; i++)
{
char c = text.ToArray()[i];
if (i%16 != 0)
result += c;
else
result += "," + c;
}
答案 3 :(得分:0)
这有点短(而不是非常C#-ish):
string x = "that string", y = string.Empty; ; int i = -16;
while ( i < x.Length ) y += x.Skip( i += 16 ).Take( 16 ) + ",";
如果速度很重要,你应该考虑使用StringBuilder。
答案 4 :(得分:0)
以下是基于输入的示例代码...
var input ="443322115355524403000000000000001a000000c0604f0060624f0000000000ee3e01000000000050726f626520300050726f6265203000446174612047726f7570000000000000412d5363616e000047723a310000000044656661756c74204265616d0000000047617465204d61696e2028412d5363616e2900000000000000790000407b0000807d0000c07f0000008200004084000080860000c0880000008b0000408d0000808f0000c0910000009400004096000080980000c09a0000009d0000409f000080a10000c0a3000000a6000040a8000080aa0000c0ac000000af000040b1000080b30000c0b5000000b8000040ba000080bc0000c0be000000c1000040c3000080c50000c0c7000000ca000040cc000080ce0000c0d0000000d3000040d5000080d70000c0d9000000dc000040de000080e00000c0e2000000e5000040e7000080e90000c0eb000000ee000040f0000080f20000c0f4000000f7000040f9000080fb0000c0fd0000000001004002010080040100c006010000090100400b0100800d0100c00f0100001201004014010080160100c0180100001b0100401d0100801f0100c0210100002401004026010080280100c02a0100002d0100402f010080310100c03301000036010040380100803a0100c03c0100003f01004041010080430100c045010000480100404a0100804c0100c04e0100005101004053010080550100c0570100005a0100405c0100805e0100c06001000";
var output = string.Empty;
int count = 0;
foreach (var c in input)
{
count++;
output += count % 16 == 0 ? "," : c.ToString();
}
//Output : 443322115355524,030000000000000,1a000000c0604f0,60624f000000000,ee3e01000000000,50726f626520300,50726f626520300,446174612047726,757000000000000,412d5363616e000,47723a310000000,44656661756c742,4265616d0000000,47617465204d616,6e2028412d53636,6e2900000000000,00790000407b000,807d0000c07f000,008200004084000,80860000c088000,008b0000408d000,808f0000c091000,009400004096000,80980000c09a000,009d0000409f000,80a10000c0a3000,00a6000040a8000,80aa0000c0ac000,00af000040b1000,80b30000c0b5000,00b8000040ba000,80bc0000c0be000,00c1000040c3000,80c50000c0c7000,00ca000040cc000,80ce0000c0d0000,00d3000040d5000,80d70000c0d9000,00dc000040de000,80e00000c0e2000,00e5000040e7000,80e90000c0eb000,00ee000040f0000,80f20000c0f4000,00f7000040f9000,80fb0000c0fd000,000001004002010,80040100c006010,00090100400b010,800d0100c00f010,001201004014010,80160100c018010,001b0100401d010,801f0100c021010,002401004026010,80280100c02a010,002d0100402f010,80310100c033010,003601004038010,803a0100c03c010,003f01004041010,80430100c045010,00480100404a010,804c0100c04e010,005101004053010,80550100c057010,005a0100405c010,805e0100c060010,0
Console.WriteLine(String.Format("Output : {0}", output));