Check character by position in C#

Asked

Viewed 40 times

-1

Good evening, developing a program in C# for the first time, I’ve done this program in JS and now I’m learning C# and passing the program to him. I need to check character by character of a variable, in JS I only had to put the variable and the position of the character I wanted to see (variable[0] //first character of the variable). it is possible to do this check in C#? I have a variable with a binary number inside, I need to check in which positions appear the digit 1.

for (var i = 0; i < nums_mapa_bits.length; i++) {

        nums_mapa_bits[i] == 1 ? bits_ativos.push(i + 1) : '';

    }

the variable 'nums_mapa_bits' is with the binary inside, I want to play this in C#, for now I have not found a way to do this.

1 answer

0

using System;
using System.Collections.Generic;

public class Program
{
  public static void Main()
  {
    string nums_mapa_bits = "0101000111";
    List<int> bits_ativos = new List<int>();

    for(var i=0; i< nums_mapa_bits.Length; i++)
    {
       if(nums_mapa_bits[i] == '1')
       {
          Console.WriteLine($"Posicao {i} é ativo");
          bits_ativos.Add(i);
    
       }
    }
  }
}

In C# you can access the element of a string by the index. Try using this code above and adapt to your case. I considered that this binary number you have is a string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.