Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to the Scanner class in C# for strings?

In Java I can pass a Scanner a string and then I can do handy things like, scanner.hasNext() or scanner.nextInt(), scanner.nextDouble() etc.

This allows some pretty clean code for parsing a string that contains rows of numbers.

How is this done in C# land?

If you had a string that say had:

"0 0 1 22 39 0 0 1 2 33 33" 

In Java I would pass that to a scanner and do a

while(scanner.hasNext())      myArray[i++] = scanner.nextInt(); 

Or something very similar. What is the C#' ish way to do this?

like image 323
mmcdole Avatar asked Apr 06 '09 16:04

mmcdole


People also ask

Can we use Scanner in C#?

This blog defines scanner class in C#. I was in need to create a small application using WIA (windows image acquisition) to use in scanning images and save them as a single file a PDF file as an electronic archive using of course a data base to stock information about each file.

What is a syntax for Scanner class?

Create a Scanner Object in Java Once we import the package, here is how we can create Scanner objects. // read input from the input stream Scanner sc1 = new Scanner(InputStream input); // read input from files Scanner sc2 = new Scanner(File file); // read input from a string Scanner sc3 = new Scanner(String str);

How do I scan with C sharp?

Connect to the scanner using the DeviceInfo instance. Select the scanner through the element with index 1 inside the items property with the connection instance. Use the Transfer method of the selected scanner and provide as first argument the output format of the scanned image. Save the returned image data to a file.

What is the package name for Scanner class?

The Scanner class is used to get user input, and it is found in the java.util package.


1 Answers

I'm going to add this as a separate answer because it's quite distinct from the answer I already gave. Here's how you could start creating your own Scanner class:

class Scanner : System.IO.StringReader {   string currentWord;    public Scanner(string source) : base(source)   {      readNextWord();   }    private void readNextWord()   {      System.Text.StringBuilder sb = new StringBuilder();      char nextChar;      int next;      do      {         next = this.Read();         if (next < 0)            break;         nextChar = (char)next;         if (char.IsWhiteSpace(nextChar))            break;         sb.Append(nextChar);      } while (true);      while((this.Peek() >= 0) && (char.IsWhiteSpace((char)this.Peek())))         this.Read();      if (sb.Length > 0)         currentWord = sb.ToString();      else         currentWord = null;   }    public bool hasNextInt()   {      if (currentWord == null)         return false;      int dummy;      return int.TryParse(currentWord, out dummy);   }    public int nextInt()   {      try      {         return int.Parse(currentWord);      }      finally      {         readNextWord();      }   }    public bool hasNextDouble()   {      if (currentWord == null)         return false;      double dummy;      return double.TryParse(currentWord, out dummy);   }    public double nextDouble()   {      try      {         return double.Parse(currentWord);      }      finally      {         readNextWord();      }   }    public bool hasNext()   {      return currentWord != null;   } } 
like image 143
BlueMonkMN Avatar answered Sep 21 '22 11:09

BlueMonkMN