Click here to Skip to main content
15,302,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The method is simple. Takes in a array of strings and a char. and returns a string of those array's separated with the char.

Problem with my code is that it dose not return the array as a string. Rather in this format: `[element1, element 2, .....]`

I want it to return the array as a string without the brackets and commas. How can i approach this?

What I have tried:

public static String Separator(String[] list, char sep){
      
      for(int i =0; i<list.length;i++){
         list[i] += sep;
      }
          
        return Arrays.toString(list);
    }
Posted
Updated 2-Apr-21 20:27pm
v3
Comments
Richard Deeming 6-Apr-21 10:02am
   
Removing the content of your question after someone has gone to the trouble of answering you is extremely rude.

I have reverted your destructive edit.

1 solution

No. You want to return a string, so try using a StringBuilder:
Java
public class Main {
	public static void main(String[] args) {
		System.out.println(Separator(new String[]{"Hello", 
		                                          "World", 
		                                          "this", 
		                                          "is", 
		                                          "Java"},
		                             ','));
	}
	
	public static String Separator(String[] list, char sep) {
        StringBuilder result = new StringBuilder();
        boolean between = false;
        for(int i = 0; i < list.length; i++) {
            if (between) result.append(sep);
            result.append(list[i]);
            between = true;
        }
        return result.toString();
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900