在数组中查找非常具体的值

时间:2016-02-19 22:08:54

标签: c# arrays search

我的文本文件种类繁多。

SETUP - BALANCE MODE PREFERENCE ORDER 3RD CHOICE: HEBM_TapeTape
SETUP - ALLOW SINGLE WEIGHT ALONG PROFILE: no
SETUP - ALLOW OPTICAL SPOKE CALCULATIONS: no
Number of spokes used for this spin: 0
spoke locations: 
spoke base widths (enc cnts): 
spoke base widths       (mm): 
Spokes were entered manually: no

Weight 0: 2.25oz
    Is3DWeightValueNonZero: yes
    Plane Type: HEPT_Inner
    Weight type: HEWT_Tape
    Weight width: 18.000000 mm
    Weight height: 3.810000 mm
    IsSplitWeight: no
    IsSpokeWeight: no
    Is3MStyleTapeWeight: no
    Weight-circle distance: 192.340000 mm
    Weight-circle radius: 194.310000 mm
    Number of tape weight Rows: 1
    Number of tape weight chiclets last row: 9
    Max number of tape weight chiclets per row: 12
    Tape menu selection in use: 0
    Tape sub-menu selection in use: 0
Weight 1: NA
    Is3DWeightValueNonZero: 
    Plane Type: 
    Weight type: 
    Weight width:
    Weight height:
    IsSplitWeight: 
    IsSpokeWeight: 
    Is3MStyleTapeWeight: 
    Weight-circle distance:
    Weight-circle radius:
    Number of tape weight Rows:
    Number of tape weight chiclets last row:
    Max number of tape weight chiclets per row:
    Tape menu selection in use:
    Tape sub-menu selection in use:
Weight 2: 2.00oz
    Is3DWeightValueNonZero: yes
    Plane Type: HEPT_Outer
    Weight type: HEWT_Tape
    Weight width: 18.000000 mm
    Weight height: 3.810000 mm
    IsSplitWeight: no
    IsSpokeWeight: no
    Is3MStyleTapeWeight: no
    Weight-circle distance: 302.340000 mm
    Weight-circle radius: 193.040000 mm
    Number of tape weight Rows: 1
    Number of tape weight chiclets last row: 8
    Max number of tape weight chiclets per row: 12
    Tape menu selection in use: 0
    Tape sub-menu selection in use: 0

等。

我必须在重量后找到测量值(测量值可以在用作标题的任何权重之后,例如2.25)并将它们存储到变量中。在单词权重(0-4)之后提取浮点值的最佳方法是什么。文档在不同的保存期间更改长度,值可以在任何一个权重子标题之后。我目前将每个文本文件存储在如下数组中:

readWeights = File.OpenText(siteChosen + Storage[Counter] + sWeightsFrstPresFile);
sCopyWeightsFile = readWeights.ReadToEnd();
sWeightsFirstPresented = sCopyWeightsFile.Split(new string[] { "\n", "\r" }, StringSplitOptions.None);

我尝试拆分数组然后搜索“oz”关键字,但它似乎找不到它。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

使用List<string>或仅使用您已拆分的普通数组迭代数组。在这些数组元素中搜索您的文本。使用包含&#34; oz&#34;。

的新List<string>构建一个新var myList = new List<string>(); for (var i = 0; i < sWeightsFirstPresented.length; i++) { if (sWeightsFirstPresented[i].IndexOf("oz") > -1) myList.Add(sWeightsFirstPresented[i]); }
hex

答案 1 :(得分:0)

您可以使用File.ReadAllLines()来读取文件。但是,如果您在文本字段中包含换行符,则可能无效。

然后你可以使用这样的简单正则表来识别权重。

var weights = list.Where(w => Regex.IsMatch(w, @"[Weight] \d")).Select(w => w.Split(new[] {':'})[1]).ToList();