Condividi tramite


Stringhe

Aggiornamento: novembre 2007

In C# una stringa è costituita da un gruppo di uno o più caratteri dichiarati tramite la parola chiave string, che corrisponde alla versione abbreviata della classe System.String nel linguaggio C#. In C# le stringhe sono più facilmente utilizzabili e meno soggette a errori di programmazione rispetto alle matrici di caratteri in C o C++.

Una stringa letterale viene dichiarata utilizzando le virgolette, come illustrato nell'esempio riportato di seguito:

string greeting = "Hello, World!";

È possibile estrarre sottostringhe e concatenare stringhe nel modo seguente:

string s1 = "A string is more ";
string s2 = "than the sum of its chars.";

// Concatenate s1 and s2. This actually creates a new
// string object and stores it in s1, releasing the
// reference to the original object.
s1 += s2;

System.Console.WriteLine(s1);
// Output: A string is more than the sum of its chars.

Gli oggetti stringa non sono modificabili una volta creati. I metodi che intervengono sulle stringhe in realtà restituiscono nuovi oggetti stringa. Di conseguenza, per motivi di prestazioni, per concatenazioni estese o altre modifiche complesse di stringhe è opportuno utilizzare la classe StringBuilder, come illustrato negli esempi di codice riportati di seguito.

Utilizzo delle stringhe

Caratteri di escape

È possibile includere nelle stringhe caratteri di escape come "\n" (nuova riga) e "\t" (tabulazione). La riga:

string columns = "Column 1\tColumn 2\tColumn 3";
//Output: Column 1        Column 2        Column 3

string rows = "Row 1\r\nRow 2\r\nRow 3";
/* Output:
  Row 1
  Row 2
  Row 3
*/

string title = "\"The \u00C6olean Harp\", by Samuel Taylor Coleridge";
//Output: "The Æolean Harp", by Samuel Taylor Coleridge

corrisponde a:

Hello

World!

Se si desidera includere una barra rovesciata, quest'ultima deve essere preceduta da un'altra barra rovesciata. La stringa riportata di seguito:

         string filePath = @"C:\Users\scoleridge\Documents\";
         //Output: C:\Users\scoleridge\Documents\

         string text = @"My pensive SARA ! thy soft cheek reclined
Thus on mine arm, most soothing sweet it is
To sit beside our Cot,...";
         /* Output:
         My pensive SARA ! thy soft cheek reclined
            Thus on mine arm, most soothing sweet it is
            To sit beside our Cot,... 
         */

         string quote = @"Her name was ""Sara.""";
         //Output: Her name was "Sara."

corrisponde effettivamente a:

\\My Documents\

Simbolo @

Il simbolo @ indica che i caratteri di escape e le interruzioni di riga devono essere ignorate durante la creazione della stringa. Di conseguenza le due stringhe riportate di seguito sono identiche:

string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";

ToString()

I tipi di dati incorporati in C# forniscono tutti il metodo ToString, che converte un valore in una stringa. Questo metodo può essere utilizzato per convertire valori numerici in stringhe, nel modo seguente:

int year = 1999;
string msg = "Eve was born in " + year.ToString();
System.Console.WriteLine(msg);  // outputs "Eve was born in 1999"

Accesso a caratteri singoli

È possibile accedere a caratteri singoli contenuti in una stringa tramite metodi quali Substring, Replace, Split e Trim.

string s3 = "Visual C# Express";
System.Console.WriteLine(s3.Substring(7, 2));
// Output: "C#"

System.Console.WriteLine(s3.Replace("C#", "Basic"));
// Output: "Visual Basic Express"

// Index values are zero-based
int index = s3.IndexOf("C");
// index = 7

È anche possibile copiare i caratteri in una matrice di caratteri, nel modo seguente:

string question = "hOW DOES mICROSOFT wORD DEAL WITH THE cAPS lOCK KEY?";
System.Text.StringBuilder sb = new System.Text.StringBuilder(question);

for (int j = 0; j < sb.Length; j++)
{
    if (System.Char.IsLower(sb[j]) == true)
        sb[j] = System.Char.ToUpper(sb[j]);
    else if (System.Char.IsUpper(sb[j]) == true)
        sb[j] = System.Char.ToLower(sb[j]);
}
// Store the new string.
string corrected = sb.ToString();
System.Console.WriteLine(corrected);
// Output: How does Microsoft Word deal with the Caps Lock key?            

È possibile accedere ai singoli caratteri derivati da una stringa con un indice, nel modo seguente:

string s5 = "Printing backwards";

for (int i = 0; i < s5.Length; i++)
{
    System.Console.Write(s5[s5.Length - i - 1]);
}
// Output: "sdrawkcab gnitnirP"

Modifica della combinazione di maiuscole e minuscole

Per modificare le lettere in una stringa in maiuscole o minuscole, utilizzare ToUpper() o ToLower(), nel modo seguente:

string s6 = "Battle of Hastings, 1066";

System.Console.WriteLine(s6.ToUpper());
// outputs "BATTLE OF HASTINGS 1066"
System.Console.WriteLine(s6.ToLower());
// outputs "battle of hastings 1066"

Confronti

Il modo migliore per confrontare due stringhe non localizzate è utilizzare il metodo Equals con StringComparison.Ordinal e StringComparison.OrdinalIgnoreCase.

// Internal strings that will never be localized.
string root = @"C:\users";
string root2 = @"C:\Users";

// Use the overload of the Equals method that specifies a StringComparison.
// Ordinal is the fastest way to compare two strings.
bool result = root.Equals(root2, StringComparison.Ordinal);

