Procedura: Leggere caratteri da una stringaHow to: Read Characters from a String

Gli esempi di codice seguenti illustrano come leggere i caratteri in modo sincrono e asincrono da una stringa.The following code examples show how to read characters synchronously and asynchronously from a string.

EsempioExample

Questo esempio legge 13 caratteri in modo sincrono da una stringa, li archivia in una matrice e li visualizza.This example reads 13 characters synchronously from a string, stores them in an array, and displays those characters. Legge quindi i caratteri rimanenti nella stringa, li archivia nella matrice a partire dal sesto elemento e visualizza il contenuto della matrice.It then reads the remaining characters in the string, stores them in the array starting at the sixth element, and displays the contents of the array.

using namespace System;
using namespace System::IO;

public ref class CharsFromStr
{
public:
    static void Main()
    {
        // Create a string to read characters from.
        String^ str = "Some number of characters";
        // Make a char array the size of the source string
        array<Char>^ b = gcnew array<Char>(str->Length);
        // Create an instance of StringReader and attach it to the string.
        StringReader^ sr = gcnew StringReader(str);
        // Read 13 characters into the array that holds the string,
        // starting at the third array member.
        sr->Read(b, 0, 13);
        // Display the output.
        Console::WriteLine(b);
        // Read the rest of the string from the current position in the
        // source string into the array, starting at the 6th array member.
        sr->Read(b, 5, str->Length - 13);
        // Display the output.
        Console::WriteLine(b);
        // Close the StringReader.
        sr->Close();
    }
};

int main()
{
    CharsFromStr::Main();
}
// The example has the following output:
//
// Some number o
// Some f characters
using System;
using System.IO;

public class CharsFromStr
{
    public static void Main()
    {
        string str = "Some number of characters";
        char[] b = new char[str.Length];

        using (StringReader sr = new StringReader(str))
        {
            // Read 13 characters from the string into the array.
            sr.Read(b, 0, 13);
            Console.WriteLine(b);

            // Read the rest of the string starting at the current string position.
            // Put in the array starting at the 6th array member.
            sr.Read(b, 5, str.Length - 13);
            Console.WriteLine(b);
        }
    }
}
// The example has the following output:
//
// Some number o
// Some f characters
Imports System
Imports System.IO

Public Class CharsFromStr
    Public Shared Sub Main()
        Dim str As String = "Some number of characters"
        Dim b(str.Length) As Char

        Using sr As StringReader = New StringReader(str)
            ' Read 13 characters from the string into the array.
            sr.Read(b, 0, 13)
            Console.WriteLine(b)

            ' Read the rest of the string starting at the current string position.
            ' Put in the array starting at the 6th array member.
            sr.Read(b, 5, str.Length - 13)
            Console.WriteLine(b)
        End Using
    End Sub
End Class
' The example has the following output:
'
' Some number o
' Some f characters

EsempioExample

L'esempio seguente legge tutti i caratteri in modo asincrono da un controllo TextBox e li archivia in una matrice.The next example reads all the characters asynchronously from a TextBox control, and stores them in an array. Scrive quindi in modo asincrono ogni lettera o spazio su una riga separata seguita da un'interruzione di riga in un controllo TextBlock.It then asynchronously writes each letter or white space character on a separate line followed by a line break to a TextBlock control.

using System;
using System.Text;
using System.Windows;
using System.IO;

namespace WpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void ReadButton_Click(object sender, RoutedEventArgs e)
        {
            char[] charsRead = new char[UserInput.Text.Length];
            using (StringReader reader = new StringReader(UserInput.Text))
            {
                await reader.ReadAsync(charsRead, 0, UserInput.Text.Length);
            }

            StringBuilder reformattedText = new StringBuilder();
            using (StringWriter writer = new StringWriter(reformattedText))
            {
                foreach (char c in charsRead)
                {
                    if (char.IsLetter(c) || char.IsWhiteSpace(c))
                    {
                        await writer.WriteLineAsync(char.ToLower(c));
                    }
                }
            }
            Result.Text = reformattedText.ToString();
        }
    }
}
Imports System.IO
Imports System.Text

Class MainWindow

    Private Async Sub ReadButton_Click(sender As Object, e As RoutedEventArgs)
        Dim charsRead As Char() = New Char(UserInput.Text.Length) {}
        Using reader As StringReader = New StringReader(UserInput.Text)
            Await reader.ReadAsync(charsRead, 0, UserInput.Text.Length)
        End Using

        Dim reformattedText As StringBuilder = New StringBuilder()
        Using writer As StringWriter = New StringWriter(reformattedText)
            For Each c As Char In charsRead
                If Char.IsLetter(c) Or Char.IsWhiteSpace(c) Then
                    Await writer.WriteLineAsync(Char.ToLower(c))
                End If
            Next
        End Using
        Result.Text = reformattedText.ToString()
    End Sub
End Class

Vedere ancheSee Also

StringReader
StringReader.Read
I/O di file asincronoAsynchronous File I/O
Procedura: creare una visualizzazione directoryNIB: How to: Create a Directory Listing
Procedura: Leggere e scrivere su un file di dati appena creatoHow to: Read and Write to a Newly Created Data File
Procedura: Aprire e accodare un file di logHow to: Open and Append to a Log File
Procedura: Leggere testo da un fileHow to: Read Text from a File
Procedura: Scrivere un testo in un fileHow to: Write Text to a File
Procedura: Scrivere caratteri in una stringaHow to: Write Characters to a String
I/O di file e di flussiFile and Stream I/O