How to copy a string in Java

11 Answers

0 votes
// Copy using simple assignment

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";

        String dest = src; // Copies reference only

        System.out.println(dest);
    }
}


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
edited 2 days ago by avibootz
0 votes
// Copy using new String()

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";

        String dest = new String(src);

        System.out.println(dest);
    }
}


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
edited 2 days ago by avibootz
0 votes
// Copy using String.valueOf()

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";

        String dest = String.valueOf(src);

        System.out.println(dest);
    }
}


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using String.format()

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";

        String dest = String.format("%s", src);

        System.out.println(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using StringBuilder

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        
        StringBuilder sb = new StringBuilder();
        sb.append(src);
        String dest = sb.toString();

        System.out.println(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using StringBuffer

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        
        StringBuffer sb = new StringBuffer();
        sb.append(src);
        String dest = sb.toString();

        System.out.println(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using toCharArray()

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        
        char[] arr = src.toCharArray();
        String dest = new String(arr);

        System.out.println(dest);
    }
}


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using Arrays.copyOf()

import java.util.Arrays;

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        
        char[] arr = Arrays.copyOf(src.toCharArray(), src.length());
        String dest = new String(arr);

        System.out.println(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using manual loop

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        char[] arr = new char[src.length()];

        for (int i = 0; i < src.length(); i++) {
            arr[i] = src.charAt(i);
        }

        String dest = new String(arr);
        System.out.println(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using substring()

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        
        String dest = src.substring(0); // full copy

        System.out.println(dest);
    }
}


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using concat()

public class Copy {
    public static void main(String[] args) {
        String src = "Programming is fun";
        
        String dest = src.concat("");

        System.out.println(dest);
    }
}


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz

Related questions

6 answers 19 views
7 answers 19 views
8 answers 27 views
8 answers 24 views
7 answers 19 views
...