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 184 views
1 answer 140 views
1 answer 114 views
2 answers 149 views
1 answer 127 views
1 answer 129 views
1 answer 122 views
...