string.replaceAll(“\\n”,””) 不起作用,如果字符串是从控制台输入并包含换行符。

huangapple go评论79阅读模式
英文:

string.replaceAll("\\n","") not working if string is taken as input from console and contains newline character

问题

Case 1: 从扫描仪获取字符串输入并将 \n 替换为 --(无法正常工作)

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\\n", "--");
System.out.println(str);

输入: "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n"

Case 2: 如果我直接将字符串赋值为与上述相同的值,则相同的操作可以正常工作。

String str = "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n";
       
str = str.replaceAll("\\n", "--");

PS:我已经尝试使用 \n 和 line.separater。

英文:

Case 1: Taking string input from scanner and replacing \n with -- (Not working)

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\n", "--");
System.out.println(str);

input: "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n"

Case2: Same thing works if I directly assign string with same value as above.

String str = "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n";
       
str = str.replaceAll("\n", "--");

PS: I have already tried using \n, line.separater

答案1

得分: 1

String str = "Input\\nwith backslash and n";
str = str.replaceAll("\\\\n", "--");
System.out.println(str);

Output:

> Input--with backslash and n

We need to escape the backslash twice: To tell the regular expression that a literal backslash is intended we need to put two backslashes. And to tell the Java compiler that we intend literal backslashes in the string, each of those two needs to be entered as two backslashes. So we end up typing four of them.

Less confusing solution

We dont need to use any regular expression here, and doing that complicates the escaping business. So dont.

String str = "Input\\nwith backslash\\nand n\\n";
str = str.replace("\\n", "--");
System.out.println(str);

> Input--with backslash--and n--
英文:
	String str = "Input\\nwith backslash and n";
	str = str.replaceAll("\\\\n", "--");
	System.out.println(str);

Output:

> Input--with backslash and n

We need to escape the backslash twice: To tell the regular expression that a literal backslash is intended we need to put two backslashes. And to tell the Java compiler that we intend literal backslashes in the string, each of those two needs to be entered as two backslashes. So we end up typing four of them.

nextLine() reads one line, so the line cannot contain a newline character. So I have assumes that you were entering a backslash and an n as part of your input.

Less confusing solution

We don’t need to use any regular expression here, and doing that complicates the escaping business. So don’t.

	String str = "Input\\nwith backslash\\nand n\\n";
	str = str.replace("\\n", "--");
	System.out.println(str);

> Input--with backslash--and n--

The replace method replaces all occurrences of the literal string given (in spite of not having All in the method name). So now we only need one escape, the one for the Java compiler.

答案2

得分: 1

单个反斜杠“\”在正则表达式中会引发错误,因为它是一个转义字符。如果你使用双反斜杠“\”,它会引发“java.util.regex.PatternSyntaxException: Unexpected internal error near index”异常。

在正则表达式中,双反斜杠被视为单个反斜杠“\”。因此,为了匹配字符串中的单个反斜杠,需要添加四个反斜杠“\\”。

请尝试将 \n 替换为 \\n:

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\\\\n", "--");
System.out.println(str);
英文:

In regular expression if you use single backward slash “\” throws error as it is a escape character. If you use double backward slash “\”, it throws “java.util.regex.PatternSyntaxException: Unexpected internal error near index” exception.

The double backward slash is treated as a single backward slash “\” in regular expression. So four backward slash “\\” should be added to match a single backward slash in a String.

Please try replacing \n with \\n:

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\\\\n", "--");
System.out.println(str);

huangapple
  • 本文由 发表于 2020年8月13日 12:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63388075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定