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

다음 코드 예제를 사용하면 기존 문자열의 지정된 위치에서 시작하여 지정된 개수 만큼의 문자를 읽을 수 있습니다. 아래에 나타난 것과 같이 StringReader를 사용하여 이를 수행합니다.

이 코드는 문자열을 정의하고 이 문자열을 문자배열로 변환합니다. 그런 다음 적절한 StringReader.Read 메서드를 사용하여 읽습니다.

이 예제는 다음과 같이 문자열에서 지정된 개수의 문자만 읽습니다.

예제

Imports System
Imports System.IO

Public Class CharsFromStr
    Public Shared Sub Main()
        ' Create a string to read characters from.
        Dim str As String = "Some number of characters"
        ' Make a char array the size of the source string
        Dim b(str.Length) As Char
        ' Create an instance of StringReader and attach it to the string.
        Dim sr As New 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()
    End Sub
End Class
' The example has the following output:
'
' Some number o
' Some f characters
using System;
using System.IO;

public 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
        char[] b = new char[str.Length];
        // Create an instance of StringReader and attach it to the string.
        StringReader sr = new 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();
    }
}
// The example has the following output:
//
// Some number o
// Some f characters
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

참고 항목

작업

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

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

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

방법: 파일의 텍스트 읽기

방법: 파일에 텍스트 쓰기

방법: 문자열에 문자 쓰기

참조

StringReader

StringReader.Read

개념

기본 파일 I/O