473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");

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

if s.StartsWith(comCrime.Value) Then txtProtocol.Text =
s[1].ToString;

}
}

Nov 7 '06 #1
4 2179
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_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");

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

if s.StartsWith(comCrime.Value) Then txtProtocol.Text =
s[1].ToString;

}
}
Well, you say the separators are semicolons, but your sample code splits on
colons. Is it just a simple typo?

-rick-
Nov 7 '06 #2
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" <ga**@ccleasing.co.ukwrote in message
news:11**********************@h54g2000cwb.googlegr oups.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_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");

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

if s.StartsWith(comCrime.Value) Then txtProtocol.Text =
s[1].ToString;

}
}

Nov 7 '06 #3
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_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");
foreach (string s in lines)
{
string[] items = s.Split(':');
if s.StartsWith(comCrime.Value) Then txtProtocol.Text
= 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 SelectedIndexChanged event.

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

private void comCrime_SelectedIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");
if (lines.Length == 0)
return;

string lookUpValue = comCrime.Text;

foreach (string s in lines)
{
if (s.StartsWith(lookUpValue))
{
string[] items = s.Split(';');
if (items.Length 1)
{
txtProtocol.Text = items[1];
break;
}
}
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 7 '06 #4
Thankyou very much that's exactly what i wanted.
Much indebted to you.

Nov 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic...
4
by: Andy | last post by:
What do people think of this? 'prefixed string'.lchop('prefix') == 'ed string' 'string with suffix'.rchop('suffix') == 'string with ' 'prefix and suffix.chop('prefix', 'suffix') == ' and ' ...
6
by: LongBow | last post by:
Hello all, I am having a little problems getting String's StartsWith and EndsWith methods. If I have a string defined as sNormalPt which equals "0x11D0" then use the following command ...
2
by: Alan Silver | last post by:
Hello, I have code like the following... foreach (Control ctl in Page.Controls) { if (ctl.ID.StartsWith("X_")) { // do stuff } }
3
by: trint | last post by:
Note: My question is after the following code. I have this call: Navigate2(@"http://www.mySite.com"); Using this code: private void Navigate2(String address) { if...
9
by: shapper | last post by:
Hello, How can I filter a List(Of String)? I need to get the list elements which start with the letters contained in the variable Text. Thanks, Miguel
3
by: cschuy2006 | last post by:
Lucy 1 1 1 3 2 3 3 1 3 3 3 3 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 2 3 3 3 1 1 2 3 2 3 3 2 John 3 3 3 3 3 3 3 3 1 3 3 3 2 2 2 3 2 2 2 2 2 3 2 1
6
by: Extremest | last post by:
Does anyone know how to decode a yenc encoded file? I have created a decoder that seems to work great for text. Now i am trying to create another one for images and I can't get it to work. I...
3
by: afrobeard | last post by:
The following following code fails with the failiure:- File "test.py", line 27, in __main__.sanitize_number Failed example: sanitize_number('0321-4683113') Expected: '03214683113' Got:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.