gpt4 book ai didi

c# - 在 C# 中删除字符串中的重复子字符串

转载 作者:行者123 更新时间:2023-12-04 16:17:08 26 4
gpt4 key购买 nike

如何删除字符串中的重复子字符串?例如,如果我有一个像 smith:rodgers:someone:smith:white 这样的字符串那么我怎样才能得到一个新的字符串,它有额外的 smith删除喜欢 smith:rodgers:someone:white .我也想保留冒号,即使它们是重复的。

非常感谢

最佳答案

string input = "smith:rodgers:someone:smith:white";
string output = string.Join(":", input.Split(':').Distinct().ToArray());

当然,此代码假定您只是在寻找重复的“字段”值。这不会删除以下字符串中的“smithsmith”:
"smith:rodgers:someone:smithsmith:white"

可以编写一个算法来做到这一点,但很难让它高效......

关于c# - 在 C# 中删除字符串中的重复子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3586053/

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