0

我知道这可能是有史以来最愚蠢的问题之一,但我的大脑似乎不在了。我有这个方法可以从 vCard 中生成一个字符串:

public static String process(String vCard) {
        ArrayList<ArrayList<String>> vCardData = parseData(vCard);
        if (vCardData != null) {
            StringBuilder readableVCard = new StringBuilder();

            for (int i = 0; i < FIELD_COUNT; i++) {
                ArrayList<String> vCardDataField = vCardData.get(i);

                if (vCardDataField.size() > 0) {
                    String field = null;

                    if (i == FORMATTED_NAME) {
                        field = "Name: ";
                    } else if (i == PHONE_MOBILE) {
                        field = "Phone (mobile): ";
                    } else if (i == PHONE_HOME) {
                        field = "Phone (home): ";
                    } else if (i == PHONE_WORK) {
                        field = "Phone (work): ";
                    } else if (i == PHONE) {
                        field = "Phone: ";
                    } else if (i == FAX_HOME) {
                        field = "Fax (home): ";
                    } else if (i == FAX_WORK) {
                        field = "Fax (work): ";
                    } else if (i == PAGER) {
                        field = "Pager: ";
                    } else if (i == EMAIL_HOME) {
                        field = "Email (home): ";
                    } else if (i == EMAIL_WORK) {
                        field = "Email (work): ";
                    } else if (i == EMAIL) {
                        field = "Email: ";
                    } else if (i == ORGANISATION) {
                        field = "Company: ";
                    } else if (i == JOB_TITLE) {
                        field = "Job title: ";
                    } else if (i == ADDRESS_HOME) {
                        field = "Address (home): ";
                    } else if (i == ADDRESS_WORK) {
                        field = "Address (work): ";
                    } else if (i == ADDRESS) {
                        field = "Address: ";
                    } else if (i == IM_SKYPE) {
                        field = "Skype: ";
                    } else if (i == IM_GOOGLE) {
                        field = "Google Talk: ";
                    } else if (i == IM_JABBER) {
                        field = "Jabber: ";
                    } else if (i == IM_YAHOO) {
                        field = "Yahoo: ";
                    } else if (i == IM_MSN) {
                        field = "MSN: ";
                    } else if (i == IM_ICQ) {
                        field = "ICQ: ";
                    } else if (i == IM_AIM) {
                        field = "AIM: ";
                    } else if (i == TWITTER) {
                        field = "Twitter: ";
                    } else if (i == BIRTHDAY) {
                        field = "Birthday: ";
                    } else if (i == ANNIVERSARY) {
                        field = "Anniversary: ";
                    } else if (i == NOTES) {
                        field = "Notes: ";
                    } else if (i == WEBSITE) {
                        field = "Website: ";
                    } else {
                        continue;
                    }

                    if (readableVCard.length() != 0) {
                        readableVCard.append("\n");
                    }
                    readableVCard.append(field);

                    for (int j = 0; j < vCardDataField.size(); j++) {
                        if (j != 0) {
                            readableVCard.append("; ");
                        }
                        readableVCard.append(vCardDataField.get(j));
                    }
                }
            }

            if (readableVCard.length() != 0) {
                String textVCard = readableVCard.toString();
                try {
                    textVCard = qpDecoder.decode(readableVCard.toString());
                } catch (Exception e) {
                    Logger.e("VCard to UTF-8", e.getMessage());
                }

                return (textVCard);
            }
        }

        return (null);
    }

所以我目前的输出是这样的:

Name: Marko
Phone(mobile):1312
Phone(fax):441231
Phone(home):543534
Email(home):dddd
Email(work):eeee
Email(other):aaaa

我想要的是在组(姓名/电话/电子邮件)之间添加一个换行符,所以我得到这样的东西:

Name: Marko

Phone(mobile):1312
Phone(fax):441231
Phone(home):543534

Email(home):dddd
Email(work):eeee
Email(other):aaaa

Addressblablabla
.
.
.

IMsblablabla
.
.
.

有人可以帮忙吗?谢谢。

4

2 回答 2

0

您可以迭代字符串数组,按 : 分割,然后检查下一项是否以 current 开头。如果是,则什么也不做,如果不是,则将换行符添加到当前项目并在数组中替换它。

注意:用 : 分割也分割所有电话 (xxx) 类型。为避免这种情况,您可以创建枚举类型,如果字符串以“phone”开头,则将类型映射到 Phone,并在遍历数组时将该枚举用作当前类型。如果当前类型与下一个类型不同,则添加换行符。

于 2013-07-08T11:02:15.780 回答
0

您可以通过 System.getProperty("line.separator"); 附加行分隔符

于 2013-07-08T10:55:43.560 回答