替换字符串的特定位置

时间:2013-11-15 18:51:12

标签: java string str-replace

我有一个固定长度的字符串记录,我想用不同的字符串值替换特定位置的字符串。

String record ="ABCU0MARK                          111111118 CFTT        130913   101100023424";
String extractAccountaccountNumber = record.substring(79, 87);
String newAccountNumber = "some value"
record = record.replaceFirst(extractAccountaccountNumber,newAccountNumber);

如果存在重复值,则此功能可能无效。请建议

1 个答案:

答案 0 :(得分:1)

您只需将其分配给新的String变量或自身:

string = string.replace("to", "xyz");

String newString = string.replace("to", "xyz");


public class Run1 {

public static final int its = 4;

public static void main(String[] args) {

    String record ="ABCU0MARK                          111111118 CFTT        130913          101100023424";

    String extractAccountaccountNumber = record.substring(46, 55);
    System.out.println("extractAccountaccountNumber:"+extractAccountaccountNumber);
    String newAccountNumber = "some value";
    String result=record.replaceFirst(extractAccountaccountNumber,newAccountNumber);
    System.out.println("result:"+result);

}
}

这是结果:

extractAccountaccountNumber:FTT
结果:ABCU0MARK 111111118 Csome值130913 101100023424