简体   繁体   English

如何以有组织的方式将值范围与字符串进行比较?

[英]How to compare a range of values to a string in an organized fashion?

this is a simple question. 这是一个简单的问题。

I have a string which I have split into an array of characters. 我有一个字符串,已将其拆分为字符数组。 I need to iterate through each character and check if it is the same as a relevant one that indicates certain behaviour. 我需要遍历每个字符,并检查它是否与指示某些行为的相关字符相同。 How can I do this without having a bunch of if/switch statements. 如何在没有一堆if / switch语句的情况下执行此操作。 Other than create a new method which I implement that accepts multiple arguments and compares all of them, not sure what I can do here. 除了创建我要实现的新方法之外,该方法接受多个参数并比较所有参数,不确定在这里我能做什么。

Thanks. 谢谢。

edit: I am looking for a good implementation of a function that can do this or a java statement (ie if statement) that would make this process easier. 编辑:我正在寻找可以做到这一点的函数或Java语句(即if语句)的良好实现,这将使此过程更加容易。

edit2: mistagged as c#, but I regularly use both languages so I don't mind an explanation in either one. edit2:被误认为是c#,但是我经常使用两种语言,因此我不介意使用任何一种语言进行解释。

Some crappy code to illustrate a simple example of what I mean: 一些糟糕的代码来说明我的意思的一个简单示例:

`if (charsArray[i] == "-")
    s.push(charsArray[i]);
if (charsArray[i] == "+")
    s.push(charsArray[i]);
if (charsArray[i] == "[")
    s.push(charsArray[i]);
if (charsArray[i] == "]")
    s.push(charsArray[i]);`

Note: I would use a for loop to iterate through each char after it is an array of characters. 注意:我将使用for循环来迭代每个字符组成的字符。

EDIT: The change from C# to java occurred while I was creating this. 编辑:从C#到Java的更改是在我创建此代码时发生的。 As such, this may not work. 因此,这可能不起作用。

Simple question, semi-complicated answer. 简单的问题,半复杂的答案。

You could create a Dictionary <char, object> where object is some other Type that will work for you. 您可以创建一个Dictionary <char, object> ,其中object是其他一些适合您的Type After that, in your iterating through the string, check if the Dictionary contains the key and then... well, you can sort it out from there. 之后,在遍历字符串时,检查Dictionary包含密钥,然后...好吧,您可以从那里进行排序。 :) :)

A suggestion, you could use a delegate or one of it's child types ( Action , Predicate , or Func ). 建议中,您可以使用delegate或其子类型之一( ActionPredicateFunc )。 I can't be any more specific as you did not post any code... 由于您没有发布任何代码,因此我无法再具体说明...

If the behavior is not unique for each symbol, two pretty good solutions are a switch: 如果每个符号的行为都不都是唯一的,则有两个不错的解决方案:

switch(charsArray[i]) {
    case '+': case '-': case '[': case ']':
        s.push(charsArray[i]);
}

And String contains (this might be what the comment suggestion is referring to): 和String包含(这可能是注释建议所指的内容):

if("+-[]".contains(String.valueOf(charsArray[i]))) {
    s.push(charsArray[i]);
}

If the behavior is unique, you might use the strategy pattern with a HashMap: 如果行为是唯一的,则可以将策略模式与HashMap一起使用:

// where charMap is some HashMap<Character, SomeStrategy>
SomeStrategy strat = charMap.get(charArray[i]);
if(strat != null)
    strat.execute();

(Which I think is the Java way to do what @bubbinator is suggesting. I don't know C# but I guess Dictionary is a hash table.) (我认为这是执行@bubbinator建议的Java方法。我不知道C#,但我想Dictionary是一个哈希表。)

Remove all "uninteresting" characters from the string (by replacing them with a blank), then push all remaining chars. 从字符串中删除所有“无趣的”字符(通过将它们替换为空白), 然后压入所有剩余的字符。

It's just two lines: 只有两行:

for (char c : str.replaceAll("[^-+\\[\\]]", "").toCharArray())
    s.push(c);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM