0

I am working on text file..i want to know how to search for a string in text file and then perform the operations on that string.I have written a code but there is some problem with "if condition",when the particular string comes,the control skips to the next line..it doesnot perform any operation on that string.. Below is the line from text file from which i am trying to find "MerchantNo:105838015" and then performing Substring operation on it to get only the number..There is a space in between the line.. MERCHANTNO:105838015 AGENT CODE 00913

And this is the code:

    StreamReader sr = new StreamReader(fldr);
        string line = null;

        while ((line = sr.ReadLine()) != null)
        {
            str = line.Trim().Split(' ');
            for (int i = 0; i < str.Length; i++)
            {
                if (str.ToString().ToUpper().Contains("MERCHANTNO:105838015"))
                {
                        //str = line.Split(' ');
                        string MNo1 = line.Substring(15, 23);
                        MNo = Convert.ToInt32(MNo1.ToString());
                }
                break;

            }

            //MessageBox.Show("Line is:" +line);
        }

        MessageBox.Show("MerchantNo is:" +MNo);

Please do say me what i have to do??

Nazima
  • 291
  • 3
  • 7
  • 15
  • Why do you call substr with 15, 23? – K-ballo Sep 10 '11 at 09:47
  • 1
    Are you always looking for exactly that string: ...105838015 ? If you know this number a priori you do not need to parse and convert it to Int32 because you simply already know it. from your code you are looking for it hardcoded. – Davide Piras Sep 10 '11 at 09:47
  • do you want to transform the hole stream? I don't know what operation you want to do - because your code says exactly what you describe: do nothing but check for "MERCHANTNO:105838015" and convert a substring from there to a int variable(?) MNo ... what *should* the code do? – Random Dev Sep 10 '11 at 09:49
  • Sorry for not being clear with my doubt..i am struggling in coding so if wrong do guide me..what i have to do is parse a textfile..i need to extract the exact records from the file and store it in database..and in the code above i have done hardcoding(to check) which is not a good to do..i need to extract the number based on text "MerchantNo" in the textfile and store it in database..likewise i have to do with other data also..hope now it is clear what i want to do..if not please do say me... – Nazima Sep 10 '11 at 11:31

3 Answers3

4

I suspect that this is the problem:

if (str.ToString()...)

You're calling ToString on a string array. I suspect you meant:

if (str[i].ToUpper().Contains(...))

Having said that, it's not clear why you're then using the line for a substring - or why you're calling MNo1.ToString() when MNo1 is already a string...

I would also recommend that you use:

if (str[i].IndexOf("MERCHANTNO:105838015",
                   StringComparison.CurrentCultureIgnoreCase) > -1)

(or something similar, depending on what culture you want to use) rather than upper-casing each part.

You should also consider using a foreach loop instead of a for loop:

foreach (string part in line.Trim().Split(' '))
{
    if (part.IndexOf(...) > -1)
    {
        ...
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have tried using if (str[i].IndexOf("MERCHANTNO:105838015", StringComparison.CurrentCultureIgnoreCase)) but it is showing me an error that "Cannot implicitly convert type 'int' to 'bool'"...and also i will try to do as you suggested for using foreach loop.. – Nazima Sep 10 '11 at 11:52
  • @Nazima: Apologies, I forgot to put the `> -1` bit in before - edited now. – Jon Skeet Sep 10 '11 at 12:54
1

I think if you use Regular Expression you can solve your problem quickly. First of all read whole file in a variable and then apply your Regular Expression to that.

You can use grouping in your expression to get the number separately like below:

        int number = 0;
        using (var sr = new StreamReader(fileName))
        {
            string fileContent = sr.ReadToEnd();
            Regex regex = new Regex(@"(?<=.*MERCHANTNO:)(?<number>\d*)");
            var match = regex.Match(fileContent);
            if (match.Groups["number"].Success)
            {
                number = Convert.ToInt32(match.Groups["number"].Value);
            }
        }

        MessageBox.Show("MerchantNo is:" + number);
amirhosseinab
  • 1,030
  • 2
  • 16
  • 29
0

It's a bit confusing what this code supposed to do, but I guess you wanna make sure if the file contains this merchantno? You should use regex instead of substring juggling.

With text file:

MERCHANTNO:10583218015 AGENT CODE 00913
MERCHANO: 10421523838015 AGENT CODE 00913
Some teeeext MERCHANTNO:105838015 AGENT CODE 00913
Some text salalala ME

And code:

var regi = new Regex(@"MERCHANTNO:\s*([0-9]*)");
string need = "105838015";
bool found = false;

using (StreamReader reader = new StreamReader("file.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        var match = regi.Match(line);
        if (match.Groups.Count == 2)
        {
            if (match.Groups[1].Value == need)
            {
                found = true;
                break;
            }
        }
    }
}

Console.WriteLine("Found: {0}", found);

Maybe this is what you want? Fixme.

(string testing for value, to avoid 012 == 12 int parsing)

Peter Porfy
  • 8,921
  • 3
  • 31
  • 41