在一个字符串跟踪支架 [英] Tracking brackets in a string

查看:198
本文介绍了在一个字符串跟踪支架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串,并要跟踪 ROUND( 的结束括号在我的字符串。

I am having the following string and want to track the closing bracket of ROUND( ) in my string.

<$c$c>"=ROUND(IF(AND($BY18=2);CA18*CB18/$M$11;IF($BY18=3;CA18*CB18/$M$10;IF($BY18=4;ROUND(CA18*CB18;$M$10)/$M$9;CA18*CB18)))/$M$12;$M$11)";

public class RoundParser {

    public static String parseRound(String text) {

        text = text.toUpperCase();

        String result;

        char[] ch = text.toCharArray();
        int count = -1;
        String temp = "";
        for (int i = 0; i < ch.length; i++) {
            temp = temp + ch[i];

            System.out.println(count);

            if ("ROUND(".equals(temp)) {
                count++;
            }
            if ("(".equals(temp)) {
                count++;
            }
            if (")".equals(temp) && count > 0) {
                count--;
            }
            if (")".equals(temp) && count == 0) {
                ch[i] = '#';
            }
            if (!"ROUND(".startsWith(temp) || temp.length() > 5) {
                temp = "";
            }
        }

        text = String.valueOf(ch);

        result = text;
        return result;
    }

    public static void main(String[] args) {
        String text = "=ROUND(IF(AND($BY18=2);CA18*CB18/$M$11;IF($BY18=3;CA18*CB18/$M$10;IF($BY18=4;ROUND(CA18*CB18;$M$10)/$M$9;CA18*CB18)))/$M$12;$M$11)";

        System.out.println(parseRound(text));
    }

}

然而,使用我的解析器,此刻我得到:

However, using my parser at the moment I am getting:

<$c$c>=ROUND(IF(AND($BY18=2);CA18*CB18/$M$11;IF($BY18=3;CA18*CB18/$M$10;IF($BY18=4;ROUND(CA18*CB18;$M$10)/$M$9;CA18*CB18))#/$M$12;$M$11#

我想要得到的输出是:

<$c$c>=ROUND(IF(AND($BY18=2);CA18*CB18/$M$11;IF($BY18=3;CA18*CB18/$M$10;IF($BY18=4;ROUND(CA18*CB18;$M$10#/$M$9;CA18*CB18)))/$M$12;$M$11#

正如你所看到的不正确的)所取代,如; $ M $ 11); ; $ M $ 10)。我真的AP $ P $,如果您有任何想法如何repalce这两种情况pciate。

As you can see the not the right ) are replaced, as ;$M$11)"; and ;$M$10). I really appreciate if you have any idea how to repalce these two cases.

推荐答案

有2个办法,这个

1)如果开闭括号中的数字总是将是平等的,那么你可以通过使用一个for循环轨道的最后一个右括号。

1) if the number of opening and closing brackets are always going to be equal, then you can just track the last closing bracket by using a for loop.

2),如果你不能确定开幕式和闭幕式括号是平等的,那么你可以这样下 - >

2) if you are not sure about opening and closing brackets to be equal, then you can so the following-->

public class RoundParser {

    public static String parseRound(String text) {

        text = text.toUpperCase();

        String result;

        char[] ch = text.toCharArray();

     int count=0,pos=0;
        int c[10];
        for(int i=0;i<ch.length;i++){

        if(ch[i].equals("(")){
        count++;  
         if(ch[i-1].equals("D")){
              c[pos]=count;   //will store the count value at every opening round
                  pos++;
              }
        }

        if(ch[i].equals(")")){
        count--;
         for(int j=0;j<10;j++){
             if(c[j]==count)   //if the closing of round had been encountered
                   ch[i]="#";   
         }
       }

        }


        text = String.valueOf(ch);

        result = text;
        return result;
    }

    public static void main(String[] args) {
        String text = "=ROUND(IF(AND($BY18=2);CA18*CB18/$M$11;IF($BY18=3;CA18*CB18/$M$10;IF($BY18=4;ROUND(CA18*CB18;$M$10)/$M$9;CA18*CB18)))/$M$12;$M$11)";

        System.out.println(parseRound(text));
    }

}

你去那里。

我认为这应该工作。

希望这会有所帮助。

这篇关于在一个字符串跟踪支架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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