leeray3017 2010-01-15 16:41
浏览 838
已采纳

JAVA中的String某个字符替换

在java语言中,需要将String字符串中的所有"'"转换成 "\'",也就是单引号前增加一个"\"
要求在jdk1.4.2和jdk 1.5下都能正常运行

[b]问题补充:[/b]
问题来由是这样的,我们开发使用webshpere,我是后台开发人员,测试机was jdk为1.4.2,之前我这样写的
[code="java"]
String columnValue = this.getColumnValue(columnName).toString().replaceAll("\'", "\'");
[/code]
结果在was 的jdk 1.4.2下没问题,发到项目上,项目使用的was 的jdk 1.5
结果没有该方法
为什么要有那么多的"\"呢,我不大清楚

  • 写回答

9条回答 默认 最新

  • mginobili 2010-01-15 17:06
    关注

    因为replace方法内部是使用正则表达式的

    lz可以看看下面这段代码
    [code="java"]
    public Matcher appendReplacement(StringBuffer sb, String replacement) {

        // If no match, return error
        if (first < 0)
            throw new IllegalStateException("No match available");
    
        // Process substitution string to replace group references with groups
        int cursor = 0;
        String s = replacement;
        StringBuffer result = new StringBuffer();
    
        while (cursor < replacement.length()) {
            char nextChar = replacement.charAt(cursor);
            if (nextChar == '\\') {
                cursor++;
                nextChar = replacement.charAt(cursor);
                result.append(nextChar);
                cursor++;
            } else if (nextChar == '$') {
                // Skip past $
                cursor++;
    
                // The first number is always a group
                int refNum = (int)replacement.charAt(cursor) - '0';
                if ((refNum < 0)||(refNum > 9))
                    throw new IllegalArgumentException(
                        "Illegal group reference");
                cursor++;
    
                // Capture the largest legal group string
                boolean done = false;
                while (!done) {
                    if (cursor >= replacement.length()) {
                        break;
                    }
                    int nextDigit = replacement.charAt(cursor) - '0';
                    if ((nextDigit < 0)||(nextDigit > 9)) { // not a number
                        break;
                    }
                    int newRefNum = (refNum * 10) + nextDigit;
                    if (groupCount() < newRefNum) {
                        done = true;
                    } else {
                        refNum = newRefNum;
                        cursor++;
                    }
                }
    
                // Append group
                if (group(refNum) != null)
                    result.append(group(refNum));
            } else {
                result.append(nextChar);
                cursor++;
            }
        }
    
        // Append the intervening text
        sb.append(getSubSequence(lastAppendPosition, first));
        // Append the match substitution
        sb.append(result.toString());
    
        lastAppendPosition = last;
    return this;
    }
    

    [/code]
    注意 if (nextChar == '\')
    你该知道了吧~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办