如何在任何操作员签名之前解析字符串? [英] How Can I Parse A String Before Any Operator Sign ?

查看:64
本文介绍了如何在任何操作员签名之前解析字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的字符串



EndMarket = 100



我想只获得EndMarket该字符串中的部分。



有时它可以是EndMarket< = 100或BeginMarket> = 50或BeginMarket< 20等



每次如果他找到任何类似<,=,>的运算符然后我得到操作员之前的部分。



对于我们的例子,我想使用C#代码获得EndMarket或BeginMarket部分。



任何人都可以提供此问题的代码吗?

解决方案

参见 http://msdn.microsoft.com/en-us/library/b873y76a(v = vs.110)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。


您可以使用 String.Split 方法或正则表达式



如果您打算使用拆分这样的事情可能适合您;

< pre lang =cs> 使用系统;
使用 System.Linq;
使用 System.Collections.Generic;

命名空间示例{

// < span class =code-comment>注意,没有考虑错误处理!
public static class MyExtensions {
private static readonly IEnumerable< string>分隔符= 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)。 ();
}
返回 null ;
}
}

class 计划{
静态 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());
}
}
}
}



希望这会有所帮助,

Fredrik


让我们认为你有3个运算符<,>和=(可根据需要增加)



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

if (sp.Trim()!=
{
// 用sp做一些事情(返回你的第一部分,即EndMarket或你在textValue中使用的任何东西
}
}


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?

解决方案

See http://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx[^].


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;

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


Lets think you have 3 operators <,> and = (can increase as you need)

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 
         }            
 }


这篇关于如何在任何操作员签名之前解析字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