How to replace the first N characters in a string in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String str = "java";
        
        int N = 3;
        str = "XYZ" + str.substring(N);
	    
	    System.out.println(str);

    }
}




/*
run:

XYZa

*/

 



answered Jun 12, 2022 by avibootz
edited Jun 12, 2022 by avibootz

Related questions

1 answer 172 views
1 answer 128 views
1 answer 100 views
2 answers 134 views
1 answer 116 views
1 answer 106 views
1 answer 114 views
...