标签: replaceall

java中的字符串replaceAll("¾")

我的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变成"½"?

java regex string replaceall

19
推荐指数
3
解决办法
744
查看次数

为什么 string.replaceAll() 不是 Android React Native 上的函数?

我最近偶然发现了这个问题,虽然我在 iOS 上运行良好,但无法在 Android 上运行,即使我使用相同的代码库并并行运行它们。

React Native 0.64.4 上的错误日志

请注意,(number + '') 始终返回一个字符串,并且此代码在 iOS 上运行。

尝试使用全局标志“g”的正则表达式:

string.replace(/searchString/g, replaceString)

像一些建议的 StackOverFlow 答案也不起作用。

大家能猜出原因并提供解决方案吗?

javascript android replaceall react-native

18
推荐指数
1
解决办法
8699
查看次数

为什么Java不包含javadoc中每个函数的时间/空间复杂度?

嗨,我想知道String类的"replaceAll"函数的时间复杂度是什么,但我找不到任何相关信息.(http://docs.oracle.com/javase/6/docs/api/ java/lang/String.html)

Java在Javadoc中包含复杂性会不会更好?我相信有人知道这是非常重要的事情.

java string complexity-theory javadoc replaceall

15
推荐指数
1
解决办法
3407
查看次数

正则表达式"\\ p {Z}"是什么意思?

我正在使用java中的一些代码,它具有类似的语句

String tempAttribute = ((String) attributes.get(i)).replaceAll("\\p{Z}","")
Run Code Online (Sandbox Code Playgroud)

我不习惯正则表达式,所以它的含义是什么?(如果你能提供一个网站来学习那些很棒的正则表达式的基础知识)我已经看到了像

ept as y它变成了eptasy,但这似乎不对.我相信写这篇文章的人想要修剪前导空格和尾随空格.

java regex replaceall

15
推荐指数
3
解决办法
1万
查看次数

java replaceAll不适用于\n字符

我有一个这样的字符串:John \n Barber现在我想用实际的新行字符替换\n,这样它就会变成

John

理发师

这是我的代码

replaceAll("\\n", "\n");
Run Code Online (Sandbox Code Playgroud)

但它不起作用,并给我相同的字符串 John \n Barber

java regex string replaceall

11
推荐指数
1
解决办法
1万
查看次数

如何使用流读取文件时保留换行符 - java 8

      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 newline replaceall java-8 java-stream

9
推荐指数
1
解决办法
4683
查看次数

正则表达式使用Java String.replaceAll

我想要替换如下的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)

实现上述替换的最简单方法是什么?

java regex string replaceall

8
推荐指数
2
解决办法
7万
查看次数

在尝试替换字符串中的字符时获取"

我想用"字符串替换^.

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)

java regex string replaceall

8
推荐指数
2
解决办法
410
查看次数

字符串替换所有不替换i ++;

String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i++;", "");
Run Code Online (Sandbox Code Playgroud)

//期望的输出:: newCode = "helloworld";

但这并不是用空白取代i ++.

java string replaceall str-replace

8
推荐指数
1
解决办法
233
查看次数

JavaScript 字符串替换 vs replaceAll

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)

javascript string replaceall ecmascript-2021

8
推荐指数
1
解决办法
2304
查看次数