Help with string.StartsWith() please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CCLeasing

    Help with string.StartsWith() please

    I have a text file which consists of lines of data. Each line has units
    of data seperated by semicolons, like so.

    AAAA ; some text relating to AAAA ; some more text relating to AAAA
    BBBB ; some text relating to BBBB ; some more text relating to BBBB

    I have a form which consists of a combo box control, and text box
    control.
    combo box contol is called = comCrime
    text box control is called = txtProtocol

    I am trying to compare the value in a combo box with the contents of a
    text file. I am trying to find the line in the text file that starts
    with the text in the combo box. Once this line is found I would like to
    populate the textbox control on my form with the text that lies between
    the first and second semicolons of the line. I have tried the following
    code but it isn't working. Can someone suggest a change please.

    private void comCrime_TabInd exChanged(objec t sender, EventArgs e)
    {
    string[] lines = File.ReadAllLin es(@"c:\protoco l.txt");

    foreach (string s in lines)
    {
    string[] items = s.Split(':');

    if s.StartsWith(co mCrime.Value) Then txtProtocol.Tex t =
    s[1].ToString;

    }
    }

  • Rick Lones

    #2
    Re: Help with string.StartsWi th() please

    CCLeasing wrote:
    I have a text file which consists of lines of data. Each line has units
    of data seperated by semicolons, like so.
    >
    AAAA ; some text relating to AAAA ; some more text relating to AAAA
    BBBB ; some text relating to BBBB ; some more text relating to BBBB
    >
    I have a form which consists of a combo box control, and text box
    control.
    combo box contol is called = comCrime
    text box control is called = txtProtocol
    >
    I am trying to compare the value in a combo box with the contents of a
    text file. I am trying to find the line in the text file that starts
    with the text in the combo box. Once this line is found I would like to
    populate the textbox control on my form with the text that lies between
    the first and second semicolons of the line. I have tried the following
    code but it isn't working. Can someone suggest a change please.
    >
    private void comCrime_TabInd exChanged(objec t sender, EventArgs e)
    {
    string[] lines = File.ReadAllLin es(@"c:\protoco l.txt");
    >
    foreach (string s in lines)
    {
    string[] items = s.Split(':');
    >
    if s.StartsWith(co mCrime.Value) Then txtProtocol.Tex t =
    s[1].ToString;
    >
    }
    }
    >
    Well, you say the separators are semicolons, but your sample code splits on
    colons. Is it just a simple typo?

    -rick-

    Comment

    • Dan Bass

      #3
      Re: Help with string.StartsWi th() please

      Gary,

      If you know the text file is going to be really small then it's not much of
      an issue, but rather than reading all the data into the string[] lines
      variable, maybe open the file, then go through and read each line. This
      method is arguably more efficient in time and on the resources.

      Have you tried stepping through this code to see what the values are for the
      variables?
      Also, I'd put in checks around the array accessors to ensure that when you
      go "items[1]...." that you actually have a valid non-null array with at
      least two entries [0] and [1].

      Finally, are you expecting the text to be case sensitive or not? your code
      is case sensitive at the moment because StartsWith is case-sensitive...

      Thanks.
      Dan.

      "CCLeasing" <gary@ccleasing .co.ukwrote in message
      news:1162913083 .570302.256540@ h54g2000cwb.goo glegroups.com.. .
      >I have a text file which consists of lines of data. Each line has units
      of data seperated by semicolons, like so.
      >
      AAAA ; some text relating to AAAA ; some more text relating to AAAA
      BBBB ; some text relating to BBBB ; some more text relating to BBBB
      >
      I have a form which consists of a combo box control, and text box
      control.
      combo box contol is called = comCrime
      text box control is called = txtProtocol
      >
      I am trying to compare the value in a combo box with the contents of a
      text file. I am trying to find the line in the text file that starts
      with the text in the combo box. Once this line is found I would like to
      populate the textbox control on my form with the text that lies between
      the first and second semicolons of the line. I have tried the following
      code but it isn't working. Can someone suggest a change please.
      >
      private void comCrime_TabInd exChanged(objec t sender, EventArgs e)
      {
      string[] lines = File.ReadAllLin es(@"c:\protoco l.txt");
      >
      foreach (string s in lines)
      {
      string[] items = s.Split(':');
      >
      if s.StartsWith(co mCrime.Value) Then txtProtocol.Tex t =
      s[1].ToString;
      >
      }
      }
      >

      Comment

      • Dustin Campbell

        #4
        Re: Help with string.StartsWi th() please

        I have a text file which consists of lines of data. Each line has
        units of data seperated by semicolons, like so.
        >
        AAAA ; some text relating to AAAA ; some more text relating to AAAA
        BBBB ; some text relating to BBBB ; some more text relating to BBBB
        >
        I have a form which consists of a combo box control, and text box
        control.
        combo box contol is called = comCrime
        text box control is called = txtProtocol
        I am trying to compare the value in a combo box with the contents of a
        text file. I am trying to find the line in the text file that starts
        with the text in the combo box. Once this line is found I would like
        to populate the textbox control on my form with the text that lies
        between the first and second semicolons of the line. I have tried the
        following code but it isn't working. Can someone suggest a change
        please.
        >
        private void comCrime_TabInd exChanged(objec t sender, EventArgs e)
        {
        string[] lines = File.ReadAllLin es(@"c:\protoco l.txt");
        foreach (string s in lines)
        {
        string[] items = s.Split(':');
        if s.StartsWith(co mCrime.Value) Then txtProtocol.Tex t
        = s[1].ToString;
        >
        }
        }
        If you're using the TabIndexChanged event, it's unlikely that your code will
        ever execute because that only fires when the TabIndex property is changed.
        You might try the SelectedIndexCh anged event.

        Based, on your description, the code should look like this:

        private void comCrime_Select edIndexChanged( object sender, EventArgs e)
        {
        string[] lines = File.ReadAllLin es(@"c:\protoco l.txt");
        if (lines.Length == 0)
        return;

        string lookUpValue = comCrime.Text;

        foreach (string s in lines)
        {
        if (s.StartsWith(l ookUpValue))
        {
        string[] items = s.Split(';');
        if (items.Length 1)
        {
        txtProtocol.Tex t = items[1];
        break;
        }
        }
        }
        }

        Best Regards,
        Dustin Campbell
        Developer Express Inc.


        Comment

        • CCLeasing

          #5
          Thankyou

          Thankyou very much that's exactly what i wanted.
          Much indebted to you.

          Comment

          Working...