开发者

how to search for a string in text file in C#

开发者 https://www.devze.com 2023-04-04 10:59 出处:网络
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\",wh

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??


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)
    {
        ...
    }
}


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);


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消