C# 如何从字符串中收集匹配值的数组?
我正在将内存映射文件中的数据填充为字符串,如下所示:C# 如何从字符串中收集匹配值的数组?,c#,arrays,.net,C#,Arrays,.net,我正在将内存映射文件中的数据填充为字符串,如下所示: AAPL,2013-1-2 Open:79.117 Close:78.433 High:79.286 Low:77.376 Volume:139948984 AAPL,2013-1-3 Open:78.268 Close:77.442 High:78.524 Low:77.286 Volume:88114464 等等 现在我想做一个数组,表示所有天数的接近值。并且在内存映射文件和字符串中收集了数千天的数据。那个么,我怎样才能获取接近值并将
AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984
AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464
等等
现在我想做一个数组,表示所有天数的接近值。并且在内存映射文件和字符串中收集了数千天的数据。那个么,我怎样才能获取接近值并将其作为数组呢
我试图使它的数组,但它是使整个数据成为一个单一的数组。所以这不是我想要的
string[] lines = System.IO.File.ReadAllLines(@"D:\mine.txt");
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Console.WriteLine("\t" + line);
}
byte[] bytes = new byte[10000000];
stream.ReadArray(0, bytes, 0, bytes.Length);
string txt = Encoding.UTF8.GetString(bytes).Trim('\0');`
所以我需要一个包含所有接近值的数组来从该字符串中提取。就像这样:
{78.433, 77.442, etc..}
试试这个:
decimal[] arrayOfCloses =
File
.ReadAllLines(@"D:\mine.txt")
.Select(x => x.Split(':'))
.Where(x => x.Length == 2)
.Where(x => x[0] == "Close")
.Select(x => decimal.Parse(x[1]))
.ToArray();
File.ReadLines(@"D:\mine.txt")
// Pick only those lines starting with "Close"
.Where(line => line.StartsWith("Close:"))
// Get value, which follows colon, and parse it do double
.Select(line => double.Parse(line.Split(':')[1]))
// Convert result to an array
.ToArray();
试试这个:
decimal[] arrayOfCloses =
File
.ReadAllLines(@"D:\mine.txt")
.Select(x => x.Split(':'))
.Where(x => x.Length == 2)
.Where(x => x[0] == "Close")
.Select(x => decimal.Parse(x[1]))
.ToArray();
File.ReadLines(@"D:\mine.txt")
// Pick only those lines starting with "Close"
.Where(line => line.StartsWith("Close:"))
// Get value, which follows colon, and parse it do double
.Select(line => double.Parse(line.Split(':')[1]))
// Convert result to an array
.ToArray();
我以为你的档案是这样的:
AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984
AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464
试试这个
var lines = System.IO.File.ReadAllLines(@"C:\Users\bouyami\Documents\AB_ATELIER\1.txt").ToList();
var linesFiltred = lines.Where(x => x.StartsWith("Close")).ToList();
var result = linesFiltred.Select(x => x.Split(':')[1]).ToList();
我以为你的档案是这样的:
AAPL,2013-1-2
Open:79.117
Close:78.433
High:79.286
Low:77.376
Volume:139948984
AAPL,2013-1-3
Open:78.268
Close:77.442
High:78.524
Low:77.286
Volume:88114464
试试这个
var lines = System.IO.File.ReadAllLines(@"C:\Users\bouyami\Documents\AB_ATELIER\1.txt").ToList();
var linesFiltred = lines.Where(x => x.StartsWith("Close")).ToList();
var result = linesFiltred.Select(x => x.Split(':')[1]).ToList();
试试这个:
decimal[] arrayOfCloses =
File
.ReadAllLines(@"D:\mine.txt")
.Select(x => x.Split(':'))
.Where(x => x.Length == 2)
.Where(x => x[0] == "Close")
.Select(x => decimal.Parse(x[1]))
.ToArray();
File.ReadLines(@"D:\mine.txt")
// Pick only those lines starting with "Close"
.Where(line => line.StartsWith("Close:"))
// Get value, which follows colon, and parse it do double
.Select(line => double.Parse(line.Split(':')[1]))
// Convert result to an array
.ToArray();
试试这个:
decimal[] arrayOfCloses =
File
.ReadAllLines(@"D:\mine.txt")
.Select(x => x.Split(':'))
.Where(x => x.Length == 2)
.Where(x => x[0] == "Close")
.Select(x => decimal.Parse(x[1]))
.ToArray();
File.ReadLines(@"D:\mine.txt")
// Pick only those lines starting with "Close"
.Where(line => line.StartsWith("Close:"))
// Get value, which follows colon, and parse it do double
.Select(line => double.Parse(line.Split(':')[1]))
// Convert result to an array
.ToArray();
尝试以下接受空行的选项:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication98
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
AAPL aapl = new AAPL(FILENAME);
}
}
public class AAPL
{
static List<AAPL> aapls = new List<AAPL>();
private DateTime date { get; set; }
public decimal open { get; set; }
public decimal close { get; set; }
public decimal low { get; set; }
public decimal high { get; set; }
public int volume { get; set; }
public AAPL() { }
public AAPL(string filename)
{
StreamReader reader = new StreamReader(filename);
string line = "";
AAPL newAAPL = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (line.StartsWith("AAPL"))
{
string dateStr = line.Substring(line.IndexOf(",") + 1);
date = DateTime.Parse(dateStr);
newAAPL = new AAPL();
aapls.Add(newAAPL);
newAAPL.date = date;
}
else
{
string[] splitArray = line.Split(new char[] { ':' });
switch (splitArray[0])
{
case "Open":
newAAPL.open = decimal.Parse(splitArray[1]);
break;
case "Close":
newAAPL.close = decimal.Parse(splitArray[1]);
break;
case "Low":
newAAPL.low = decimal.Parse(splitArray[1]);
break;
case "High":
newAAPL.high = decimal.Parse(splitArray[1]);
break;
case "Volume":
newAAPL.volume = int.Parse(splitArray[1]);
break;
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序98
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
AAPL AAPL=新的AAPL(文件名);
}
}
公共类AAPL
{
静态列表aapls=新列表();
私有日期时间日期{get;set;}
公共十进制开放{get;set;}
公共十进制关闭{get;set;}
公共十进制低位{get;set;}
公共十进制高位{get;set;}
公共int卷{get;set;}
公共AAPL(){}
公共AAPL(字符串文件名)
{
StreamReader=新的StreamReader(文件名);
字符串行=”;
AAPL newAAPL=null;
而((line=reader.ReadLine())!=null)
{
line=line.Trim();
如果(直线长度>0)
{
if(行起始带(“AAPL”))
{
字符串dateStr=line.Substring(line.IndexOf(“,”)+1);
date=DateTime.Parse(dateStr);
newAAPL=newAAPL();
aa请添加(新aapl);
newAAPL.date=日期;
}
其他的
{
string[]splitArray=line.Split(新字符[]{':'});
交换机(拆分阵列[0])
{
案例“未结”:
newAAPL.open=decimal.Parse(splitArray[1]);
打破
案例“结束”:
newAAPL.close=decimal.Parse(splitArray[1]);
打破
案例“低”:
newAAPL.low=decimal.Parse(splitArray[1]);
打破
案例“高”:
newAAPL.high=decimal.Parse(splitArray[1]);
打破
案例“卷”:
newAAPL.volume=int.Parse(splitArray[1]);
打破
}
}
}
}
}
}
}
尝试以下接受空行的选项:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication98
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
AAPL aapl = new AAPL(FILENAME);
}
}
public class AAPL
{
static List<AAPL> aapls = new List<AAPL>();
private DateTime date { get; set; }
public decimal open { get; set; }
public decimal close { get; set; }
public decimal low { get; set; }
public decimal high { get; set; }
public int volume { get; set; }
public AAPL() { }
public AAPL(string filename)
{
StreamReader reader = new StreamReader(filename);
string line = "";
AAPL newAAPL = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (line.StartsWith("AAPL"))
{
string dateStr = line.Substring(line.IndexOf(",") + 1);
date = DateTime.Parse(dateStr);
newAAPL = new AAPL();
aapls.Add(newAAPL);
newAAPL.date = date;
}
else
{
string[] splitArray = line.Split(new char[] { ':' });
switch (splitArray[0])
{
case "Open":
newAAPL.open = decimal.Parse(splitArray[1]);
break;
case "Close":
newAAPL.close = decimal.Parse(splitArray[1]);
break;
case "Low":
newAAPL.low = decimal.Parse(splitArray[1]);
break;
case "High":
newAAPL.high = decimal.Parse(splitArray[1]);
break;
case "Volume":
newAAPL.volume = int.Parse(splitArray[1]);
break;
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序98
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
AAPL AAPL=新的AAPL(文件名);
}
}
公共类AAPL
{
静态列表aapls=新列表();
私有日期时间日期{get;set;}
公共十进制开放{get;set;}
公共十进制关闭{get;set;}
公共十进制低位{get;set;}
公共十进制高位{get;set;}
公共int卷{get;set;}
公共AAPL(){}
公共AAPL(字符串文件名)
{
StreamReader=新的StreamReader(文件名);
字符串行=”;
AAPL newAAPL=null;
而((line=reader.ReadLine())!=null)
{
line=line.Trim();
如果(直线长度>0)
{
if(行起始带(“AAPL”))
{
字符串dateStr=line.Substring(line.IndexOf(“,”)+1);
date=DateTime.Parse(dateStr);
newAAPL=newAAPL();
aa请添加(新aapl);
newAAPL.date=日期;
}
其他的
{
string[]splitArray=line.Split(新字符[]{':'});
交换机(拆分阵列[0])
{
案例“未结”:
newAAPL.open=decimal.Parse(splitArray[1]);
打破
案例“结束”:
newAAPL.close=decimal.Parse(splitArray[1]);
打破
案例“低”:
newAAPL.low=decimal.Parse(splitArray[1]);
打破
案例“高”:
newAAPL.high=decimal.Parse(splitArray[1]);
打破
案例“卷”:
newAAPL.volume=int.Parse(splitArray[1]);
打破
}
}
}
}
}
}
}
我不相信我完全理解这个问题。但是,如果您正在枚举文件的行,并且文件的格式如您所示,则在foreach
循环中,只需检查行
是否以Close>开头