[英]how to split a string on every parenthese and keep all text into an array in c#
I'm trying to split a string on every parenthese into an array and keep all text in C#, get everything in the parenthese. 我正在尝试将每个括号上的字符串拆分成一个数组并将所有文本保存在C#中,将所有内容都放在括号中。
Example: " hmmmmmmmm (asdfhqwe)asasd " 示例:“ hmmmmmmmm(asdfhqwe)asasd ”
Should become: " hmmmmmmmm ", " (asdfhqwe) " and " asasd ". 应该成为:“ hmmmmmmmm ”,“ (asdfhqwe) ”和“ asasd ”。
My current setup is only able to take everything inside the parentheses and discards the rest. 我目前的设置只能将所有内容都放在括号内并丢弃其余部分。
var output = input.Split('(', ')').Where((item, index) => index % 2 != 0).ToList();
How would i go forward to do such thing (disregarding my current code) ? 我将如何继续做这样的事情(无视我目前的代码)?
试试这个:
string[] subString = myString.Split(new char[] { '(', ')' });
Oky so I do not know what the real string will look like in your application, but based on the provided string this will be my hack of a solution: Oky所以我不知道真正的字符串在你的应用程序中会是什么样子,但根据提供的字符串,这将是我的解决方案:
string sample = "hmmmmmmmm (asdfhqwe)asasd";
var result = sample.Replace("(", ",(").Replace(")", "),").Split(',');
So i replaced where the split should be with a comma, but you can use any other char that might never occur in your string, Say like the '~' could also work. 所以我用逗号替换了split的位置,但是你可以使用你的字符串中可能永远不会出现的任何其他字符,比如'〜'也可以。
But not knowing all the required functionality, this would work for above scenario. 但是不知道所有必需的功能,这将适用于上述场景。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.