Como: Gravar uma seqüência de caracteres

O exemplo de código a seguir grava um determinado número de caracteres de uma matriz de caracteres em uma sequência existente, começando em um local especificado na matriz. Use StringWriter para fazer isso, como demonstrado abaixo.

Exemplo

Imports System
Imports System.IO
Imports System.Text

Public Class CharsToStr
    Public Shared Sub Main()
        ' Create an instance of StringBuilder that can then be modified.
        Dim sb As New StringBuilder("Some number of characters")
        ' Define and create an instance of a character array from which
        ' characters will be read into the StringBuilder.
        Dim b() As Char = {" ","t","o"," ","w","r","i","t","e"," ","t","o","."}
        ' Create an instance of StringWriter
        ' and attach it to the StringBuilder.
        Dim sw As New StringWriter(sb)
        ' Write three characters from the array into the StringBuilder.
        sw.Write(b, 0, 3)
        ' Display the output.
        Console.WriteLine(sb)
        ' Close the StringWriter.
        sw.Close()
    End Sub
End Class
using System;
using System.IO;
using System.Text;

public class CharsToStr
{
    public static void Main()
    {
        // Create an instance of StringBuilder that can then be modified.
        StringBuilder sb = new StringBuilder("Some number of characters");
        // Define and create an instance of a character array from which
        // characters will be read into the StringBuilder.
        char[] b = {' ','t','o',' ','w','r','i','t','e',' ','t','o','.'};
        // Create an instance of StringWriter
        // and attach it to the StringBuilder.
        StringWriter sw = new StringWriter(sb);
        // Write three characters from the array into the StringBuilder.
        sw.Write(b, 0, 3);
        // Display the output.
        Console.WriteLine(sb);
        // Close the StringWriter.
        sw.Close();
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Text;

public ref class CharsToStr
{
public:
    static void Main()
    {
        // Create an instance of StringBuilder that can then be modified.
        StringBuilder^ sb = gcnew StringBuilder("Some number of characters");
        // Define and create an instance of a character array from which
        // characters will be read into the StringBuilder.
        array<Char>^ b = {' ','t','o',' ','w','r','i','t','e',' ','t','o','.'};
        // Create an instance of StringWriter
        // and attach it to the StringBuilder.
        StringWriter^ sw = gcnew StringWriter(sb);
        // Write three characters from the array into the StringBuilder.
        sw->Write(b, 0, 3);
        // Display the output.
        Console::WriteLine(sb);
        // Close the StringWriter.
        sw->Close();
    }
};

int main()
{
    CharsToStr::Main();
}

Programação robusta

Este exemplo ilustra o uso de um StringBuilder para modificar uma sequência existente. Observe que isso requer uma declaração using adicional, já que a classe StringBuilder é um membro do namespace System.Text. Além disso, em vez de definir uma sequência de caracteres e convertê-lo em uma matriz de caracteres, este é um exemplo de como criar uma matriz de caracteres diretamente e inicializá-la.

Esse código produz a seguinte saída.

Some number of characters to

Consulte também

Tarefas

Como: Criar uma listagem de diretório

Como: Ler e gravar em um arquivo de dados recém-criado

Como: Abrir e anexar um arquivo de Log

Como: Ler texto de um arquivo.

Como: Gravar texto em um arquivo

Como: Caracteres de leitura de uma seqüência

Referência

StringWriter

StringWriter.Write

StringBuilder

Conceitos

Arquivo básico de E/S