字符串分割c#有类似的分隔符

本文关键字:分隔符 分割 字符串 | 更新日期: 2023-09-27 18:18:59

我想用分隔符':'分割下面的字符串

string s = key1:::key2:value1:::value2;

那么my字符串将被分割成

s1 = key1:::key2
s2 = value1:::value2 

我使用分隔符':'来分割字符串,但它给了我以下结果

s1 = key1
s2 = ""...etc

请帮助我如何分割上述字符串。提前感谢

字符串分割c#有类似的分隔符

string s = "key1:::key2:value1:::value2";
string[] parts = Regex.Split(s, "(?<!:):(?!:)");
Firstly replace ":::" with "$"
s.replace(":::",$).Split(':')
s1 = key1$key2
s2 = value1$value2 

now replace "$" with ":::"
s1 = key1:::key2
s2 = value1:::value2

用这个

Regex.Split("key1:::key2:value1:::value2", "(?<!:):(?!:)");

现场演示

试试:

string s = "key1:::key2:value1:::value2";
string[] spl = s.replace(":::","|").split(':');
spl[0] = spl[0].replace("|",":::");
spl[1] = spl[1].replace("|",":::");