Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to figure out how to compare certain part of the number in textbox1 with certain part of the number from textbox2 in C# window forms at button click.

For ex.
textbox1 value 1232016000001 (13 digit)
textbox2 value 1201502212121 (13 digit)

I ma trying to compare year value from textbox1 which is 2016 to value 2015 from other textbox. I need advice what can I use or this purpose.

All help is appreciated.

Thanks.

What I have tried:

I am trying to get some idea like, if I can user array or list.
Posted
Updated 14-Jun-16 12:29pm
v3
Comments
BillWoodruff 14-Jun-16 17:35pm    
If the text content of each data item is always 13 characters and you know the date is in characters 4~7 of that string, this is very simple. You should make an attempt to write this yourself, first studying the Methods provided by the 'String Class.

Note: your second string has 12 characters, not 13.
Member 12076824 14-Jun-16 17:38pm    
Thanks for reply Bill.Both are supposed to be 13, I will give that a try.
BillWoodruff 14-Jun-16 18:40pm    
Good ! Keep in mind that if you don't have full control over the data you are handling, you may need to do some checking for valid data. imho a good programming practice is to always check and validate data as "early" in the code as you can. Mistakes will happen :)

1 solution

string txt1= txt1.Text.Substring(3, 4).ToString();
string txt2= txt2.Text.Substring(1, 4).ToString();

if(txt1 !=txt2)
{
do something
}
 
Share this answer
 
v2
Comments
BillWoodruff 14-Jun-16 21:48pm    
Please do not post code here as a solution that you have not verified is correct.

Your first use of 'Substring will return "201600" ... the second use will return "2015"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900