如何:从字符串中读取字符
更新: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();
}
}