[英]Using string split in C#
I am attempting to read in from a txt file strings that are very similar to the following: 我试图从一个非常类似于以下的txt文件字符串读入:
YXCZ0000292=TRUE YXCZ0000292 = TRUE
or 要么
THS83777930=FALSE 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. 我需要使用字符串拆分来收集序列号并将其放入我稍后可以使用的变量中,以及使用字符串的true或false部分来设置复选框。 The serial numbers will never be the same and the TRUE or FALSE portion can be random.
序列号永远不会相同,TRUE或FALSE部分可以是随机的。 Anyone have a good way to handle this one?
任何人都有办法处理这个问题吗?
Given any string
called line
, you should be able to do 给定任何名为
line
string
,您应该能够做到
var parts = line.Split('=');
var serial = parts[0];
var boolean = bool.Parse(parts[1]);
I'm thinking that should work as needed. 我认为应该按需要工作。
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();
var ss = String.Split('=');
Console.WriteLine(ss[0]); //YXCZ0000292
Console.WriteLine(ss[1]); //TRUE
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. 现在parts [0]是序列号,part [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()
请参见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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.