现在正在做一个东西,需要将从数据库某张表中数据取出,但取出一条数据不是将一个字段一个字段的单独取值,而是想将所有字段内容以一个分隔符(如“,”号)分隔开后,合并成一个String!
请问有什么简单方法吗?
因为有多张不同结构的表要进行这样操作,因此想要一个通用的方法来做^^
请高手指定,最好能给出例子来
先谢了!

解决方案 »

  1.   

    apache的common.lang包有个StringUtils.java
    有这个方法可以    /**
         * <p>Joins the elements of the provided array into a single String
         * containing the provided list of elements.</p>
         *
         * <p>No delimiter is added before or after the list.
         * Null objects or empty strings within the array are represented by
         * empty strings.</p>
         *
         * <pre>
         * StringUtils.join(null, *)               = null
         * StringUtils.join([], *)                 = ""
         * StringUtils.join([null], *)             = ""
         * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
         * StringUtils.join(["a", "b", "c"], null) = "abc"
         * StringUtils.join([null, "", "a"], ';')  = ";;a"
         * </pre>
         *
         * @param array  the array of values to join together, may be null
         * @param separator  the separator character to use
         * @return the joined String, <code>null</code> if null array input
         * @since 2.0
         */
    public static String join(Object[] array, char separator)如果你查询出来的是对象数组,这个方法刚刚好,
    如果是基于框架查询的,结果是实体类,你可以转换一下
      

  2.   

    StringUtils确实很实用。
    楼上方法很好。
      

  3.   

    从数据库里查出来还是Cursor,不想再将Cursor中的值一个个取出,想直接对Cursor操作而取得一个所需String,请问可行吗?
      

  4.   

    是实体就更简单了,直接覆写toString()
    就ok了
      

  5.   

    可以考虑利用反射将对象(比如Cursor)所有的成员变量用分隔符连接起来,这样就可以实现通用了。
      

  6.   

    直接用toString()吗?
    cursor.toString()....这样可以把所有字段以字符串方式输出?