如何使得在 .txt 文件中位于“,”之后的所有内容可以被单独检查?

huangapple go评论71阅读模式
英文:

How can i make that everything after "," in .txt file will be checked separetly :)?

问题

我需要改进这段代码,它将从名为 file.txt 的文本文件中读取内容。

我想要使用以下形式:

file = login.getText();
if (file.equals("sergy"))

我只需要做一些能够将文本文件中的内容分别读取出来的操作,并忽略逗号“,”,或者除逗号“,”之外的其他内容。

英文:

I need to improve this code that will read from a text file named file.txt

>sergy,many,mani,kserder

I would like to use this form :

file = login.getText();
if (file.equals("sergy"))

I just need to do something that will read everything in the text file separately and ignoring "," sign, or something else other than "," sign.

答案1

得分: 1

你可以通过逗号字符,来分割字符串,并检查数组中的任何元素是否等于你要查找的值:

file = login.getText();
if (Arrays.asList(file.split(",")).contains("sergy")) {
    // 做些什么...
英文:

You could split the string by the , character and check if any of the array's elements are equal to the value you're looking for:

file = login.getText();
if (Arrays.asList(file.split(",")).contains("sergy")) {
    // do something...

huangapple
  • 本文由 发表于 2020年4月10日 18:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61138286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定