如何在java中检查字符串中的字符是否包含在括号中?

ep6jt1vc  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(570)

我有一个要求,我必须从一个不包含在括号中的字符串中替换一个字符(比如@)的多次出现。可以保证输入字符串具有有效的平衡圆括号。整个字符串中可以有多对括号。
示例:string str=“你今天过得怎么样?”;
将“@”符号(括号外)替换为#后的预期输出:
你今天过得怎么样。
我可以取出括号内的字符串,进行替换并将括号文本放回。但我正在寻找一种更简单更干净的方法。

yiytaume

yiytaume1#

是的,这正是你需要的。。

/**
 *@author jore
 *@version 1.0 2/1/2021
 */
public class replacer
{
  public static void main(String... a)
  {
    /* All these yield correct outpUt */
    String input = "How @ are (you @ doing) today @.";
//    String input = How @ are @ ... @ ... (you @ doing (...@...)..@ ) gghhh @ today @ ( hhh @ uuu (.. @.. ) huu ( hyyy @ bhh @ ) hhh @ ) hhh @..@";

    int opened_brackets = 0; //To keep track of opened brackets preventing ..(@..(@..)..#).. and allow complex nesting
    char[] temp_array = input.toCharArray(); //To ease random access in input
    char c; // temp char for testing

    for (int i = 0; i < input.length(); i++)
    {
      c = temp_array[i];
      if (c == '(') opened_brackets++; //record opened brackets (reason as explained above
      else if (c == ')') opened_brackets--; //record closure of bracket too...
      if (opened_brackets == 0 && c == '@') temp_array[i] = '#'; // Now replacing is fine if opened brackets are == 0, ot means the character is not in brackets
    }

    String output = new String(temp_array); //re-create edited output
    System.out.println(output); //....u know what this does.....¿?
  }
}

输出1: How # are (you @ doing) today #. 输出2: # -- replacer How # are # ... # ... (you @ doing (...@...)..@ ) gghhh # today hhh @ uuu (.. @.. ) huu ( hyyy @ bhh @ ) hhh @ ) hhh #..# 我创造了一个 temp_array 为了允许快速随机访问..现在我跟踪打开的支架以防止替换 @ 在嵌套的括号里…除了等待 ) 替换..如果 0 支架打开。。意思是 @ 放错位置了(括号外,应该立即更换…希望能解决你的问题

gk7wooem

gk7wooem2#

一个简单的方法是保持一个“for循环”来读取字符串的所有字符,例如:

String str = "How @ are (you @ doing) today @.";
        char ab ='';
       for(int i=0;i<str.length();i++)
        {
         ab = str.charAt(i);
         if(ab!='(')
           // check if character is '@' and replace
         else{
           //print as it is; 
            while(ab != ')')
            {//print ab as it is & increment the value of i
            }
           }

         }
yrdbyhpb

yrdbyhpb3#

因为您没有提到嵌套是否可行以及字符串数据的可能大小。下面是一个可能的易于理解的代码。
如果堆栈为空=>我们在括号外=>replaceon else off

public static void main(String[] args) {
        String data = "How @ are (you @ do(ok@ok)ing) today @.";

        char candidate = '@';
        char replacement = '#';
        boolean replaceFlag = true;

        Stack<Character> stack = new Stack<>();

        StringBuilder newData = new StringBuilder();

        for(char c: data.toCharArray()) {
            if (c == '(') {
                stack.push(c);
                replaceFlag = false;
            } else if(c == ')') {
                stack.pop();
            }

            if (stack.empty()) {
                replaceFlag = true; // we are not inside parenthesis
            }

            if (c == candidate && replaceFlag) c = replacement;

            newData.append(c);
        }

        System.out.println("Old data: "+ data);
        System.out.println("New data: "+ newData);
    }

相关问题