Reputation: 2225
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)";
Upvotes: 3
Views: 4227
Reputation: 29036
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.RemoveEmptyEntries
will helps you to remove empty entries from the split result
Upvotes: 7
Reputation: 11808
If I understand your question correctly, you have a string like this:
"(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)"
and want output like this:
"(value1)&&(value2)&&(value3)||(value4)&&(value5)||(value6)"
Where each of the value*
values is found by looking up a matched string (for instance "abc"
) in a dictionary you supply.
If that is the case you can use a regular expression with a MatchEvaluator that does the dictionary lookup.
class Program
{
private static Dictionary<string, string> _dict = new Dictionary<string, string>
{
{"a", "value1"},
{"abc", "value2"},
{"5", "value3"},
{"rfd", "value4"},
{"nn", "value5"},
{"iu", "value6"}
};
private static void Main(string[] args)
{
string input = "(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)";
string output = Regex.Replace(input, @"(?<=\()\w+(?=\))", Lookup);
Console.WriteLine(output);
}
private static string Lookup(Match m)
{
string result;
_dict.TryGetValue(m.Value, out result);
return result;
}
}
The regex (?<=\()\w+(?=\))
matches a non zero length string consisting of upper or lower cases letters, the underscore or digits: \w+
, a look behind assertion for the opening parenthesis (?<=\()
and a look ahead assertion for the closing parenthesis (?=\))
.
Note that one of the strings that is matched in the input string "hh"
is not in the dictionary you supply. I have chosen to return null
in that case, you might want to throw an exception or handle such an error in another way.
In simple cases, the MatchEvaluator
can be replaced by a lambda expression.
Upvotes: 2
Reputation: 33
You should try this code :
string value = "(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)";
string[] splitVal = value.Split(new string[] { "&&", "||" },StringSplitOptions.None);
Upvotes: -3