如何:向字符串写入字符

下面的代码示例把从字符数组中指定位置开始的一定数目的字符写入现有的字符串。 使用 StringWriter 执行此操作,如下所示。

示例

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
Imports System.Text

Module Example
   Public Sub Main()
      Dim sb As New StringBuilder()
      Dim flag As Boolean = True
      Dim spellings() As String = { "recieve", "receeve", "receive" }
      sb.AppendFormat("Which of the following spellings is {0}:", flag)
      sb.AppendLine()
      For ctr As Integer = 0 To spellings.GetUpperBound(0)
         sb.AppendFormat("   {0}. {1}", ctr, spellings(ctr))
         sb.AppendLine()
      Next
      sb.AppendLine()
      Console.WriteLine(sb.ToString())
   End Sub
End Module
' The example displays the following output:
'       Which of the following spellings is True:
'          0. recieve
'          1. receeve
'          2. receive
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 System;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder();
      bool flag = true;
      string[] spellings = { "recieve", "receeve", "receive" };
      sb.AppendFormat("Which of the following spellings is {0}:", flag);
      sb.AppendLine();
      for (int ctr = 0; ctr <= spellings.GetUpperBound(0); ctr++) {
         sb.AppendFormat("   {0}. {1}", ctr, spellings[ctr]);
         sb.AppendLine();
      }
      sb.AppendLine();
      Console.WriteLine(sb.ToString());
   }
}
// The example displays the following output:
//       Which of the following spellings is True:
//          0. recieve
//          1. receeve
//          2. receive
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();
}

可靠编程

此示例演示如何使用 StringBuilder 来修改现有的字符串。 请注意,这需要一个附加的 using 声明,因为 StringBuilder 类是 System.Text 命名空间的成员。 另外,这是一个直接创建字符数组并对其进行初始化的示例,而不是定义字符串然后将字符串转换为字符数组。

此代码产生以下输出:

Some number of characters to

请参见

任务

如何:创建目录列表

如何:对新建的数据文件进行读取和写入

如何:打开并追加到日志文件

如何:从文件读取文本

如何:向文件写入文本

如何:从字符串中读取字符

参考

StringWriter

StringWriter.Write

StringBuilder

概念

基本的文件 I/O