读取数值类似于
3.93 * 2 , 3.94 * 5 ,3.95 * 3 , 4.1 , 4.2 * 5,
逗号是必须要拆开的,这个问题不大,主要是*这个,比如 * 后面是2,表示这个数值连着出现了两次,只出现一次就不显示
上面这行值解下来应该是
3.93
3.93
3.94
3.94
3.94
3.94
3.94
3.95
3.95
3.95
4.1
4.2
4.2
4.2
4.2
4.2
这个用C#代码该怎么写?不知道发这个问题的时候星号能不能显示出来
split得到每组数组,遍历在split下按照第二项生成第一项的个数压入数组中,示例代码如下
using System;
using System.Linq;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
var s = "3.93*2,3.94*5,3.95*3,4.1,4.2*5";
var rs = s.Split(',').SelectMany(i =>
{
var item = i.Split('*').Select(x => double.Parse(x)).ToArray();
if (item.Length == 1) return new double[] { item[0] };
else return Enumerable.Repeat(item[0], (int)item[1]).ToArray();
});
Console.WriteLine(string.Join("\n", rs));
Console.ReadKey();
}
}
}
string a = Regex.Replace("3.93 * 2 , 3.94 * 5 ,3.95 * 3 , 4.1 , 4.2 * 5", @"[,\s]*([\d\.\s\*]+)", delegate (Match m)
{
string[] s = Regex.Replace(m.Groups[1].Value, @"\s+", "").Split(new string[] { "*" }, StringSplitOptions.None);
if (s.Length == 1)
{
return s[0] + "\n";
}
else
{
return string.Concat(Enumerable.Repeat(s[0] + "\n", Convert.ToInt32(s[1])));
}
});