How to: Read Characters from a String


The following code examples show how to read characters synchronously and asynchronously from a string.

This example reads 13 characters synchronously from a string, stores them in an array, and displays those characters. It then reads the remaining characters in the string, stores them in the array starting at the sixth element, and displays the contents of the array.

using namespace System;
using namespace System::IO;

public ref class CharsFromStr
    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.
        // 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.
        // Close the StringReader.

int main()
// The example has the following output:
// Some number o
// Some f characters

The next example reads all the characters asynchronously from a TextBox control, and stores them in an array. It then asynchronously writes each letter or white space character on a separate line followed by a line break to a TextBlock control.

No code example is currently available or this language may not be supported.

Asynchronous File I/O
NIB: How to: Create a Directory Listing
How to: Read and Write to a Newly Created Data File
How to: Open and Append to a Log File
How to: Read Text from a File
How to: Write Text to a File
How to: Write Characters to a String
File and Stream I-O