Console.WriteLine("Ordinal comparison: {0} and {1} are {2}", root, root2,
                    result ? "equal." : "not equal.");

// To ignore case means "user" equals "User". This is the same as using
// String.ToUpperInvariant on each string and then performing an ordinal comparison.
result = root.Equals(root2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Ordinal ignore case: {0} and {1} are {2}", root, root2,
                     result ? "equal." : "not equal.");

// A static method is also available.
bool areEqual = String.Equals(root, root2, StringComparison.Ordinal);


// String interning. Are these really two distinct objects?
string a = "The computer ate my source code.";
string b = "The computer ate my source code.";

// ReferenceEquals returns true if both objects
// point to the same location in memory.
if (String.ReferenceEquals(a, b))
    Console.WriteLine("a and b are interned.");
else
    Console.WriteLine("a and b are not interned.");

// Use String.Copy method to avoid interning.
string c = String.Copy(a);

if (String.ReferenceEquals(a, c))
    Console.WriteLine("a and c are interned.");
else
    Console.WriteLine("a and c are not interned.");

Gli oggetti stringa dispongono anche di un metodo CompareTo() che restituisce un valore integer basato sul fatto che una stringa sia minore (<) o maggiore (>) di un'altra. Durante il confronto delle stringhe, viene utilizzato il valore Unicode e le lettere minuscole hanno un valore inferiore alle lettere maiuscole.

// Enter different values for string1 and string2 to
// experiement with behavior of CompareTo
string string1 = "ABC";
string string2 = "abc";

int result2 = string1.CompareTo(string2);

if (result2 > 0)
{
    System.Console.WriteLine("{0} is greater than {1}", string1, string2);
}
else if (result2 == 0)
{
    System.Console.WriteLine("{0} is equal to {1}", string1, string2);
}
else if (result2 < 0)
{
    System.Console.WriteLine("{0} is less than {1}", string1, string2);
}
// Output: ABC is less than abc

Per cercare una stringa all'interno di un'altra stringa, utilizzare il metodo IndexOf(). Il metodo IndexOf() restituisce -1 se la stringa di ricerca non viene trovata; in caso contrario, restituisce l'indice in base zero della prima posizione in cui è presente.

// Date strings are interpreted according to the current culture.
// If the culture is en-US, this is interpreted as "January 8, 2008",
// but if the user's computer is fr-FR, this is interpreted as "August 1, 2008"
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);            
Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", dt.Year, dt.Month, dt.Day);

// Specify exactly how to interpret the string.
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);

// Alternate choice: If the string has been input by an end user, you might 
// want to format it according to the current culture:
// IFormatProvider culture = System.Threading.Thread.CurrentThread.CurrentCulture;
DateTime dt2 = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
Console.WriteLine("Year: {0}, Month: {1}, Day {2}", dt2.Year, dt2.Month, dt2.Day);

/* Output (assuming first culture is en-US and second is fr-FR):
    Year: 2008, Month: 1, Day: 8
    Year: 2008, Month: 8, Day 1
 */

Suddivisione di una stringa in sottostringhe

La suddivisione di una stringa in sottostringhe, analoga alla suddivisione di una frase in singole parole, è una comune attività di programmazione. Il metodo Split() accetta una matrice char di delimitatori, ad esempio uno spazio e restituisce una matrice di sottostringhe. È possibile accedere a questa matrice con foreach, nel modo seguente:

string numString = "1287543"; //"1287543.0" will return false for a long
long number1 = 0;
bool canConvert = long.TryParse(numString, out number1);
if (canConvert == true)
  Console.WriteLine("number1 now = {0}", number1);
else
  Console.WriteLine("numString is not a valid long");

byte number2 = 0;
numString = "255"; // A value of 256 will return false
canConvert = byte.TryParse(numString, out number2);
if (canConvert == true)
  Console.WriteLine("number2 now = {0}", number2);
else
  Console.WriteLine("numString is not a valid byte");

decimal number3 = 0;
numString = "27.3"; //"27" is also a valid decimal
canConvert = decimal.TryParse(numString, out number3);
if (canConvert == true)
  Console.WriteLine("number3 now = {0}", number3);
else
  Console.WriteLine("number3 is not a valid decimal");            

Questo codice visualizza ogni parola su una riga separata, nel modo seguente:

The

cat

sat

on

the

mat.

Utilizzo di StringBuilder

La classe StringBuilder crea un buffer di stringhe che offre prestazioni migliori se il programma esegue numerose modifiche di stringhe. La classe StringBuilder consente inoltre di riassegnare singoli caratteri, operazione non supportata dal tipo di dati di stringa incorporato.

In questo esempio viene creato un oggetto StringBuilder e i relativi contenuti vengono aggiunti uno per uno tramite il metodo Append.

class TestStringBuilder
{
    static void Main()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        // Create a string composed of numbers 0 - 9
        for (int i = 0; i < 10; i++)
        {
            sb.Append(i.ToString());
        }
        System.Console.WriteLine(sb);  // displays 0123456789

        // Copy one character of the string (not possible with a System.String)
        sb[0] = sb[9];

        System.Console.WriteLine(sb);  // displays 9123456789
    }
}

Vedere anche

Attività

Procedura: generare stringhe letterali con più righe (Visual C#)

Procedura: cercare una stringa in una matrice di stringhe

Procedura: cercare all'interno di una stringa

Concetti

Nozioni di base del linguaggio C#

Tipi di dati incorporati (Visual C# Express)

Riferimenti

string (Riferimenti per C#)