如何用 Java 中的单词替换字符
How to replace a character with a word in Java
我有一个像这样的输入字符串,它接受中缀表达式:String str = "-(4-2)";
我的输出字符串returns一个后缀表达式形式的字符串值:4 2 - -
如何将 4 2 - -
末尾的 -
符号替换为 negate
,以便我的输出看起来像 4 2 - negate
?
我尝试使用 str.replace
但它不起作用,因为您只能将 char 替换为 char 或将 string 替换为 string。
我将中缀表达式转换为后缀表达式的代码:
private int precedence(Character character)
{
switch (character)
{
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
}
return 0;
}
@Override public T visitExp(ExpAnalyserParser.ExpContext ctx) {
String postfix = "";
Stack<Character> stack = new Stack<>();
for (int i = 0; i< ctx.getText().length(); i++) {
char c = ctx.getText().charAt(i);
if (Character.isDigit(c)) {
postfix += c;
}
else if (c == '(') {
stack.push(c);
}
else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
postfix += " " + (stack.pop());
}
if (!stack.isEmpty() && stack.peek() != '(')
System.out.println("Invalid Expression");
else
stack.pop();
}
else {
postfix += " ";
while (!stack.isEmpty() && precedence(c) <= precedence(stack.peek()))
postfix += (stack.pop()) + " " ;
stack.push(c);
}
}
while (!stack.isEmpty()){
postfix += " " + (stack.pop());
}
postfix = postfix.replace("%", "mod");
try(FileWriter out = new FileWriter("postfix.txt")){
out.write(postfix);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Infix Expression: " + ctx.getText());
return (T) postfix;
}
我们将不胜感激。
一种方法是使用 substring
删除最后一个字符,然后将您的单词连接到末尾:
str = str.substring(0, str.length() - 1) + "negate";
ReplaceAll,这听起来违反直觉,使用正则表达式,因此您可以在字符串末尾指定减号:
-> str.replaceAll ("-$", "negate");
| Expression value is: "4 2 - negate"
| assigned to temporary variable of type String
我有一个像这样的输入字符串,它接受中缀表达式:String str = "-(4-2)";
我的输出字符串returns一个后缀表达式形式的字符串值:4 2 - -
如何将 4 2 - -
末尾的 -
符号替换为 negate
,以便我的输出看起来像 4 2 - negate
?
我尝试使用 str.replace
但它不起作用,因为您只能将 char 替换为 char 或将 string 替换为 string。
我将中缀表达式转换为后缀表达式的代码:
private int precedence(Character character)
{
switch (character)
{
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
}
return 0;
}
@Override public T visitExp(ExpAnalyserParser.ExpContext ctx) {
String postfix = "";
Stack<Character> stack = new Stack<>();
for (int i = 0; i< ctx.getText().length(); i++) {
char c = ctx.getText().charAt(i);
if (Character.isDigit(c)) {
postfix += c;
}
else if (c == '(') {
stack.push(c);
}
else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
postfix += " " + (stack.pop());
}
if (!stack.isEmpty() && stack.peek() != '(')
System.out.println("Invalid Expression");
else
stack.pop();
}
else {
postfix += " ";
while (!stack.isEmpty() && precedence(c) <= precedence(stack.peek()))
postfix += (stack.pop()) + " " ;
stack.push(c);
}
}
while (!stack.isEmpty()){
postfix += " " + (stack.pop());
}
postfix = postfix.replace("%", "mod");
try(FileWriter out = new FileWriter("postfix.txt")){
out.write(postfix);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Infix Expression: " + ctx.getText());
return (T) postfix;
}
我们将不胜感激。
一种方法是使用 substring
删除最后一个字符,然后将您的单词连接到末尾:
str = str.substring(0, str.length() - 1) + "negate";
ReplaceAll,这听起来违反直觉,使用正则表达式,因此您可以在字符串末尾指定减号:
-> str.replaceAll ("-$", "negate");
| Expression value is: "4 2 - negate"
| assigned to temporary variable of type String