Je dois écrire une fonction qui retourne tous les caractères qui apparaissent 2 fois ou plus dans le texte. Il n'y a pas de problème quand je l'utilise sans fonction (par exemple sur un clic de bouton). Voilà comment je le fais:C#. Comment retourner un caractère?
for (int i = 0; i < alph.Length; i++) // alph is my text(string)
{
int count = allText.Split(alphCh[i]).Length - 1;
if (count >= 2)
listView2.Items.Add(alphCh[i].ToString());
}
Voilà comment j'écris la fonction:
public char[] chars2(string text)
{
char[] allChar = text.ToCharArray();
string allText = text.ToString();
string allTextL = text.ToLower();
string alph = "abcdefghijklmnopqrstuvwxyz";
char[] alphCh = alph.ToCharArray();
char[] result = new char[0];
int allcount = 0;
for (int i = 0; i < alph.Length; i++)
{
int count = allText.Split(alphCh[i]).Length - 1;
if (count >= 2)
{
allcount++;
result = new char[allcount];
for (int j = 0; j < allcount; j++)
{
result[j] = alphCh[i];
return result;
}
}
}
return result;
}
Mais la fonction retourne tout premier caractère qui se produit 2 fois ou plus dans le texte. Par exemple j'écris abcbca - func renvoie a, et je veux func retourne a, b, c, pour l'écrire dans ListView par exemple. Qu'est-ce que je fais de mal? S'il te plaît, j'ai tellement besoin de ton aide. Merci.
Le retour dans la boucle arrête la boucle. –
Non, cela peut se produire 2 fois ou plus n'importe où dans le texte: abcdabcd, retourne aussi a, b, c, d. – Daria
Yochai Timmer, merci, mais que faire? J'ai essayé d'écrire continue mais ce n'est pas utile. – Daria