我正在尝试替换包含char"$"的子字符串.我很高兴听到它为什么不能这样工作,以及它如何工作.
谢谢,user_unknown
public class replaceall {
public static void main(String args[]) {
String s1= "$foo - bar - bla";
System.out.println("Original string:\n"+s1);
String s2 = s1.replaceAll("bar", "this works");
System.out.println("new String:\n"+s2);
String s3 = s2.replaceAll("$foo", "damn");
System.out.println("new String:\n"+s3);
}
}
Run Code Online (Sandbox Code Playgroud) 我想在这里找出这个问题的答案.
首先,
blah[abc] = blah[abc].replaceAll("(.*) (.*)", "$2, $1");
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下(.*),$ 2和$ 1是什么?
其次,当我在for语句中嵌套以便反转字符串的两个部分时,我遇到异常错误.我想知道是否有人知道为什么会这样.
谢谢
编辑:这是我收到的错误
线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:1在ChangeNames.main(ChangeNames.java:21)
我有一个Java字符串,可以在一个更大的字符串中多次包含" {"和" }"对:
"My name is {name}. I am a {animal}. I like to {activity} whenever it rains."
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写将剥离这些对的所有实例的代码:
"My name is . I am a . I like to whenever it rains."
Run Code Online (Sandbox Code Playgroud)
这些对可以出现在字符串内的任何位置,并且可能根本不会发生.显然a String#replaceAll(someRegex, "")在这里是有序的,但我似乎无法做到someRegex正确:
// My best attempt.
String stripped = toStrip.replaceAll("{*}", "");
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢.
我有字典输入.迭代字典以替换key文本中的字典.但replaceAll功能也取代了subString.
如何确保它匹配整个单词(整体而不是一个subString)
String text= "Synthesis of 1-(2,6-dimethylbenzyl)-1H-indole-6-carboxylic acid [69-3] The titled compound (883 mg) sdvfshd[69-3]3456 as a white solid was prepared"
dictionary= {[69-3]=1-(2,6-dimethylbenzyl)-1H-indole-6-carboxylic acid }
for(Map.Entry<String, String> entry : dictionary.entrySet()){
text=text.replaceAll("\\b"+Pattern.quote(entry.getKey())+"\\b", entry.getValue());
}
Run Code Online (Sandbox Code Playgroud) replaceAll("\\s+")和之间有什么区别replaceAll("\\\\s+")?通常我会使用\\s+但有时我会看到\\\\s+.
我想利用来自用户的输入作为String和替换换行符\n与,
我试过了 :
String test ="s1\ns2\ns3\ns4";
System.out.println(test.replaceAll("\n",","));
输出为s1,s2,s3,s4
但是,当我通过从UI获取输入来尝试相同的代码时,它无法正常工作.
当我调试它时,字符串测试(我硬编码)被视为,
S1
S2
S3
S4
但是来自UI的字符串是" s1\ns2\ns3\ns4".
请说明出了什么问题.
昨天我正在制作定制的信件和备忘录.我制作了映射关键字,如:[Dateofbirth],[Email],[Employee],[Salary]等,它们将在生成时替换.
示例:亲爱的[员工],您的当前薪水是[薪水].
预期产量:
输出:亲爱的约翰,你目前的薪水是12000.
我在replaceAll()这里使用的方法是一个代码.
String str = "dear [Employee], your Current Salary is [Salary].";
Map<String,String> vals = new HashMap<String,String>();
vals.put("[Employee]","John");
vals.put("[Salary]","12000");
for(String key:vals.keySet()){
str=str.replaceAll(key,vals.get(key));
}
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)
但是出来的是:
dJohn1200012000 [JohnJohnJohnJohnJohnJohnJohnJohn], JohnJohnu12000 Cu1200012000Johnnt 1200012000John1200012000John is [1200012000John1200012000John].
Run Code Online (Sandbox Code Playgroud)
我很困惑,谷歌搜索它并试图使其正确,之后我改变replaceAll()了replace()
EX: str=str.replace(key,vals.get(key));
Run Code Online (Sandbox Code Playgroud)
现在工作正常.问题是为什么替换所有正在做这种行为的核心概念是什么replaceAll(),何时使用replace()何时使用replaceAll().谢谢
我想用另一个值替换数据框列中的一个值,并且我必须为许多列(假设为 30/100 列)执行此操作
from pyspark.sql.functions import when, lit, col
df = sc.parallelize([(1, "foo", "val"), (2, "bar", "baz"), (3, "baz", "buz")]).toDF(["x", "y", "z"])
df.show()
# I can replace "baz" with Null separaely in column y and z
def replace(column, value):
return when(column != value, column).otherwise(lit(None))
df = df.withColumn("y", replace(col("y"), "baz"))\
.withColumn("z", replace(col("z"), "baz"))
df.show()
Run Code Online (Sandbox Code Playgroud)
我可以在 y 和 z 列中分别用 Null 替换“baz”。但我想对所有列都这样做 - 类似于下面的列表理解方式
[replace(df[col], "baz") for col in df.columns]
Run Code Online (Sandbox Code Playgroud) 我正在寻找在 Kotlin 中用相应的不同字符替换多个不同字符的可能性。
\n作为一个例子,我在 PHP 中寻找与此类似的函数:
\nstr_replace(["\xc4\x81", "\xc4\x93", "\xc4\xab", "\xc5\x8d", "\xc5\xab"], ["a","e","i","o","u"], word)\nRun Code Online (Sandbox Code Playgroud)\n现在在 Kotlin 中,我只需调用 5 次相同的函数(对于每个声音),如下所示:
\nvar newWord = word.replace("\xc4\x81", "a")\nnewWord = word.replace("\xc4\x93", "e")\nnewWord = word.replace("\xc4\xab", "i")\nnewWord = word.replace("\xc5\x8d", "o")\nnewWord = word.replace("\xc5\xab", "u")\nRun Code Online (Sandbox Code Playgroud)\n如果我必须使用一系列单词而不仅仅是一个单词来执行此操作,那么这当然可能不是最好的选择。有没有办法做到这一点?
\n是否有一个库或任何简单的方法来转换字符串,并确保它兼容作为正则表达式来寻找和替换另一个字符串.因此,如果字符串是"$ money",它将转换为"\ $ money".我尝试使用StringEscapeUtil.escape,但它不能用于$等字符.