Search code examples
javaarraysobject-to-string

convert chars from a position in a char array through an other position into a string in java


So i have a string and an array of characters and what i want to do is make a string with some characters between two positions of this array.

Ex. If Arr = {a,b,c,d,e,f} I want Str= "bcde"


Solution

  • You can use the String(char[], int, int) constructor:

    char[] arr = { 'a', 'b', 'c', 'd', 'e', 'f' };
    
    int start = 1, end = 4;
    
    System.out.println(new String(arr, start, end - start + 1));
    
    bcde