Jesse Blanke
Jesse Blanke

Reputation: 21

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!

Upvotes: 2

Views: 767

Answers (4)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

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

Upvotes: 1

HB MAAM
HB MAAM

Reputation: 4002

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();

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500835

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";
}

Upvotes: 8

user974608
user974608

Reputation: 242

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";

Upvotes: 4

Related Questions