Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Reading characters in a string in C#

String Reader is used to read the characters in a string

  1. using System;
  2. using System.IO;
  3.  
  4. public class ForgetCode
  5. {
  6. public static void Main()
  7. {
  8. string str = "Coming from Forget Code";
  9. char[] b = new char[str.Length];
  10.  
  11. using (StringReader sr = new StringReader(str))
  12. {
  13. // Read 15 characters from the string into the array.
  14. sr.Read(b, 0, 15);
  15. Console.WriteLine(b);
  16. // Read the rest of the string starting at the current string position.
  17. // Put in the array starting at the 6th array member.
  18. sr.Read(b, 6, str.Length-15);
  19. Console.WriteLine(b);
  20. }
  21. }
  22. }

Output:
Coming from For
Comingget Coder

Explanation:
  1. sr.Read(b, 6, str.Length-15);
  2. Console.WriteLine(b);

The above code produces the output as Comingget Coder since the char array already have the data Coming from For.
It overwrites the the values from 7th position of the array to the defined point.