字符串中的关系运算符 [英] Relational Operator from String

查看:150
本文介绍了字符串中的关系运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是你的一个。我想从一个

字符串对象创建一个关系运算符,即我想以某种方式说:

string opString ="> =" ;;

int i1 =" 20";

int i2 =" 10";


if(i1 somemethodhere(opString) i2){

//在这里做真的

}

else {

//在这里做假

}


不要问为什么:)

不要一个笨重的切换声明。

想法?

Here''s one for ya. I want to create a relational operator from a
string object, i.e. I want to somehow be able to say:
string opString = ">=";
int i1 = "20";
int i2 = "10";

if (i1 somemethodhere(opString) i2) {
//do the true here
}
else {
//do the false here
}

Don''t ask why :)
Don''t want a clunky switch statement.
Thoughts?

推荐答案

6月25日下午2:11,ujjc ... @ gmail.com写道:
On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:

if(i1 somemethodhere(opString)i2){

//在这里做真的}
if (i1 somemethodhere(opString) i2) {
//do the true here}



如果您打算进行方法调用,更容易阅读

方法可能会使扩展方法行为相似。


------------

静态bool GreaterThan(此字符串c)

{

//您的自定义比较代码

}

------------


来自致电代码:

------------

if(i1.GreaterThan(i2)){

//

}其他{; }

------------


但是,如果你的比较要求就像你的
$ b一样简单例如,你可以更轻松地逃脱。

If you''re going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.

------------
static bool GreaterThan(this string c)
{
//your custom comparison code
}
------------

from calling code:
------------
if (i1.GreaterThan(i2)) {
//
} else { ; }
------------

However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.


6月25日,2:05 * pm,Mick Wilson< mick.wil ... @ gmail .comwrote:
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gmail.comwrote:

6月25日下午2:11,ujjc ... @ gmail.com写道:
On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:

if(i1 somemethodhere(opString)i2){

//在这里做真的}
if (i1 somemethodhere(opString) i2) {
//do the true here}



如果你是无论如何要进行方法调用,更容易阅读

方法可能会使扩展方法行为相似。


------- -----

静态布尔BigThan(此字符串c)

{

* * //您的自定义比较代码}


------------


致电代码:

----- -------

if(i1.GreaterThan(i2)){

* * //} else {; }


------------


但是,如果你的比较要求和你的比较一样简单/>
例如,你可以更轻松地逃脱。


If you''re going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.

------------
static bool GreaterThan(this string c)
{
* * //your custom comparison code}

------------

from calling code:
------------
if (i1.GreaterThan(i2)) {
* * //} else { ; }

------------

However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.



问题是我不知道运营商会是什么。它可以

是=或>或> =我仍然需要一个switch语句

来根据opString的字符串输入知道要运行哪个方法。

The problem is that I don''t know what the operator will be. It could
be "=" or ">" or ">=" I would still have to have a switch statement
to know which method to run based on the string input of opString.


6月25日, 2:13 * pm,ujjc ... @ gmail.com写道:
On Jun 25, 2:13*pm, ujjc...@gmail.com wrote:

6月25日,2:05 * pm,Mick Wilson< mick.wil。 .. @ gmail.comwrote:

On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gmail.comwrote:



6月25日下午2:11,ujjc ... @ gmail.com写道:
On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:


if(i1 somemethodhere(opString)i2){

//这里是真的}
if (i1 somemethodhere(opString) i2) {
//do the true here}


如果你打算进行方法调用,更容易阅读

方法可能是使行为类似的扩展方法。
If you''re going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.


------------

static bool GreaterThan(this string c)

{

* * //您的自定义比较代码}
------------
static bool GreaterThan(this string c)
{
* * //your custom comparison code}


------ ------
------------


来自调用代码:

------------

if(i1.GreaterThan(i2)){

* * //} else {; }
from calling code:
------------
if (i1.GreaterThan(i2)) {
* * //} else { ; }


------------
------------


但是,如果您的比较要求与您的

示例一样简单,那么您可以更轻松地完成某些操作。
However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.



问题在于我不知道运营商会是什么。 *它可以

是=或>或> = *我仍然需要一个switch语句

来根据opString的字符串输入知道要运行哪个方法.-隐藏引用文本 -


- 显示引用的文字 -


The problem is that I don''t know what the operator will be. *It could
be "=" or ">" or ">=" *I would still have to have a switch statement
to know which method to run based on the string input of opString.- Hide quoted text -

- Show quoted text -



也,我不想要自定义比较代码,我真的想要

与opString值,即如果我说:

if(i1 opString i2)...

我只想要与
$一起使用比较的bool结果字符串中的b $ b运算符。 (你可以将运营商存储为

财产吗?试过,无法弄明白。)

杰夫

also, I don''t want "custom comparison code", I literally want to
compare with the opString value i.e. if I say:
if (i1 opString i2)...
I just want a bool result of the comparison when used with the
operator that is in the string. (Can you store an operator as a
property? Tried, couldn''t figure out).
Jeff


这篇关于字符串中的关系运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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