在字符串中的项目组之间添加空行

时间:2013-07-08 10:52:45

标签: android string vcard

我知道这可能是最愚蠢的问题之一,但我的大脑似乎已经消失了。 我有这个方法,它使用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
.
.
.

有人可以帮忙吗? 感谢。

2 个答案:

答案 0 :(得分:0)

  

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

答案 1 :(得分:0)

你可以迭代字符串数组,拆分:然后检查下一项是否以current开头。如果是,则不执行任何操作,如果不是,则将换行添加到当前项并将其替换为数组。

注意:拆分:还会分隔所有手机(xxx)类型。为避免这种情况,您可以创建枚举类型,如果字符串以“phone”开头,则将地图类型创建为Phone,并在迭代数组时将该枚举用作当前类型。如果当前类型与下一个类型不同,则添加换行符。