在C#中如何将一段文本格式化输出,如下
发布网友
发布时间:2022-05-08 00:00
我来回答
共1个回答
热心网友
时间:2023-11-21 07:58
//字符串数组Price从文件“报价.txt”读取数据,回车符是数组元素的分隔符
string[] Price = System.IO.File.ReadAllLines(@"..\..\报价.txt");
//将名称和单价之间多余的空格去掉,只保留一个空格
for (int i = 0; i < Price.Length; i++)
{
int start = Price[i].IndexOf(" ");
int offset = 0;
while (Price[i][start + offset] == ' ')
offset++;
Price[i] = Price[i].Remove(start + 1, offset);
}
//方法Split将空格作为分隔符,使price成为字符串数组
var Query = from price in Price
let x = price.Split(' ')
select new
{
名称 = x[0],
单价 = x[1]
};
foreach (var q in Query)
Console.WriteLine("{0,-20} {1,10}",q.名称,q.单价);
热心网友
时间:2023-11-21 07:58
//字符串数组Price从文件“报价.txt”读取数据,回车符是数组元素的分隔符
string[] Price = System.IO.File.ReadAllLines(@"..\..\报价.txt");
//将名称和单价之间多余的空格去掉,只保留一个空格
for (int i = 0; i < Price.Length; i++)
{
int start = Price[i].IndexOf(" ");
int offset = 0;
while (Price[i][start + offset] == ' ')
offset++;
Price[i] = Price[i].Remove(start + 1, offset);
}
//方法Split将空格作为分隔符,使price成为字符串数组
var Query = from price in Price
let x = price.Split(' ')
select new
{
名称 = x[0],
单价 = x[1]
};
foreach (var q in Query)
Console.WriteLine("{0,-20} {1,10}",q.名称,q.单价);
热心网友
时间:2023-11-21 07:58
//字符串数组Price从文件“报价.txt”读取数据,回车符是数组元素的分隔符
string[] Price = System.IO.File.ReadAllLines(@"..\..\报价.txt");
//将名称和单价之间多余的空格去掉,只保留一个空格
for (int i = 0; i < Price.Length; i++)
{
int start = Price[i].IndexOf(" ");
int offset = 0;
while (Price[i][start + offset] == ' ')
offset++;
Price[i] = Price[i].Remove(start + 1, offset);
}
//方法Split将空格作为分隔符,使price成为字符串数组
var Query = from price in Price
let x = price.Split(' ')
select new
{
名称 = x[0],
单价 = x[1]
};
foreach (var q in Query)
Console.WriteLine("{0,-20} {1,10}",q.名称,q.单价);