1/// <summary>
2/// Il metodo mette in maiuscolo l'iniziale di alcune o tutte le parole del testo in input.
3/// </summary>
4/// <param name="Testo">Testo da modificare.</param>
5/// <param name="TutteLeParole">Booleano che dice se la modifica è da applicare su tutte o su alcune parole.</param>
6/// <returns>Stringa contenente il testo modificato.</returns>
7public static String CapitalizeFirstChar(String Testo, Boolean TutteLeParole)
8{
9 Testo = Testo.Trim().ToLower();
10
11 if (String.IsNullOrEmpty(Testo)) return String.Empty;
12
13 if (TutteLeParole)
14 {
15 if (Testo.Length > 1)
16 {
17
18 if (Testo.Contains(" "))
19 {
20 Int32 ind = Testo.IndexOf(' ');
21
22 return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole) + " " + CapitalizeFirstChar(Testo.Substring(ind + 1), TutteLeParole);
23 }
24 else if (Testo.Contains("."))
25 {
26 Int32 ind = Testo.IndexOf('.');
27
28 //se il carattere separatore è in fondo alla stringa evitiamo
29 //la chiamata al metodo con la stringa vuota
30 if (ind != Testo.Length - 1)
31 {
32 return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole) + "." + CapitalizeFirstChar(Testo.Substring(ind + 1), TutteLeParole);
33 }
34 else
35 return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole);
36 }
37 else if (Testo.Contains("'"))
38 {
39 Int32 ind = Testo.IndexOf("'");
40
41 if (ind != Testo.Length - 1)
42 {
43 return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole) + "'" + CapitalizeFirstChar(Testo.Substring(ind + 1), TutteLeParole);
44 }
45 else return CapitalizeFirstChar(Testo.Substring(0, ind), TutteLeParole);
46 }
47 //caso base: capitalizziamo la stringa
48 else return Testo[0].ToString().ToUpper() + Testo.Substring(1, Testo.Length - 1);
49 }
50 else return Testo.ToString().ToUpper();
51 }
52 else
53 {
54 return Testo[0].ToString().ToUpper() + Testo.Substring(1, Testo.Length - 1);
55 }
56 }