3

I am attempting to read in from a txt file strings that are very similar to the following:

YXCZ0000292=TRUE

or

THS83777930=FALSE

I need to use string split to collect a serial number and put it into a variable I can use later as well as use the true or false portion of the string to set a check box. The serial numbers will never be the same and the TRUE or FALSE portion can be random. Anyone have a good way to handle this one?

2
  • 2
    What have you already tried (i.e. do you have a specific problem)? Commented Aug 4, 2011 at 23:47
  • Please give more instruction/details before giving downvotes for new users. This question is not a too bad question for a new user I think. Commented Aug 4, 2011 at 23:49

5 Answers 5

3

Given any string called line, you should be able to do

var parts = line.Split('=');
var serial = parts[0];
var boolean = bool.Parse(parts[1]);

I'm thinking that should work as needed.

Sign up to request clarification or add additional context in comments.

3 Comments

currently, you have "TRUE" or "FALSE" string value in your "boolean" variable. You should use Boolean.Parse
Good point, I'll throw that in (I overlooked that fact that he wanted to use them with a checkbox).
Works like a champ. Thanks for the quick replies!
2
string s = "THS83777930=FALSE";
var parts = s.Split( '=' );

// error checking here, i.e., make sure parts.Length == 2
var serial = parts.First();
var booleanValue = parts.Last();

Comments

1
var ss = String.Split('=');
Console.WriteLine(ss[0]); //YXCZ0000292
Console.WriteLine(ss[1]); //TRUE

Comments

1

Assuming the text file contains only one serial and value:

string text=File.ReadAllText("c:\filePath.txt");
string[] parts=text.split("=");

Now parts[0] is the serial and parts[1] is the boolean.

Comments

1

All of the above should work correctly. Once you need to set some checkbox value, you should have a boolean value parsed. See Boolean.Parse()

string s = "YXCZ0000292=TRUE";
string[] parts = s.Split('=');
string serial = parts[0];
bool value = Boolean.Parse(parts[1].ToLower());

to set checkbox value just use checked

checkbox.checked = value

1 Comment

You don't the call the ToLower since Boolean.Parse works in a case-insentive manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.