java中如何查找字符串中的EOF?

发布于 2024-12-07 06:12:32 字数 344 浏览 0 评论 0原文

我正在从事学校项目。他们告诉我,我将得到一个字符串,其中包含一个实际的程序,例如......

导入java.io.*\n公共类A{\n...........EOF

我的工作是在该字符串(程序)中查找特定的正则表达式。

现在我的问题是..

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

在上面的代码中,如何查找字符串中是否达到 EOF?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城孤痞 2024-12-14 06:12:33

BufferedReader rd=new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt"),"UTF-8"));

  char ch;
  int r;
  while(true)
  {
    r=rd.read();    //Reading a character in integer format
    if(r==-1)   //Checking for the End of File
      break;
    else
    {
      ch=(char)r;   //Converting to char format
  System.out.println(ch);
    }
  }

BufferedReader rd=new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt"),"UTF-8"));

  char ch;
  int r;
  while(true)
  {
    r=rd.read();    //Reading a character in integer format
    if(r==-1)   //Checking for the End of File
      break;
    else
    {
      ch=(char)r;   //Converting to char format
  System.out.println(ch);
    }
  }
极度宠爱 2024-12-14 06:12:32

您不太可能需要这个 - 您可能只需要读取到字符串末尾,并且由于它是文件内容的表示,因此您的老师将其称为 EOF。

然而……

有一个字符叫EOF。它也称为 control-Z,因为这就是您键入它的方式。如果你想将它包含在字符串中,你必须将其输入为“\u001a”,如下所示:

String iHaveAnEof = "file ends here\u001a";

如果你真的需要这个,你的老师可能比我年长,而且我可能年纪大到可以成为你的祖父;- )

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 ;-)

无敌元气妹 2024-12-14 06:12:32

字符串中没有 EOF 字符。您只需要 迭代字符串中的字符

for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);        
    //Process char
}

There is no EOF character in a string. You just need to iterate over the characters in the string:

for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);        
    //Process char
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文