Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using string split in C#

Tags:

string

c#

split

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?

like image 776
JCC Avatar asked Apr 23 '26 19:04

JCC


2 Answers

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.

like image 198
Ken Wayne VanderLinde Avatar answered Apr 25 '26 08:04

Ken Wayne VanderLinde


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();
like image 31
Ed S. Avatar answered Apr 25 '26 08:04

Ed S.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!