gpt4 book ai didi

java - 按行提取子字符串

转载 作者:搜寻专家 更新时间:2023-11-01 01:00:55 24 4
gpt4 key购买 nike

我对编程还很陌生,所以请多多包涵。假设我有一个像这样的大字符串。

String story = "This is the first line.\n"
+ "This is the second line.\n"
+ "This is the third line\n"
+ "This is the fourth line.\n"
+ "This is the fifth line.";

我将如何提取第一行、第四行等等?

最佳答案

如果你想避免创建数组,你可以使用Scanner

Scanner scanner = new Scanner(story);
while(scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}

关于java - 按行提取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451903/

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