字符串拆分C#具有类似的分隔符

时间:2014-08-21 07:06:21

标签: c# string

我想用分隔符':'

拆分以下字符串
string s = key1:::key2:value1:::value2;

所以我的字符串会分成

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

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

s1 = key1
s2 = ""...etc

请帮我分解上面的字符串。 提前致谢

4 个答案:

答案 0 :(得分:4)

string s = "key1:::key2:value1:::value2";
string[] parts = Regex.Split(s, "(?<!:):(?!:)");

答案 1 :(得分:1)

Firstly replace ":::" with "$"

s.replace(":::",$).Split(':')

s1 = key1$key2
s2 = value1$value2 


now replace "$" with ":::"

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

答案 2 :(得分:1)

使用此

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

Live Demo

答案 3 :(得分:0)

试试这样:

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