有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要从字符串中转义“| |”条件

我遇到了需要删除“| |”并从字符串中插入反斜杠的情况

String s = "stack||overflow";

我需要替换,结果字符串应该是“stack\overflow”

我尝试使用replaceAll函数,但没有成功

感谢您的帮助

谢谢, 斯雷坎特


共 (1) 个答案

  1. # 1 楼答案

    这已在Oracle tutorial部分中将字符和子字符串替换为字符串中描述:

    String replace(CharSequence target, CharSequence replacement) 
    

    Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

    例如:

    String s = "stack||overflow.";
    System.out.println(s.replace("||","\\"));