如何拆分文本,一定数量的char .. [英] How the Split text, certain number of char..
本文介绍了如何拆分文本,一定数量的char ..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨..我有点问题。如何分割以下字符串。
我想分割字符32.(,)。注意:字符串可以更好。
总是将除以32.(,)字符。谢谢帮助。
string = 20151003 ,2015-10-03 18 : 17 : 36 , 83556593 , 1555907159 , 001 ,abcde, 1 ,ggg ,,,,,,,,,,,,, ,,,,,,,,,,,, 20151003 ,2015-10-03 18 :< span class =code-digit> 17 : 36 , 83556593 , 2555907157 , 002 ,sadfgfhg, 1 ,fdghh ,, ,,,,,,,,,,,,,,,,,,,, 20151003 ,2015-10-03 18 : 17 : 36 , 83556593 跨度>,< span class =code-digit> 3555907157 , 003 ,wertre, 1 ,ewr ,,,,,,,,,,,,,,,,,,,,,, $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
我想要的:
20151003 ,2015-10-03 18 : 17 : 36 , 83556593 , 1555907159 , 001 ,abcde, 1 ,ggg ,,,,,,,,,,,,,,,,,,,,,,,
20151003 ,2015-10-03 18 : 17 : 36 , 83556593 , 2555907157 , 002 ,sadfgfhg, 1 ,fdghh ,,,,,,,,,,,,,,,,,,,,,,,,, ,
20151003 ,2015-10-03 18 : 17 : 36 , 83556593 , 3555907157 , 003 ,wertre, 1 ,ewr ,,,,,,,,,,,,,,,,,,,,,,,
解决方案
以下内容:
string z = @ 20151003,2015-10-03 18:17:36,83556593,1555907159,001 ,abcde,1,ggg ,,,,,,,,,,,,,,,,,,,,,,, 20151003,2015-10-03 18:17:36,83556593,2555907157,002, sadfgfhg,1,fdghh ,,,,,,,,,,,,,,,,,,,,,,, 20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre ,如图1所示,EWR ,,,,,,,,,,,,,,,,,,,,,,,,,跨度>;
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex( @ (\d {8},\d {4} -\d {2} -\ {2}));
string [] y = reg.Split(z);
请记住,我根本不是一个正则表达式大师...;)
希望它有一些帮助
尝试下面的代码:
string temp1 = @ 20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg ,,,,,,,,,, ,,,,,,,,,,,,,,,, 20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh ,,,,,,,,,, ,,,,,,,,,,,,,,, 20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr ,,,,,,,,,,, ,,,,,,,,,,,,,,跨度>;
string [] temp = temp1.Split(' ,跨度>);
int total = temp.Length;
int totalIndex = temp.Length;
如果(总% 32 == 0 )
{
totalIndex = total / 32 ;
}
else
{
totalIndex =(total / 32 )+1;
}
string [] tempResult = new 串跨度> [totalIndex];
for ( int idx = 0 ; idx < totalIndex; idx ++)
{
var tempArr = temp.Select((s,i)= > new {i,s} )
。其中(t = > (ti > =(idx * 32 ))&&(ti < =((idx +1)* 32 - 1 )))
。选择(t = > ts)
.ToArray();
tempResult [idx] = string .Join( ,,tempArr);
}
foreach ( string t in tempResult)
{
Console.WriteLine(t.ToString());
}
string testString =
20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1, ggg ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,跨度>;
var lines = new List< string>();
int n = 0 ;
int startIndex = 0 ;
for ( int i = 0 ; i < testString.Length; i ++)
{
if (testString [i] == ' ,')
{
n ++;
if (n% 32 == 0 )
{
lines.Add(testString.Substring(startIndex,i + 1 - startIndex));
startIndex = i + 1;
}
}
}
Hi.. I have a little problem. How can I split the following string.
I would like to divide characters 32.(","). Note: String can be great more.
Always will be divided by 32.(",") characters. Thanks Help.
string = 20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,
I want :
20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,
20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,
20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,
解决方案 Something along the line of:
string z = @"20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"(\d{8},\d{4}-\d{2}-\d{2})");
string[] y = reg.Split(z);
Please bear in mind that i'm not a Regex Guru at all... ;)
hope some of its helps
Try below code:
string temp1 = @"20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,";
string[] temp = temp1.Split(',');
int total = temp.Length;
int totalIndex = temp.Length;
if (total % 32 == 0)
{
totalIndex = total / 32;
}
else
{
totalIndex = (total / 32) +1;
}
string[] tempResult = new string[totalIndex];
for (int idx = 0; idx < totalIndex; idx++)
{
var tempArr = temp.Select((s, i) => new { i, s })
.Where(t => (t.i >= (idx * 32)) && (t.i <= ((idx +1)* 32 - 1)))
.Select(t => t.s)
.ToArray();
tempResult[idx] = string.Join(",", tempArr);
}
foreach (string t in tempResult)
{
Console.WriteLine(t.ToString());
}
string testString =
"20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,";
var lines = new List<string>();
int n = 0;
int startIndex = 0;
for (int i = 0; i < testString.Length; i++)
{
if (testString[i] == ',')
{
n++;
if (n % 32 == 0)
{
lines.Add(testString.Substring(startIndex, i+1 - startIndex));
startIndex = i+1;
}
}
}
这篇关于如何拆分文本,一定数量的char ..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文