Java “替换”(“n”和“replace”)不起作用
这段代码似乎工作得很好Java “替换”(“n”和“replace”)不起作用,java,string,replace,Java,String,Replace,这段代码似乎工作得很好 String in = "dr. goldberg offers everything i look for in a general practitioner. he's nice and easy to talk to without being patronizing; he's always on time in seeing his patients; he's affiliated with a top-notch hospital (nyu) which
String in = "dr. goldberg offers everything i look for in a general practitioner. he's nice and easy to talk to without being patronizing; he's always on time in seeing his patients; he's affiliated with a top-notch hospital (nyu) which my parents have explained to me is very important in case something happens and you need surgery; and you can get referrals to see specialists without having to see him first. really, what more do you need? i'm sitting here trying to think of any complaint\n\ns i have about him, but i'm really drawing\n a blank.";
但是这个
in = in.replaceAll("\n", "");
似乎不起作用。这不是删除新行常量,请有人解释一下原因
(我正在使用substringBetween从文件中获取部分文本)查看以下代码:
for(String temp: review){
String reviews = (StringUtils.substringBetween(temp,"\"text\": \"", "\", \"type\"")).replaceAll("\n", "");
if(reviews.equals(in)){
System.out.println("yes");
System.exit(1);
}
}
或者,您可以使用系统属性删除新行字符,如下所示:
String sample="This string has new line \\n " + "New line is here if \\n \n is working";
System.out.println("Original String: " + sample); //replacing \n or newline character so that all text come in one line
System.out.println("Escaped String: " + sample.replaceAll("\n", ""));
StringUtils.substringBetween(temp,“\”text\”:\”,“\”,“\”type\”)
evalutes在运行时执行什么操作?@JigarJoshi-它的计算结果与输入结果相同,但使用新行常量,替换似乎不起作用。另外,我正在使用input.equals(reviews)检查这两个字符串是否相等,它的计算结果应该为true,但它不是。还要注意,您使用的是replace()
而不是replaceAll()
,因此它不会替换除first@RuslanOstafiychuk-我试过了,但还是不起作用。
String text = readFileAsString("words.txt");
text = text.replace(System.getProperty("line.separator"), "");