为什么此Java String.replaceAll()code不工作? [英] Why does this Java String.replaceAll() code not work?

查看:122
本文介绍了为什么此Java String.replaceAll()code不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用string.replaceAll()在Java中,没有麻烦过,但我难倒在这一个。我认为这将只是简单地工作,因为没有/或$字符。这里是我想要做的:

I have used string.replaceAll() in Java before with no trouble, but I am stumped on this one. I thought it would simply just work since there are no "/" or "$" characters. Here is what I am trying to do:

String testString = "__constant float* windowArray";
String result = testString.replaceAll("__constant float* windowArray", "__global float* windowArray");

变量的结果最终看起来一样的TestString。我不明白为什么没有任何改变,请大家帮忙。

The variable result ends up looking the same as testString. I don't understand why there is no change, please help.

推荐答案

传递的replaceAll的第一个参数仍被视为一个普通的前pression。在 * 字符是一个特殊字符的含义,大致的$ P $字符串(在这里pvious件事: T ),可以是有0个或更多次。你想要做的就是逃避 * 为常规的前pression。你的第一个参数应该看起来更像是:

The first argument passed to replaceAll is still treated as a regular expression. The * character is a special character meaning, roughly, the previous thing in the string (here: t), can be there 0 or more times. What you want to do is escape the * for the regular expression. Your first argument should look more like:

"__constant float\\* windowArray"

第二个参数是,你的目的,还只是一个普通的字符串,至少,让你不必逃避 * 那里。

String result = testString.replaceAll("__constant float\\* windowArray", "__global float* windowArray");

这篇关于为什么此Java String.replaceAll()code不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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