Java: convert List to a String

前端 未结 22 2587
日久生厌
日久生厌 2020-11-22 01:03

JavaScript has Array.join()

js>[\"Bill\",\"Bob\",\"Steve\"].join(\" and \")
Bill and Bob and Steve

Does Java have anything

22条回答
  •  盖世英雄少女心
    2020-11-22 01:34

    You can do this:

    String aToString = java.util.Arrays.toString(anArray);
    // Do not need to do this if you are OK with '[' and ']'
    aToString = aToString.substring(1, aToString.length() - 1);
    

    Or a one-liner (only when you do not want '[' and ']')

    String aToString = java.util.Arrays.toString(anArray).substring(1).replaceAll("\\]$", "");

    Hope this helps.

提交回复
热议问题