C# 如何获取字符串的一部分直到某个符号,然后在符号之后作为单独的值

C# 如何获取字符串的一部分直到某个符号,然后在符号之后作为单独的值,c#,regex,string,linq,char,C#,Regex,String,Linq,Char,如果我有字符串,好方法是什么 string str = "hello, world = how are you~hello"; 我在文本中允许的一组特定符号是:“=”、“~”、“\”、“”、“^”、“” 若字符串包含了这些符号中的任何一个,首先获取值,或者正确地说,整个字符串存在于特定符号之前的部分,然后分别获取特定符号之后的部分字符串 hello, world how are you hello 要为字符串中的前3个符号分别附加每个变量,请执行以下操作: string part1 = he

如果我有字符串,好方法是什么

string str = "hello, world = how are you~hello";
我在文本中允许的一组特定符号是:“
=
”、“
~
”、“
\
”、“
”、“
^
”、“

若字符串包含了这些符号中的任何一个,首先获取值,或者正确地说,整个字符串存在于特定符号之前的部分,然后分别获取特定符号之后的部分字符串

hello, world
how are you
hello
要为字符串中的前3个符号分别附加每个变量,请执行以下操作:

string part1 = hello, world
string part2 = how are you
string part3 = hello
所以如果字符串是:

  string str = "hello, world = how are you~hello \ ok";
结果将是:

string part1 = hello, world
string part2 = how are you
string part3 = hello \ ok
有一种方法使这变得非常简单:

string str = "hello, world = how are you~hello \\ ok";
var results = str.Split(new [] { '=', '~', '\\', '.', '^', '#'}, 3);

// results[0] = "hello, world "
// results[1] = " how are you"
// results[2] = "hello \ ok"

第二个参数是要获取的子字符串的最大数目。

编写一个正则表达式,该正则表达式可以满足您的需要。如果遇到麻烦,请就遇到的具体问题寻求帮助。这不是免费的代码编写服务。请提供一些代码,然后提问