替换第n个字符串

问题描述:

我是新来的。我想问一个问题,因为我没有在搜索中找到我想要的东西。替换第n个字符串

这是通过问题和输出的问题。这是流程:

public class change{ 
    public static void main(String args[]){ 
     Random rand = new Random(); 
     int number = 9999 + rand.nextInt(190000); 
     int replace = 1 + rand.nextInt(5); 
     String numcon = Integer.toString(number); 
     String display = ????numcon????; 

我想要的是将numcon的某个或第n个位置的字符替换为“_”。像这样: 假设numcon已经随机化为“1234567”,替换在1到6之间随机化。这应该是System.out.print(display)的外观。

replace/display 
1  /"_23456" 
2  /"1_3456" 
3  /"12_456" 
4  /"123_56" 
5  /"1234_6" 
6  /"12345_" 

public class change{ 
    public static void main(String args[]){ 
    Random rand = new Random(); 
    int number = 9999 + rand.nextInt(190000); 
    int replace = 1 + rand.nextInt(5); 
    byte[] numcon = Integer.toString(number).getBytes(); 
    numcon[replace] = '_'; 
    String display = new String(numcon); 
+0

谢谢,它的工作原理虽然。 – 2014-08-31 15:33:07