Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to replace a character at a specific index in a string with Java

4 Answers

0 votes
public class Program {
    public static String replaceCharacter(String str, int index, char ch) { 
        if (index < 0 || index > str.length()) {
            return str;
        }
          
        String tmp = "";
        int size = str.length();
        for (int i = 0; i < size; i++) {
            if (i == index) {
                tmp += ch;
            }
            else {
                tmp += str.charAt(i); 
            }
        }
              
        return tmp;
    }
    public static void main(String args[]) {
        String str = "java c c++";
         
        str = replaceCharacter(str, 1, 'Z');
 
        System.out.println(str);
    }
}
 
 
 
 
/*
run:
 
jZva c c++
 
*/

 



answered Nov 12, 2019 by avibootz
edited Mar 1, 2024 by avibootz
0 votes
public class Program {
    public static String replaceCharacter(String str, int index, char ch) { 
        StringBuilder sb = new StringBuilder(str);
         
        sb.setCharAt(1, 'Z');
         
        return sb.toString();
    }
    
    public static void main(String args[]) {
        String str = "java c c++";
 
        str = replaceCharacter(str, 1, 'Z');
 
        System.out.println(str);
    }
}
 
 
 
 
/*
run:
 
jZva c c++
 
*/

 



answered Nov 12, 2019 by avibootz
edited Mar 1, 2024 by avibootz
0 votes
public class Program {
    public static String replaceCharacter(String str, int index, char ch) { 
        return str.substring(0, index) + ch + str.substring(index + 1);
    }
    
    public static void main(String args[]) {
        String str = "java 17";
 
        str = replaceCharacter(str, 1, 'Z');
 
        System.out.println(str);
    }
}


  
/*
run:
  
jZva 17
  
*/

 



answered Mar 27, 2023 by avibootz
edited Mar 1, 2024 by avibootz
0 votes
public class Program {
    static String replaceCharacter(String str, int index, char ch) {
		return (index > 0 ? str.substring(0, index) : "")
			+ Character.toString(ch) + (index < str.length() - 1 ? str.substring(index + 1) : "");
	}
	
    public static void main(String[] args) {
        String str = "java game programming";
        
        str = replaceCharacter(str, 6, '_');
        System.out.println(str);
        
        str = replaceCharacter(str, 0, 'J');
        System.out.println(str);
        
        str = replaceCharacter(str, str.length() - 1, '@');
        System.out.println(str);
    }
}



/*
run:
  
java g_me programming
Java g_me programming
Java g_me programmin@
  
*/


 



answered Mar 1, 2024 by avibootz
...