一个字段为代码(如:01,001,0001,M01,M00001,...),另一个字段为名称(张三,王八蛋,张真人,山本一郎,大山,...)。
要把字段代码和字段名称拼成一行,并把每列左对齐,请问用c#如何实现?! 谢谢!
效果如下:
01 张三
001 王八蛋
00001 张真人
M01 山本一郎
M00001 大山
要把字段代码和字段名称拼成一行,并把每列左对齐,请问用c#如何实现?! 谢谢!
效果如下:
01 张三
001 王八蛋
00001 张真人
M01 山本一郎
M00001 大山
String str2 = "张三,王八蛋,张真人,山本一郎,大山"; String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (Int32 i = 0; i < arr1.Length; i++)
{
Console.WriteLine(arr1[i].PadRight(15) + arr2[i].PadRight(15));
ps:如果不相同的话,i < arr1.Length 就要改成 < 两个数组中较小的Length
Dictionary<string, string> dic= new Dictionary<string, string>();
string[] arr=s.Split(new string[] { ","}, StringSplitOptions.RemoveEmptyEntries);
string[] arr1=s.Split(new string[] { ","}, StringSplitOptions.RemoveEmptyEntries);
for(int i=0;i<arr.length;i++)
{
dic.Add(arr[i], arr1[i]);
}
String str1 = "01,001,0001,M01,M00001";
String str2 = "张三,王八蛋,张真人,山本一郎,大山"; String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List<String> list = new List<String>(); for (Int32 i = 0; i < arr1.Length; i++)
{
String temp = arr1[i].PadRight(15) + arr2[i].PadRight(15);
list.Add(temp);
} foreach (String s in list)
Console.WriteLine(s);
这样可以了吧。 String str1 = "01,001,0001,M01,M00001";
String str2 = "张三,王八蛋,张真人,山本一郎,大山"; String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); Int32 maxL = 0;
for (Int32 i = 0; i < arr1.Length; i++)
{
if (arr1[i].Length > maxL)
maxL = arr1[i].Length;
} List<String> list = new List<String>(); for (Int32 i = 0; i < arr1.Length; i++)
{
String temp = arr1[i].PadRight(maxL + 5) + arr2[i];
list.Add(temp);
} foreach (String s in list)
Console.WriteLine(s);
String str1 = "01,001,0001,M01,M00001";
String str2 = "张三,王八蛋,张真人,山本一郎,大山"; String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int maxlength =0 ;
for(Int32 i = 0;i<arr1.Length; i++)
{
if(arr1[i].Length>maxlength)
{
maxlength = arr1[i].Length;
}
}
int length = arr1.Length < arr2.Length ? arr1.Length : arr2.Length;
for (Int32 i = 0; i < length; i++)
{
Console.WriteLine(arr1[i].PadRight(maxlength+2) + arr2[i]);
}
String str2 = "张三,王八蛋,张真人,山本一郎,大山"; String[] arr1 = str1.Split(',' );
String[] arr2 = str2.Split(',' ); for (Int32 i = 0; i < arr1.Length; i++)
{ Console.WriteLine(arr1[i].PadRight(10) + arr2[i].PadRight(10)); }这样也可以啊! 不知道你们要加那么多没用的东西干嘛!
{
String str1 = "01,001,0001,M01,M00001";
String str2 = "张三1,王八蛋12,张真人,山本一太郎acd,大山"; String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arr1.Length; i++)
{
int w = 20 - (Encoding.Default.GetByteCount(arr2[i]) - arr2[i].Length);
Console.WriteLine(arr2[i].PadRight(w) + arr1[i].PadRight(15));
}
}运行结果:
张三1 01
王八蛋12 001
张真人a 0001
山本一太郎acd M01
大山 M00001
程序执行结束,按任意键关闭窗口!
String str2 = "张三,王八蛋,张真人,山本一郎,大山"; String[] arr1 = str1.Split(',' );
String[] arr2 = str2.Split(',' ); for (Int32 i = 0; i < arr1.Length; i++)
{ Console.WriteLine(arr1[i].PadRight(10) + arr2[i].PadRight(10)); }
PadRight或者{0:10} {0:-10}