gpt4 book ai didi

kotlin - 如何高效解析?

转载 作者:行者123 更新时间:2023-12-02 12:44:57 24 4
gpt4 key购买 nike

假设我想解析以下字符串:“01:12 Joseph We will have a meeting at 6 PM.”。在 C 我可以做(伪代码):

struct data {
char *time;
char *name;
char *message;
};

char *string = "01:12 Joseph We will have a meeting at 6 PM."
struct data notification;
notification.time = strtok(string, " ");
notification.name = strtok(NULL, " ");
notification.message = strtok(NULL, " ");
puts(notification.time); // prints the time it was sent
puts(notification.name); // prints the name of the sender
puts(notification.message); // prints the message content
请注意,我仍在使用相同的缓冲区,我认为这是使用 kotlin 的最佳方式。现在的问题是,将所有数据保留在同一个缓冲区中更好还是创建一个新的时间对象,另一个名称和消息对象更好?
您应该将其视为不断出现的新通知流,并且您必须解析每个通知才能将其放置在窗口内您想要的任何位置,这将非常有用,例如用于聊天应用程序。所以它是最适合这种用例的任何东西。

最佳答案

最简单的方法可能是:

val (time, name, message) = s.split(" ", limit=3)
https://pl.kotl.in/s7jznsszJ

关于kotlin - 如何高效解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62583817/

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