3

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)";
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
ND's
  • 2,155
  • 6
  • 38
  • 59

3 Answers3

7

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

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Dictionary dict = new Dictionary(); dict.Add("a", "value1"); dict.Add("abc", "value2"); dict.Add("5b", "value3"); dict.Add("hh", "value4"); dict.Add("nn", "value5"); dict.Add("iu", "value6"); – ND's Nov 08 '17 at 05:49
  • @John: Well, have you checked my code, working example? – sujith karivelil Nov 08 '17 at 05:49
  • 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)"; – ND's Nov 08 '17 at 05:49
  • thanks lucky it works i just want to match arrary value with dictionary and make a new string please check updated question – ND's Nov 08 '17 at 05:51
  • Probably that would be a different question. anyway what's wrong with that – sujith karivelil Nov 08 '17 at 05:55
  • ya thasts different question but i need to replace dictionay key value if match array and form a new string – ND's Nov 08 '17 at 05:57
  • @John: It needs a different approach. Since you need to add the removed brackets, && and || back to the new string. Which means a replacement is needed to perform on the initial string for each matching keys – sujith karivelil Nov 08 '17 at 06:16
2

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.

heijp06
  • 11,558
  • 1
  • 40
  • 60
-3

You should try this code :

string value = "(abc)&&(rfd)&&(5)||(hh)&&(nn)||(iu)";
string[] splitVal = value.Split(new string[] { "&&", "||" },StringSplitOptions.None);
Mehul Prajapati
  • 33
  • 1
  • 11