I am having trouble splitting a string in C# with a delimiters && and ||.
For example the string could look like:
"(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)"
Code:
string[] value = arguments.Split(new string[] { "&&" }, StringSplitOptions.None);
I need to split or retrieve values in array without () braces - I need thte output to be
"abc" "rfd" "5" "nn" "iu"
and I need it in an array
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("a", "value1");
dict.Add("abc", "value2");
dict.Add("5", "value3");
dict.Add("rfd", "value4");
dict.Add("nn", "value5");
dict.Add("iu", "value6");
foreach (string s in strWithoutBrackets)
{
foreach (string n in dict.Keys)
{
if (s == n)
{
//if match then replace dictionary value with s
}
}
}
///Need output like this
string outputStr = "(value1)&&(value2)&&(value3)||(value4)&&(value5)||(value6)";
You should try these:
string inputStr = "(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)";
string[] strWithoutAndOR = inputStr.Split(new string[] { "&&","||" }, StringSplitOptions.RemoveEmptyEntries);
string[] strWithoutBrackets = inputStr.Split(new string[] { "&&","||","(",")" }, StringSplitOptions.RemoveEmptyEntries);
Check out this working Example
As per MSDN docs: String.Split Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array. The split method is having few overloaded methods, you can make use of String.Split Method (String[], StringSplitOptions) for this scenario, where you can specify the subStrings that you want to refer for the split operation. The
StringSplitOptions.RemoveEmptyEntrieswill helps you to remove empty entries from the split result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With