我的java代码中有非常奇怪的错误,无法弄清楚出了什么问题.
假设我有这段代码:
private void test()
{
String test1 = replace("1.25");
String test2 = replace("1.5");
String test3 = replace("1.75");
}
private String replace(String s)
{
s = s.replaceAll(".25", "¼");
s = s.replaceAll(".5", "½");
s = s.replaceAll(".75", "¾");
return s;
}
Run Code Online (Sandbox Code Playgroud)
然后结果将是:
test1 ="¼"
test2 ="½"
test3 ="½"???????????
有人可以解释为什么test3变成"½"?
我最近偶然发现了这个问题,虽然我在 iOS 上运行良好,但无法在 Android 上运行,即使我使用相同的代码库并并行运行它们。
请注意,(number + '') 始终返回一个字符串,并且此代码在 iOS 上运行。
尝试使用全局标志“g”的正则表达式:
string.replace(/searchString/g, replaceString)
像一些建议的 StackOverFlow 答案也不起作用。
大家能猜出原因并提供解决方案吗?
嗨,我想知道String类的"replaceAll"函数的时间复杂度是什么,但我找不到任何相关信息.(http://docs.oracle.com/javase/6/docs/api/ java/lang/String.html)
Java在Javadoc中包含复杂性会不会更好?我相信有人知道这是非常重要的事情.
我正在使用java中的一些代码,它具有类似的语句
String tempAttribute = ((String) attributes.get(i)).replaceAll("\\p{Z}","")
Run Code Online (Sandbox Code Playgroud)
我不习惯正则表达式,所以它的含义是什么?(如果你能提供一个网站来学习那些很棒的正则表达式的基础知识)我已经看到了像
ept as y它变成了eptasy,但这似乎不对.我相信写这篇文章的人想要修剪前导空格和尾随空格.
我有一个这样的字符串:John \n Barber现在我想用实际的新行字符替换\n,这样它就会变成
John
理发师
这是我的代码
replaceAll("\\n", "\n");
Run Code Online (Sandbox Code Playgroud)
但它不起作用,并给我相同的字符串 John \n Barber
try (Stream<String> lines = Files.lines(targetFile)) {
List<String> replacedContent = lines.map(line ->
StringUtils.replaceEach(line,keys, values))
.parallel()
.collect(Collectors.toList());
Files.write(targetFile, replacedContent);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试替换文件的每一行中的多个文本模式.但我观察到"\ r \n"(字节等效10和13)正被替换为"\ r"(仅为10)并且我的比较测试失败了.
我想保留输入文件中的换行符,并且不希望java触及它们.任何人都可以建议是否有办法这样做而不必使用单独的默认替换"\ r \n".
我想要替换如下的java字符串值.下面的代码不起作用.
cleanInst.replaceAll("[<i>]", "");
cleanInst.replaceAll("[</i>]", "");
cleanInst.replaceAll("[//]", "/");
cleanInst.replaceAll("[\bPhysics Dept.\b]", "Physics Department");
cleanInst.replaceAll("[\b/n\b]", ";");
cleanInst.replaceAll("[\bDEPT\b]", "The Department");
cleanInst.replaceAll("[\bDEPT.\b]", "The Department");
cleanInst.replaceAll("[\bThe Dept.\b]", "The Department");
cleanInst.replaceAll("[\bthe dept.\b]", "The Department");
cleanInst.replaceAll("[\bThe Dept\b]", "The Department");
cleanInst.replaceAll("[\bthe dept\b]", "The Department");
cleanInst.replaceAll("[\bDept.\b]", "The Department");
cleanInst.replaceAll("[\bdept.\b]", "The Department");
cleanInst.replaceAll("[\bdept\b]", "The Department");
Run Code Online (Sandbox Code Playgroud)
实现上述替换的最简单方法是什么?
我想用"字符串替换^.
String str = "hello \"there";
System.out.println(str);
String str1 = str.replaceAll("\"", "^");
System.out.println(str1);
String str2= str1.replaceAll("^", "\"");
System.out.println(str2);
Run Code Online (Sandbox Code Playgroud)
输出是:
hello "there
hello ^there
"hello ^there
Run Code Online (Sandbox Code Playgroud)
为什么我"在字符串的开头和字符串^之间得到额外的
我期待:
hello "there
Run Code Online (Sandbox Code Playgroud) String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i++;", "");
Run Code Online (Sandbox Code Playgroud)
//期望的输出:: newCode = "helloworld";
但这并不是用空白取代i ++.
ECMAScript 2021 添加了一个新的 String 函数replaceAll。很久以前,在一个不远的星系中,人们使用split+join或正则表达式来替换所有出现的字符串。
我创建了以下示例来比较新方法和旧方法。虽然我可以在第一种情况下看到一些差异,例如我不能使用带有+ 的替换模式,或者我需要使用 来转义特殊字符,但在第二种情况下我看不出任何区别。splitjoinRegExp(str,"g")
新方法和旧方法有什么区别(行为差异、性能、浏览器兼容性...)?
const source = "abcdefabcdef";
const str1 = "abc", str2 = "xyz";
const reg1 = /abc/g, reg2 = "xyz";
//Case 1 : When we want to replace a string by another
console.log(source.split(str1).join(str2));
console.log(source.replace(new RegExp(str1,"g"),str2));
//versus
console.log(source.replaceAll(str1,str2));
//Case 2 : When we want to use a regular expression
console.log(source.replace(reg1,reg2));
//versus
console.log(source.replaceAll(reg1,reg2));
//Result = "xyzdefxyzdef"Run Code Online (Sandbox Code Playgroud)
replaceall ×10
java ×8
string ×7
regex ×5
javascript ×2
android ×1
java-8 ×1
java-stream ×1
javadoc ×1
newline ×1
react-native ×1
str-replace ×1