방법: 문자열에 문자 쓰기

다음 코드 예제는 문자 배열 내의 지정된 위치에서부터 지정된 수만큼의 문자를 기존 문자열에 씁니다. 아래에 나타난 것과 같이 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를 사용하는 것을 보여 줍니다. StringBuilder 클래스는 System.Text 네임스페이스의 멤버이므로 using 선언이 추가로 필요합니다. 또한 문자열을 정의하여 문자 배열로 변환하는 대신 문자 배열을 직접 만들어 이를 초기화합니다.

다음과 같이 출력됩니다.

Some number of characters to

참고 항목

작업

방법: 디렉터리 목록 만들기

방법: 새로 만든 데이터 파일 읽기 및 쓰기

방법: 로그 파일 열기 및 추가

방법: 파일의 텍스트 읽기

방법: 파일에 텍스트 쓰기

방법: 문자열에서 문자 읽기

참조

StringWriter

StringWriter.Write

StringBuilder

개념

기본 파일 I/O