gpt4 book ai didi

java - 分割字符串的数字列表

转载 作者:行者123 更新时间:2023-11-30 02:56:41 26 4
gpt4 key购买 nike

我有一个字符串列表,但没有正则表达式方面的技能。所以我需要你的帮助。

第一个字符串:

[begin]
65. This tutorial walks you through a series of exercises to get familiar with Kotlin.
66. Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax.
67. Each exercise is created as a failing unit test and your job is to make it pass. You can play with Koans online or offline by cloning the project on GitHub:
...
[end]

第二个字符串

[begin]
63. Download the Koans by cloning the project from GitHub
64. Open up the project in IntelliJ IDEA or your favorite editor. Note: If IntelliJ IDEA prompts you to update the Kotlin library, just click yes.
[end]

以及其他字符串...

目标:

我需要将每个数字行提取为String:

String#1 : 63. Download the Koans by cloning the project from GitHub
String#2 : 64. Open up the project in IntelliJ IDEA or your favorite editor. Note: If IntelliJ IDEA prompts you to update the Kotlin library, just click yes.
String#3 : 65. This tutorial walks you through a series of exercises to get familiar with Kotlin.
String#4 : 66. Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax.
String#5 : 67. Each exercise is created as a failing unit test and your job is to make it pass. You can play with Koans online or offline by cloning the project on GitHub:

附注将非常感谢任何用于技能提升正则表达式的在线培训工具。

最佳答案

我会这样走:

  1. 定义一个类来解析行和文本..(您将需要它来对最终的列表进行排序)

    class TextLine implements Comparable<TextLine> {

    private int line;
    private String lineText;

    public TextLine(int line, String lineText) {
    this.line = line;
    this.lineText = lineText;
    }

    @Override
    public String toString() {
    return "String:" + line + lineText;
    }

    @Override
    public int compareTo(TextLine o) {
    return Integer.compare(this.line, o.line);
    }
    }
  2. 读取并解析您在 List 中找到的所有行(您是对的,正则表达式是获取它的一种方法)

  3. 使用Collections.sort对列表进行排序...

  4. 打印

示例:

public class TestApp {

private List<TextLine> output = new ArrayList<TextLine>();

public static void main(String[] args) {
TestApp tt = new TestApp();
String str = "65. This tutorial walks you through a series of exercises to get familiar with Kotlin. ";
tt.populateLsit(str);
str = "67. Each exercise is created as a failing unit test and your job is to make it pass. You can play with Koans online or offline by cloning the project on GitHub:";
tt.populateLsit(str);
str = "66. Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax. ";
tt.populateLsit(str);
str = "63. Download the Koans by cloning the project from GitHub";
tt.populateLsit(str);
str = "64. Open up the project in IntelliJ IDEA or your favorite editor. Note: If IntelliJ IDEA prompts you to update the Kotlin library, just click yes.";
tt.populateLsit(str);
tt.sortPrint();
}

private void sortPrint() {
Collections.sort(output);
System.out.println(output);

}

private void populateLsit(String str) {
Matcher match = Pattern.compile("^(\\d+)|(.*)").matcher(str);
// String r = match.group();
match.find();
int i = Integer.parseInt(match.group());
match.find();
String s = match.group();
output.add(new TextLine(i, s));
}
}
<小时/>

Here is a good starting point for regex .

关于java - 分割字符串的数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37056568/

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