Επεξεργασία

Κοινή χρήση μέσω


StringReader.ReadLine Method

Definition

Reads a line of characters from the current string and returns the data as a string.

public:
 override System::String ^ ReadLine();
public override string ReadLine ();
public override string? ReadLine ();
override this.ReadLine : unit -> string
Public Overrides Function ReadLine () As String

Returns

The next line from the current string, or null if the end of the string is reached.

Exceptions

The current reader is closed.

There is insufficient memory to allocate a buffer for the returned string.

Examples

This code example is part of a larger example provided for the StringReader class.

// From textReaderText, create a continuous paragraph 
// with two spaces between each sentence.
   String^ aLine;
String^ aParagraph;
StringReader^ strReader = gcnew StringReader( textReaderText );
while ( true )
{
   aLine = strReader->ReadLine();
   if ( aLine != nullptr )
   {
      aParagraph = String::Concat( aParagraph, aLine,  " " );
   }
   else
   {
      aParagraph = String::Concat( aParagraph,  "\n" );
      break;
   }
}

Console::WriteLine(  "Modified text:\n\n{0}", aParagraph );
// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
    aLine = strReader.ReadLine();
    if(aLine != null)
    {
        aParagraph = aParagraph + aLine + " ";
    }
    else
    {
        aParagraph = aParagraph + "\n";
        break;
    }
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph);
' From textReaderText, create a continuous paragraph 
' with two spaces between each sentence.
Dim aLine, aParagraph As String
Dim strReader As New StringReader(textReaderText)
While True
    aLine = strReader.ReadLine()
    If aLine Is Nothing Then
        aParagraph = aParagraph & vbCrLf
        Exit While
    Else
        aParagraph = aParagraph & aLine & " "
    End If
End While
Console.WriteLine("Modified text:" & vbCrLf & vbCrLf & _ 
    aParagraph)

Remarks

This method overrides the TextReader.ReadLine method.

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), a carriage return immediately followed by a line feed ("\r\n"), or the end-of-stream marker. The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end-of-stream marker has been reached. That is to say, if there is nothing between the last line read and the end-of-stream marker, the method returns null.

If the current method throws an OutOfMemoryException, the reader's position in the underlying string is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Because the position of the reader in the string cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StringReader. To avoid such a situation, use the Read method and store the read characters in a preallocated buffer.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. How to: Write Text to a File
Write to a text file. How to: Write Text to a File
Read from a text file. How to: Read Text from a File
Append text to a file. How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText
Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. How to: Read and Write to a Newly Created Data File
Write to a binary file. How to: Read and Write to a Newly Created Data File

Applies to

See also