1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| using System;
using System.Collections.Generic;
using System.Linq;
namespace TestDoublon
{
class Program
{
private static List<string> Nom = new List<string>();
public static string NouvelleEntree { get; set; }
private static void Main(string[] args)
{
Nom.Add("Texte");
Nom.Add("TexteN1");
Nom.Add("TexteN6");
Nom.Add("TexteN5");
Nom.Add("TexteN3");
Nom.Add("TexteN4");
Nom.Add("TexteN20");
Nom.Add("TexteN19");
Nom.Add("TexteN7");
Nom.Add("TexteN2");
NouvelleEntree = "Texte";
chercherDoublon();
Console.WriteLine(NouvelleEntree);
Console.Read();
}
private static void chercherDoublon()
{
foreach (string NomDejaDansListe in Nom)
{
if (NouvelleEntree == NomDejaDansListe)
{
var FinChaine = NouvelleEntree.Split('N').Last();
if (FinChaine == NouvelleEntree)
{
FinChaine = "N1";
NouvelleEntree = NouvelleEntree + FinChaine;
}
else
{
int CountFinChaine = FinChaine.Count();
int CountChaineComplete = NouvelleEntree.Count();
string NouveauDebutChaine = NouvelleEntree.Substring(0, CountChaineComplete-CountFinChaine);
int NouveauFinChaine = Convert.ToInt16(FinChaine) + 1;
NouvelleEntree = NouveauDebutChaine + NouveauFinChaine;
chercherDoublon();
}
}
}
}
}
} |
Partager