2012-10-12 16 views
13

में # खोज स्ट्रिंग मैं स्ट्रिंग तुलना, तो यह दूसरी स्ट्रिंग जो मैं पैरामीटर के रूप में उपयोग कर रहा हूँ जब तक लाइनों को पढ़ने पर जाना चाहिए txt फ़ाइल में एक स्ट्रिंग लगाना चाहते हैं।ग txt फ़ाइल

उदाहरण:

CustomerEN //search for this string 
... 
some text wich has details about customer 
id "123456" 
username "rootuser" 
... 
CustomerCh //get text till this string 

मैं विवरण अन्यथा उनके साथ काम करने की जरूरत है।

मैं इस तरह "CustomerEN" के लिए खोज करने के लिए LINQ का उपयोग कर रहा:

File.ReadLines(pathToTextFile).Any(line => line.Contains("CustomerEN")) 

लेकिन अब मैं विवरण निकालने के लिए पढ़ने लाइनों (डेटा) "CustomerCh" तक के साथ अटक कर रहा हूँ।

+0

लगता है कि आप नियमित अभिव्यक्ति की जरूरत है और – thumbmunkeys

उत्तर

15

हैं लाइनों की अपनी जोड़ी केवल आपकी फ़ाइल में एक बार दिखाई देगा, आपको इस्तेमाल कर सकते हैं

File.ReadLines(pathToTextFile) 
    .SkipWhile(line => !line.Contains("CustomerEN")) 
    .Skip(1) // optional 
    .TakeWhile(line => !line.Contains("CustomerCh")); 

आप एक फ़ाइल में अनेक गतिविधियां हो सकता था, तो आप एक नियमित रूप से foreach पाश का उपयोग कर बंद शायद बेहतर कर रहे हैं - पढ़ने लाइनों, Trac रखने कि क्या आप के अंदर या बाहर एक ग्राहक आदि वर्तमान में कर रहे हैं की कश्मीर:

List<List<string>> groups = new List<List<string>>(); 
List<string> current = null; 
foreach (var line in File.ReadAllLines(pathToFile)) 
{ 
    if (line.Contains("CustomerEN") && current == null) 
     current = new List<string>(); 
    else if (line.Contains("CustomerCh") && current != null) 
    { 
     groups.Add(current); 
     current = null; 
    } 
    if (current != null) 
     current.Add(line); 
} 
+1

नहीं linq आप की जाँच http://stackoverflow.com/questions/444798/case-insensitive-containsstring –

2

आप केवल एक पहली स्ट्रिंग whant हैं, तो आप के लिए लूप सरल उपयोग कर सकते हैं।

var lines = File.ReadAllLines(pathToTextFile); 

var firstFound = false; 
for(int index = 0; index < lines.Count; index++) 
{ 
    if(!firstFound && lines[index].Contains("CustomerEN")) 
    { 
     firstFound = true; 
    } 
    if(firstFound && lines[index].Contains("CustomerCh")) 
    { 
     //do, what you want, and exit the loop 
     // return lines[index]; 
    } 
} 
4

LINQ के साथ, आप इस तरह SkipWhile/TakeWhile तरीकों का उपयोग कर सकता है,:

var importantLines = 
    File.ReadLines(pathToTextFile) 
    .SkipWhile(line => !line.Contains("CustomerEN")) 
    .TakeWhile(line => !line.Contains("CustomerCh")); 
7

आप foreach के बाद से while उपयोग करने के लिए सूचकांक के बारे में पता नहीं है। नीचे एक उदाहरण कोड है।

int counter = 0; 
string line; 

Console.Write("Input your search text: "); 
var text = Console.ReadLine(); 

System.IO.StreamReader file = 
    new System.IO.StreamReader("SampleInput1.txt"); 

while ((line = file.ReadLine()) != null) 
{ 
    if (line.Contains(text)) 
    { 
     break; 
    } 

    counter++; 
} 

Console.WriteLine("Line number: {0}", counter); 

file.Close(); 

Console.ReadLine(); 
+0

आप केस संवेदी खोज की जरूरत है वास्तव में सूचकांक को जानने की जरूरत नहीं है। – Rawling

+0

उसे पहली स्ट्रिंग को खोजने की ज़रूरत है, जो केवल दूसरे कीवर्ड के बाद दिखाई देता है। –

+0

और ऐसा कोई कारण नहीं है कि आप काउंटर को फोरच में भी नहीं डाल सके, अगर आप चाहें तो। – Chris

0

मैंने थोड़ा सा तरीका काम किया है कि रॉलिंग ने अंत तक एक ही फ़ाइल में एक से अधिक पंक्तियों को खोजने के लिए यहां पोस्ट किया था। यह वही मेरे लिए काम किया है:

   foreach (var line in File.ReadLines(pathToFile)) 
       { 
        if (line.Contains("CustomerEN") && current == null) 
        { 
         current = new List<string>(); 
         current.Add(line); 
        } 
        else if (line.Contains("CustomerEN") && current != null) 
        { 
         current.Add(line); 
        } 
       } 
       string s = String.Join(",", current); 
       MessageBox.Show(s); 
संबंधित मुद्दे