简体   繁体   English

给定一个字符串 (100*((2+12)/5))/14,如何在每个运算符和数字之间添加空格?

[英]Given an string (100*((2+12)/5))/14, how would you add spaces between each operator and number?

For example I want to go from this string例如我想从这个字符串 go

(100*((2+12)/5))/14

to this对此

( 100 * ( ( 2 + 12 ) / 5 ) ) / 14

You can do it as你可以这样做

"(100*((2+12)/5))/14".replaceAll("\\D", "$0 ").replaceAll("\\d+", "$0 ").trim();

Explanation:解释:

  1. \D specifies a non-digit character which will be replaced with the character plus space. \D指定将替换为字符加空格的非数字字符。
  2. After the above replacement is done the second replacement will be applied on the returned string.完成上述替换后,将对返回的字符串应用第二次替换。 In that replacement, \d+ specifies an integer number which will be replaced with the number plus space.在该替换中, \d+指定了一个 integer 数字,该数字将替换为数字加空格。
  3. Finally, the trailing and leading space will be removed by trim() .最后,尾随和前导空格将被trim()删除。

Demo演示

public class Main {
    public static void main(String[] args) {
        String expression = "(100*((2+12)/5))/14";
        expression = expression.replaceAll("\\D", "$0 ").replaceAll("\\d+", "$0 ").trim();

        System.out.println(expression);
    }
}

Output: Output:

( 100 * ( ( 2 + 12 ) / 5 ) ) / 14

You can iterate over all characters, and then check if:您可以遍历所有字符,然后检查是否:

  • if it's the last character don't do anything如果是最后一个字符,什么也不做
  • if the current index character is not a digit then add a space after it如果当前索引字符不是数字,则在其后添加一个空格
  • if the current index character is a digit but the next character isn't then add a space after it如果当前索引字符是数字但下一个字符不是,则在其后添加一个空格
public static void main(String[] args)
    {
        String str = "(100*((2+12)/5))/14";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            sb.append(c);
            if (i < str.length() - 1)
            {
                if ((Character.isDigit(c) && !Character.isDigit(str.charAt(i + 1))) || !Character.isDigit(c))
                {
                    sb.append(" ");
                }
            }
        }

        System.out.println(sb.toString());
    }

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

相关问题 如何在Java中的字符串中添加数字和单词之间的空格? - How to add spaces between number and word in a string in Java? 如何在 Java 中的字符串之间添加空格? - How to Add spaces between a string in Java? 在Java中的字符串的字母之间添加x个空格? - Add x number of spaces between the letters of a string in Java? 如何为给定字符串中的每个单词添加前缀 - How to add prefix to each word in a given string 如何生成具有可变数量的空格的字符串,或者如何在字符串后不添加空格 - how to generate string with varying number of spaces OR how to add no of spaces after a string Calcu:如何在每个数字之前添加运算符,如真实的calc - Calcu: How to add the operator before each number like a real calc 在生成1到100之间的1到300个数字并将每个数字放在字符串中时遇到问题 - Having issues with generating 1 to 300 numbers between 1 to 100 and placing each number in a String 如何用单词和整数在字符串中的数字之间添加空格? - How to add spaces between numbers in string with word and integer? 如何为给定字符串创建唯一的序列号? - How can you create a unique serial number for a given string? 如何在Java中以给定的String格式添加定界符? - How do you add a delimiter in a given String format in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM