Answers for "java replace character in string at index"

2

java string replace character at position

String str = in.nextLine();	//Original String
char cr = in.next().charAt(0); // character to replace
int index = in.nextInt();	// Index where replaced
str = str.substring(0, index) + cr + str.substring(index + 1);// modified string
Posted by: Guest on May-02-2020
0

replace substring at index java

public static void main(String[] args) {

        StringBuffer buf = new StringBuffer("123456789");

        int start = 3;
        int end = 6;
        buf.replace(start, end, "foobar"); 
        System.out.println(buf);

    }

}
Posted by: Guest on September-12-2021
3

replace character in string java

String str = ".............................."; 

        int index = 5; 
  
        char ch = '|'; 
 
        StringBuilder string = new StringBuilder(str); 
        string.setCharAt(index, ch); 

        System.out.println(string);
Posted by: Guest on August-14-2020
0

how to replace a character with another character in a string in java

public class JavaExample{
   public static void main(String args[]){
	String str = new String("Site is BeginnersBook.com");

	System.out.print("String after replacing com with net :" );
	System.out.println(str.replaceFirst("com", "net"));

	System.out.print("String after replacing Site name:" );
	System.out.println(str.replaceFirst("Beginners(.*)", "XYZ.com"));
   }
}
Posted by: Guest on April-05-2021
0

java replaceall single character

.replaceAll("(?<!\\S)[^ ](?!\\S)", " ").trim()
Posted by: Guest on March-06-2020

Code answers related to "java replace character in string at index"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language