방법: 문자열에서 문자 읽기How to: Read characters from a string

다음 코드 예제는 문자열에서 동기식 또는 비동기식으로 문자를 읽는 방법을 보여줍니다.The following code examples show how to read characters synchronously or asynchronously from a string.

예: 동기식으로 문자 읽기Example: Read characters synchronously

이 예제에서는 문자열에서 동기식으로 13자를 읽고, 배열에 저장하고, 표시합니다.This example reads 13 characters synchronously from a string, stores them in an array, and displays them. 그런 다음, 문자열의 나머지 문자를 읽고, 6번째 요소에서 시작하는 배열에 저장하고, 배열의 콘텐츠를 표시합니다.The example then reads the rest of the characters in the string, stores them in the array starting at the sixth element, and displays the contents of the array.

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.IO

Public Class CharsFromStr
    Public Shared Sub Main()
        Dim str As String = "Some number of characters"
        Dim b(str.Length - 1) 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

예: 비동기식으로 문자 읽기Example: Read characters asynchronously

다음 예제는 WPF 앱의 코드입니다.The next example is the code behind a WPF app. 창 로드 시, 이 예제는 TextBox 컨트롤에서 모든 문자를 비동기식으로 읽고 배열에 저장합니다.On window load, the example asynchronously reads all characters from a TextBox control and stores them in an array. 그런 다음, 각 문자 또는 공백 문자를 TextBlock 컨트롤의 별도 라인에 비동기식으로 씁니다.It then asynchronously writes each letter or white-space character to a separate line of a TextBlock control.

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

namespace StringReaderWriter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Window_Loaded(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

''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>

Partial Public Class MainWindow
    Inherits Window
    Public Sub New()
        InitializeComponent()
    End Sub
    Private Async Sub Window_Loaded(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

참조See also