Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have string like that

EndMarket=100

I want to get only EndMarket portion from this string.

Sometimes it can be EndMarket<=100 or BeginMarket>=50 or BeginMarket<20 etc.

For every time if he found any operator like <,=, > etc. then i get the portion before the operator.

For our example i want to get EndMarket or BeginMarket portion using C# code.

Can anyone provide a code for this problem?
Posted
Updated 6-May-14 1:13am
v3

You could use the String.Split method or a regular expression.

If you were to go for using Split something like this might do the trick for you;
C#
using System;
using System.Linq;
using System.Collections.Generic;

namespace Sample {
    
   // Beware, no error handling have been considered!
   public static class MyExtensions {
        private static readonly IEnumerable<string> Delimiters = new[] { ">=", "<=", "!=", ">", "<", "=" };

        public static string GetVariable(this string expression) {
            if (Delimiters.Any(expression.Contains)) {
                var delimiter = Delimiters.Where(expression.Contains).First();
                return expression.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries).First();
            }
            return null;
        }
    }

    class Program {
        static void Main(string[] args) {
            var input = new[]
                {
                    "EndMarket=100",
                    "EndMarket<200",
                    "EndMarket>300",
                    "EndMarket!=300"
                };

            foreach (var s in input) {
                Console.WriteLine("{0}: {1}", s, s.GetVariable());
            }
        }
    }
}

Hope this helps,
Fredrik
 
Share this answer
 
Comments
sachi Dash 6-May-14 7:15am    
It is not always EndMarket it can be BeginMarket<0, MiddleMarket<=0 etc. For everytime i want to get the portion brfore operator like MiddleMarket,BeginMarket etc.
Fredrik Bornander 6-May-14 7:20am    
That is exactly what that code I showed you does!
Calling GetVariable will get you the string on the left, regardless of what that string is.
Lets think you have 3 operators <,> and = (can increase as you need)

C#
string textValue = EndMarket<=100;
string [] splitted_string = textValue.Split(new Char [] {'<', '>', '='});
foreach (string sp in splitted_string) {

       if (sp.Trim() != "")
         {
         //do something with sp (which returns you the first part i.e EndMarket or whatever you use in the textValue 
         }            
 }
 
Share this answer
 
v2
You can use substring().

string s="EndMarket=100";

string n=s.substring(0,8);
 
Share this answer
 
Comments
sachi Dash 6-May-14 7:15am    
It is not always EndMarket it can be BeginMarket<0, MiddleMarket<=0 etc. For everytime i want to get the portion brfore operator like MiddleMarket,BeginMarket etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900