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)";
7
  • And the expected output is.? Commented Nov 8, 2017 at 5:29
  • Why have you got spaces around the && in " && "? Commented Nov 8, 2017 at 5:30
  • You can write your own parser - or look into regular expressions. Commented Nov 8, 2017 at 5:31
  • What's not working with what you have already done? Commented Nov 8, 2017 at 5:32
  • 1
    @John - Why do you expect new string[] { "&&" } to remove || and ( and )? Isn't it then fairly obvious what you need to do? Commented Nov 8, 2017 at 5:39

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

7 Comments

Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("a", "value1"); dict.Add("abc", "value2"); dict.Add("5b", "value3"); dict.Add("hh", "value4"); dict.Add("nn", "value5"); dict.Add("iu", "value6");
@John: Well, have you checked my code, working example?
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)";
thanks lucky it works i just want to match arrary value with dictionary and make a new string please check updated question
Probably that would be a different question. anyway what's wrong with that
|
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.

Comments

-3

You should try this code :

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

3 Comments

It doesn't do what the OP wants. And why have you got the string[] a = splitVal;;?
string[] a is just for testing purpose. now it's correct.
Your answer still doesn't do what the OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.