I need to be able to find specific characters entered into a text box. I need to find out what the 2 characters before and after [space]A[space] are.

For example if "RED ROSES AND A YELLOW ONE" was entered in the text box I need a way of finding the "ND" of "AND" and the "YE" of "YELLOW".

I'm currently attempting to put the text box value into a character array and then loop through it looking for " " and "A" and " " and then get the characters before and after but I'm having all sorts of issues. I'm not married to this way of doing it but any way that will actually work.

I've been beating my head against this issue for a week and am about to go crazy. Thanks for the help.

Recommended Answers

All 3 Replies

string msg = "RED ROSES AND A YELLOW ONE";
string needle = TextBox1.Text;
int offset = msg.IndexOf(needle);
			
while (offset >= 0) {
  Console.WriteLine(msg.Substring(offset - 2, 7));
  offset = msg.IndexOf(needle, offset + 1);
}

is this anything like what you mean?

commented: Yes! +13

I think the OP meant more something like this:

string msg = "RED ROSES AND A YELLOW ONE";//typed into textbox
            string needle = " A "; 
            int offset = msg.IndexOf(needle);
            string before = msg.Substring(offset - 2, 2);
            string after = msg.Substring(offset + 3, 2);

Oh right, of course ^_^;
I shouldn't try to answer questions after bed-time :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.