gpt4 book ai didi

c# - 将字符串拆分为具有特定条件的多个字符串

转载 作者:行者123 更新时间:2023-11-30 14:06:23 25 4
gpt4 key购买 nike

我想根据以下条件将一个字符串拆分为多个字符串:

  • 必须至少包含 2 个单词
  • 每个词必须相邻

例如:“你好,你好吗”我想拆分成:

  • “你好,你好吗”
  • “你好,你好吗”
  • “你好”
  • “怎么样”
  • “你好吗”
  • “你是吗”

不能重复多次。

到目前为止我得到的是:

string input = "hello how are you";
List<string> words = input.Split(' ').ToList();
List<string> inputs = new List<string>();

string temp = String.Empty;

for (int i = 0; i < words.Count; i++)
{
temp += words[i] + " ";
if (i > 0)
{
inputs.Add(temp);
}
}

输出如下:

hello how 
hello how are
hello how are you

我也想得到其他人,需要一点帮助。

最佳答案

一种方法是遍历每个单词并获取其所有可能的序列。

例子:

string input = "hello how are you";
List<string> words = input.Split(' ').ToList();
List<string> inputs = new List<string>();

for (int i = 0; i < words.Count; i++)
{
var temp = words[i];
for(int j = i+1;j < words.Count;j++) {
temp += " " + words[j];
inputs.Add(temp);
}
}
//hello how
//hello how are
//hello how are you
//how are
//how are you
//are you

关于c# - 将字符串拆分为具有特定条件的多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48012226/

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