5
How do I get an index of a value of mine array? For example:
char[] letras = new char[]{'a','b','c','d'};
In letras
, i would like to pick up the value index b
for example. How do I do this in C#?
5
How do I get an index of a value of mine array? For example:
char[] letras = new char[]{'a','b','c','d'};
In letras
, i would like to pick up the value index b
for example. How do I do this in C#?
8
If you are still learning, the leonardosnt response code is more didactic.
If you don’t use the method IndexOf()
array class.
char[] letras = new char[] { 'a', 'b', 'c', 'd' };
int indice = Array.IndexOf(letras, 'c');
or, simplifying:
char[] letras = { 'a', 'b', 'c', 'd' };
var indice = Array.IndexOf(letras, 'c');
6
You can use a loop to scroll through the array items until you find the desired one.
For example:
char[] letras = new char[]{'a','b','c','d'};
int indice = -1;
// Percorre todas as letras
for (int i = 0; i < letras.Length; i++) {
// Verifica se a letra no índice 'i' é igual à letra c.
if (letras[i] == 'c') {
indice = i;
break; // Para o loop
}
}
// Se o indice for -1 aqui significa que o item que você está procurando não está no array.
Browser other questions tagged c# array
You are not signed in. Login or sign up in order to post.