简体   繁体   English

检查字符串中的字符是1还是0

[英]Check if a character in a string is a 1 or a 0

I am recieveing a string via a com port that looks like this *000000, or *110101, or anything in between. 我通过一个看起来像这个* 000000或* 110101的com端口接收一个字符串,或者介于两者之间的任何东西。 Each character represents the status of a relay on a PCB I have and the first character after the asterisk represents the first relay and so on. 每个字符代表我所拥有的PCB上的继电器状态,星号代表第一个继电器后的第一个字符,依此类推。 I need to check each character seperatly if it is a 1 or a 0 and then set a label to either ON or OFF accordingly. 我需要分别检查每个字符是1还是0,然后相应地将标签设置为ON或OFF。

My code would be something like this: 我的代码是这样的:

if(char 1 == "1")
{

    label4.Text = "ON";    
}
else
{
    label4.Text = "OFF";
}

and so on down the line for all 6 characters. 等等所有6个字符。 But I don't know how to seperate each character. 但我不知道如何分隔每个角色。

Thanks! 谢谢!

Sounds like you want to use the indexer on String to extract a single char : 听起来你想String上使用索引器来提取一个char

string text = "*110101"; // Wherever you get this from...
label4.Text = text[1] == '1' ? "ON" : "OFF";
label5.Text = text[2] == '1' ? "ON" : "OFF";
label6.Text = text[3] == '1' ? "ON" : "OFF";
label7.Text = text[4] == '1' ? "ON" : "OFF";
label8.Text = text[5] == '1' ? "ON" : "OFF";
label9.Text = text[6] == '1' ? "ON" : "OFF";

This assumes that you're happy for the label text to be set to "OFF" for any value other than '1'. 这假设您很高兴标签文本对于除“1”以外的任何值设置为“OFF”。 (As noted in comments, you use double quotes for string literals, but single quotes for character literals.) (如注释中所述,您对字符串文字使用双引号,但对字符文字使用单引号。)

Note how I've used the conditional operator here to simplify the code: if you want to basically choose between two values ("ON" and "OFF") based on a condition, the conditional operator is much simpler than an if / else . 注意我在这里使用条件运算符来简化代码:如果你想基于条件基本上在两个值(“ON”和“OFF”)之间进行选择,条件运算符比if / else简单得多。 Don't overdo it, of course, but it's worth becoming familiar with. 当然,不要过度,但值得熟悉。

However, I would also suggest you might want to put the relevant labels into a collection. 但是,我还建议您将相关标签放入集合中。 Then you could use something like: 然后你可以使用类似的东西:

for (int i = 0; i < 6; i++)
{
    toggles[i].Text = text[i + 1] == '1' ? "ON" : "OFF";
}

You already have the string? 你已经有了这个字符串?

You can use indexers on the string object to access the characters. 您可以在字符串对象上使用索引器来访问字符。

Example: 例:

string s = "110110";
if (s[3] == '0') label4.Text = "ON";

you can get specific char by : 你可以得到具体的char:

string relaysStatus = "100110"; string relaysStatus =“100110”;
char c = relaysStatus.ElementAt(0); char c = relaysStatus.ElementAt(0);

or you can convert then all to bool list: 或者你可以将所有转换为bool列表:

var relays = relaysStatus.Select(q => q == '1').ToList(); var relayys = relaysStatus.Select(q => q =='1')。ToList();

Moving further, you can create extension method for converting chars to On/Off strings: 更进一步,您可以创建用于将字符转换为On / Off字符串的扩展方法:

public static class CharExtensions
{
    public static string ToOnOff(this char ch)
    {
        return (ch == '1') ? "On" : "Off";
    }
}

After that you can use it (pretty clean code): 之后你可以使用它(非常干净的代码):

label1.Text = input[0].ToOnOff(); 
label2.Text = input[1].ToOnOff();

UPDATE: I think good point to check both '0' and '1' values. 更新:我认为检查“0”和“1”值都很好。 But it depends on your input string. 但这取决于你的输入字符串。

public static class CharExtensions
{
    public static string ToOffOn(this char ch)
    {
        switch (ch)
        {
            case '0' : return "Off";
            case '1': return "On";
            default:
                return ch.ToString(); // Or rise exception
        }            
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM