gpt4 book ai didi

java - 如何最好地删除文件中的某些字符串?

转载 作者:行者123 更新时间:2023-12-01 14:03:42 24 4
gpt4 key购买 nike

如果我有一个包含以下内容的文件:

11:17 GET this is my content #2013
11:18 GET this is my content #2014
11:19 GET this is my content #2015

如何使用扫描仪并忽略`String line =scanner.nextLine();的某些部分?

我想要的结果是:

this is my content
this is my content
this is my content

所以我想从头开始直到 GET 为止的所有内容,然后获取 # 字符之前的所有内容。

如何轻松做到这一点?

最佳答案

您可以使用String.indexOf(String str)String.indexOf(char ch)方法。例如:

String line = scanner.nextLine();
int start = line.indexOf("GET");
int end = line.indexOf('#');
String result = line.substring(start + 4, end);

关于java - 如何最好地删除文件中的某些字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19112392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com