简体   繁体   中英

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. 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. 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.

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. 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 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'. (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 . 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 :

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

or you can convert then all to bool list:

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

Moving further, you can create extension method for converting chars to On/Off strings:

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. 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
        }            
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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