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
|
static void Main(string[] args)
{
// La chaîne initiale
string maChaîne = "mot1_mot2_mot3_mot4";
// La chaîne une fois transformée
string maNouvelleChaîne = Transform(maChaîne, '_');
// Affichage du résultat
Console.WriteLine(maNouvelleChaîne);
Console.Read();
}
/// <summary>
/// La fonction de transformation.
/// </summary>
/// <param name="chaîne">La chaîne à transformer.</param>
/// <param name="séparateur">Le séparateur (dans notre cas: _).</param>
/// <returns>Chaîne transformée.</returns>
static string Transform(string chaîne, char séparateur)
{
// Utilisation d'un StringBuilder (pourrait être remplacé par un simple string).
StringBuilder result = new StringBuilder();
// Séparation de la chaîne en sous chaînes (mots dans notre cas).
string[] mots = chaîne.Split(séparateur);
// Traitement de chaque mot séparément.
foreach (string s in mots)
{
// Variable qui va nous servir à stocker les caractères composants le mot.
string motFinal = "";
// Récupération de chacun des caractères formant le mot (utile pour mettre en majuscule la première lettre).
char[] mot = s.ToLower().ToCharArray();
// Ajout de la première lettre (mise en majuscule).
motFinal += mot[0].ToString().ToUpper();
// Ajout des autres lettres.
for (int i = 1; i < mot.Length; i++)
{
motFinal += mot[i].ToString();
}
// Ajout de notre mot dans le StringBuilder.
result.Append(motFinal);
}
// On retourne le résultat sous la forme d'une chaîne.
return result.ToString();
} |