How to find EOF in a string in java?

Muthu Ganapathy Nathan picture Muthu Ganapathy Nathan · Sep 29, 2011 · Viewed 41.9k times · Source

I am working in school project. In that what they told is, I will be given a String which contains an actual program like....

import java.io.*\npublic class A{\n...........EOF

And My job is to find particular regular expressions in that String(Program).

Now My question is..

void myFunc(String s)
{
 while(s.charAt(i) != EOF) /*needed replacement for EOF*/
 {
 // Actual Code
 }
}

In the above code, how to find whether EOF is reached in a string?

Answer

Ed Staub picture Ed Staub · Sep 29, 2011

It's unlikely that you need this - you probably just need to read till the end of the string, and since it's a representation of a file's contents, your teacher referred to it as EOF.

However...

There is a character called EOF. It's also called control-Z, because that's how you type it. If you want to include it in a string, you have to type it as "\u001a" as in:

String iHaveAnEof = "file ends here\u001a";

If you really need this, your teacher is probably older than me, and I'm probably old enough to be your grandfather ;-)