2011-04-01 158 views
2

我有一個帶有174個不同字符串列表的.txt文件。每個字符串都有唯一的標識符。 例如:在txt文件中搜索字符串

123|this data is variable| 
456|this data is variable| 
789|so is this| 
etc.. 

我希望寫在C#中一個長期計劃,將閱讀txt文件,並只顯示174個字符串中的一個,如果我指定我想要的字符串的ID。這是因爲在文件中我所有的數據都是可變的,所以只有ID可以用來拉字符串。所以,而不是結束與我只得到一條線的例子。

如剛剛

123|this data is variable| 

我似乎能夠寫一個長期計劃將從.txt文件拉只是ID,而不是整個字符串或程序mearly讀取整個文件,並顯示它。但我還沒有確切地知道我所需要的。幫幫我!

那麼我從txt文件中得到的實際字符串沒有'|'他們只是在這個例子中。真正的字符串的一個例子是:0111111(0010101)其中括號中的數據是可變的。括號也不存在於真正的字符串中。

命名空間String_reader { 類節目 { 靜態無效的主要(字串[] args){ 字符串 文件路徑= @ 「C:\這裏我的文件名」; 絃線;

 if(File.Exists(filepath)) 

     { 
      StreamReader file = null; 
      try 
      { 
       file = new StreamReader(filepath); 
       while ((line = file.ReadLine()) !=null) 
       { 
        string regMatch = "ID number here"; //this is where it all falls apart. 
        Regex.IsMatch (line, regMatch); 

        Console.WriteLine (line);// When program is run it just displays the whole .txt file 
       } 
      } 
     } 
     finally{ 
      if (file !=null) 
       file.Close(); 
     } 
    } 
    Console.ReadLine(); 


    } 
} 

}

+1

向我們展示您已擁有的代碼。 – Carra 2011-04-01 14:52:18

+2

嗨。請發佈迄今爲止您嘗試的代碼/方法的示例。 – trickwallett 2011-04-01 14:53:04

+0

你的代碼很好..你所需要的只是修復你的模式,爲你的正則表達式。見Flynn的帖子。 – Kipotlov 2011-04-01 15:21:13

回答

1

使用readlines方法得到線的字符串數組然後在字符串分割|

+0

此外,您可以抓取所有數據使用拆分,然後將其存儲在字典類型的結構,以便您不必不斷從文件中獲取數據。 – 2011-04-01 15:02:30

+0

static void Main(string [] args) { String filepath = @「C:\ myfile name here」; 絃線; if(File.Exists(filepath)) { StreamReader file = null; 嘗試 { file = new StreamReader(filepath); while((line = file.ReadLine())!= null) { – Matt 2011-04-01 15:07:22

0

您可以使用分割方法將整個文本劃分爲由'|'劃分的部分。然後,所有的偶數元素將對應於數字奇數元素 - 字符串。

StreamReader sr = new StreamReader(filename); 
string text = sr.ReadToEnd(); 
string[] data = text.Split('|'); 

然後將某些數據元素轉換爲數字和字符串,即int [] ID和string [] Strs。發現給定的ID與IDX = Array.FindIndex(IDS,ID.Equals)的索引和相應的字符串將是STRS [IDX]

List <int> IDs; 
List <string> Strs; 
for (int i = 0; i < data.Length - 1; i += 2) 
{ 
    IDs.Add(int.Parse(data[i])); 
    Strs.Add(data[i + 1]); 
} 
idx = Array.FindIndex(IDs, ID.Equals); // we get ID from input 
answer = Strs[idx]; 
1
FileInfo info = new FileInfo("filename.txt"); 
String[] lines = info.OpenText().ReadToEnd().Split(' '); 

foreach(String line in lines) 
{ 
    int id = Convert.ToInt32(line.Split('|')[0]); 
    string text = Convert.ToInt32(line.Split('|')[1]); 
} 
2

假設您有pathid

Console.WriteLine(File.ReadAllLines(path).Where(l => l.StartsWith(id + "|")).FirstOrDefault()); 
1
  • 將數據讀入一個字符串
  • 分割字符串的 「|」
  • 閱讀項目2×2:鍵:值,鍵:值,...
  • 它們添加到字典

現在你可以很容易地找到詞典[關鍵]您的字符串。

3

使用正則表達式。東西沿線Regex.Match("|"+inputString+"|",@"\|[ ]*\d+\|(.+?)\|").Groups[1].Value

哦,我差點忘了;您需要將d+替換爲您想要的實際索引。現在,這隻會讓你成爲第一個。

「|」在輸入字符串之前和之後確保索引和值都包含在|中適用於所有元素,包括第一個和最後一個。有沒有它的方式做一個正則表達式,但恕我直言,他們只是讓你的正則表達式更復雜,可讀性更差。

+0

那麼我從txt文件中得到的實際字符串沒有'|'他們只是在這個例子中。真正的字符串的一個例子是:0111111(0010101)其中括號中的數據是可變的。括號不存在於真正的字符串 – Matt 2011-04-01 15:09:46

+0

嗯,使用正則表達式@@ 0111111([01] {7})「'會在這個例子中找到一個;當然如果你的變量數據碰巧包含一個與有效索引相同的值,你將會遇到問題。 – Flynn1179 2011-04-01 15:14:23

1
private static IEnumerable<string> ReadLines(string fspec) 
    { 
     using (var reader = new StreamReader(new FileStream(fspec, FileMode.Open, FileAccess.Read, FileShare.Read))) 
     { 
      while (!reader.EndOfStream) 
       yield return reader.ReadLine(); 
     } 
    } 

    var dict = ReadLines("input.txt") 
     .Select(s => 
        { 
         var split = s.Split("|".ToArray(), 2); 
         return new {Id = Int32.Parse(split[0]), Text = split[1]}; 
        }) 
     .ToDictionary(kv => kv.Id, kv => kv.Text); 

請注意,與.NET 4.0,你不需要readlines方法的功能,因爲ReadLines

現在,您可以與任何字典工作:

Console.WriteLine(dict[12]); 
Console.WriteLine(dict[999]); 

沒有錯誤在這裏處理,請自行添加

1

先將該文件加載到一個字符串中。
那就試試這個:

string s = "123|this data is variable| 456|this data is also variable| 789|so is this|"; 
int index = s.IndexOf("123", 0); 
string temp = s.Substring(index,s.Length-index); 
string[] splitStr = temp.Split('|'); 
Console.WriteLine(splitStr[1]); 

希望這是你在找什麼。