방법: 문자열에서 문자 읽기
업데이트: 2007년 11월
다음 코드 예제를 사용하면 기존 문자열의 지정된 위치에서 시작하여 지정된 개수 만큼의 문자를 읽을 수 있습니다. 아래에 나타난 것과 같이 StringReader를 사용하여 이를 수행합니다.
이 코드는 문자열을 정의하고 이 문자열을 문자배열로 변환합니다. 그런 다음 적절한 StringReader.Read 메서드를 사용하여 읽습니다.
이 예제는 다음과 같이 문자열에서 지정된 개수의 문자만 읽습니다.
Some number o
예제
Option Explicit On
Option Strict On
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"
' Size the array to hold all the characters of the string
' so that they are all accessible.
Dim b(24) As Char
' Create an instance of StringReader and attach it to the string.
Dim sr As New StringReader(str)
' Read 13 characters from the array that holds the string, starting
' from the first array member.
sr.Read(b, 0, 13)
' Display the output.
Console.WriteLine(b)
' Close the StringReader.
sr.Close()
End Sub
End Class
using System;
using System.IO;
public class CharsFromStr
{
public static void Main(String[] args)
{
// Create a string to read characters from.
String str = "Some number of characters";
// Size the array to hold all the characters of the string
// so that they are all accessible.
char[] b = new char[24];
// Create an instance of StringReader and attach it to the string.
StringReader sr = new StringReader(str);
// Read 13 characters from the array that holds the string, starting
// from the first array member.
sr.Read(b, 0, 13);
// Display the output.
Console.WriteLine(b);
// Close the StringReader.
sr.Close();
}
}