1

Hey guys can somebody show me a way good way of concatenating these strings with commas

Basically Im building a header criteria string showing which forms variables have been selected. I need to put commas in between the values and keep the break tags in place...can somebody see a better way to do it. I didnt want commas if there were just on value

This is what it looks like currently formatted:

enter image description here

protected final String getCriteriaHeader(MetricFilterCriteriaForm form)
{
    String filterCriteria = "<br/>";

    }
    if (form.isSacNone() || form.isSac1() || form.isSac2() || form.isSac3())
    {
        filterCriteria = filterCriteria + "SAC:";
    }
    if (form.isSacNone())
    {
        filterCriteria = filterCriteria + " NONE";
    }
    if (form.isSac1())
    {
        filterCriteria = filterCriteria + " 1";
    }
    if (form.isSac2())
    {
        filterCriteria = filterCriteria + " 2";
    }
    if (form.isSac3())
    {
        filterCriteria = filterCriteria + " 3";
    }
    if (form.isSac1() || form.isSac2() || form.isSac3())
    {
        filterCriteria = filterCriteria + "<br/>";
    }
    if (form.isRegularScheduleType() || form.isLotScheduleType() || form.isBatchScheduleType())
    {
        filterCriteria = filterCriteria + "Schedule Type:";
    }
    if (form.isRegularScheduleType())
    {
        filterCriteria = filterCriteria + " Regular";
    }
    if (form.isLotScheduleType())
    {
        filterCriteria = filterCriteria + " Lot";
    }
    if (form.isBatchScheduleType())
    {
        filterCriteria = filterCriteria + " Batch";
    }

    return filterCriteria;
}
1
  • What have you tried? What conditions define 'good'? I'm guessing 'has a red stripe' simply because that is very spiffy. Commented Feb 20, 2013 at 14:51

5 Answers 5

3

There are different ways to concatenate a set of values in a string with a separator.

With StringBuilder

Add the values with the comma, then remove the last comma manually.

StringBuilder sb = new StringBuilder();
if (/*condition1*/) {
    sb.add("A,"); // value with comma
}
if (/*condition2*/) {
    sb.add("B,");
}
sb.delete(sb.length()-1, sb.length()); // remove last character, which is the comma.
String result = sb.toString(); // get the result string.

With Guava's Joiner

Put it all in a List and use Joiner.

List<String> list = Lists.newArrayList();
if (/*condition1*/) {
    list.add("A"); // no comma here
}
if (/*condition2*/) {
    list.add("B");
}
String result = Joiner.on(",").join(list); // use Joiner to join elements of the list.

Alternatively to Guava, there is StringUtils.Join from Apache Common Lang. See @Iswanto San's answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice alternative with the Joiner, got to love Guava.
3

You can use StringUtils.Join from Apache Common Lang

Example :

protected final String getCriteriaHeader(MetricFilterCriteriaForm form)
{
    String filterCriteria = "<br/>";
    List<String> sacs = new ArrayList<String>();
    List<String> schedules = new ArrayList<String>();

    if (form.isSacNone() || form.isSac1() || form.isSac2() || form.isSac3())
    {
        filterCriteria = filterCriteria + "SAC:";
    }
    if (form.isSacNone())
    {
        filterCriteria = filterCriteria + " NONE";
    }
    if (form.isSac1())
    {
        sacs.add(" 1");
    }
    if (form.isSac2())
    {
        sacs.add(" 2");
    }
    if (form.isSac3())
    {
        sacs.add(" 3");
    }
    filterCriteria += StringUtils.join(saces, ",");
    if (form.isSac1() || form.isSac2() || form.isSac3())
    {
        filterCriteria = filterCriteria + "<br/>";
    }
    if (form.isRegularScheduleType() || form.isLotScheduleType() || form.isBatchScheduleType())
    {
        filterCriteria = filterCriteria + "Schedule Type:";
    }
    if (form.isRegularScheduleType())
    {
        schedules.add(" Regular");
    }
    if (form.isLotScheduleType())
    {
        schedules.add(" Lot");
    }
    if (form.isBatchScheduleType())
    {
        schedules.add(" Batch");
    }
    filterCriteria+=StringUtils.join(schedules, ",");

    return filterCriteria;
}

Comments

1

At first avoid creating so much String instances by using StringBuilder. Then nest the conditions to speed things up a bit and to get more structure.

protected final String getCriteriaHeader(MetricFilterCriteriaForm form)
{
    StringBuilder filterCriteria = new StringBuilder("<br/>");
    if (form.isSacNone() || form.isSac1() || form.isSac2() || form.isSac3())
    {
        filterCriteria.append("SAC:");
        if (form.isSacNone())
            filterCriteria.append(" NONE");
        if (form.isSac1() || form.isSac2() || form.isSac3())
        {
            if (form.isSac1())
                filterCriteria.append(" 1,");
            if (form.isSac2())
                filterCriteria.append(" 2,");
            if (form.isSac3())
                filterCriteria.append(" 3,");
            if(','==filterCriteria.charAt(filterCriteria.length-1) )
                filterCriteria.deleteCharAt(filterCriteria.length-1)
            filterCriteria.append("<br/>");
        }
    }
    if (form.isRegularScheduleType() || form.isLotScheduleType() || form.isBatchScheduleType())
    {
        filterCriteria.append("Schedule Type:");
        if (form.isRegularScheduleType())
            filterCriteria.append(" Regular,");
        if (form.isLotScheduleType())
            filterCriteria.append(" Lot,");
        if (form.isBatchScheduleType())
            filterCriteria.append(" Batch,");
        if(','==filterCriteria.charAt(filterCriteria.length-1) )
            filterCriteria.deleteCharAt(filterCriteria.length-1)
    }
    return filterCriteria.toString();
}

If only one condition can be true,you can also use else if instead of cascades of if.

1 Comment

Thanks...but you didnt append any commas anywhere....you just rewrote it with stringBuilder
1

You could use a StringBuilder to build the string, it's better than simple string concatenation :

StringBuilder sb = new StringBuilder();
if(XX) {
  sb.append("XX");
}
return sb.toString();

Hope this helps :)

PS: Note that StringBuilder is faster than StringBuffer, but the latter is Thread-safe.

EDIT

I re-read your question, and it seems I don't answer it well, although I provided useful advice (IMHO). I don't understand exactly what you need.

1 Comment

i want to publish the same but you faster ;) +1
1

I would suggest whacking the thing into a List then using a StringBuilder:

protected final String getCriteriaHeader(MetricFilterCriteriaForm form) {
        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<br/>");

        final List<String> sacList = new LinkedList<String>();
        if (form.isSacNone() || form.isSac1() || form.isSac2() || form.isSac3()) {
            stringBuilder.append("SAC: ");
        }
        if (form.isSacNone()) {
            sacList.add("NONE");
        }
        if (form.isSac1()) {
            sacList.add("1");
        }
        if (form.isSac2()) {
            sacList.add("2");
        }
        if (form.isSac3()) {
            sacList.add("3");
        }
        final Iterator<String> sacIter = sacList.iterator();
        while (sacIter.hasNext()) {
            stringBuilder.append(sacIter.next());
            if (sacIter.hasNext()) {
                stringBuilder.append(", ");
            }
        }
        if (form.isSac1() || form.isSac2() || form.isSac3()) {
            stringBuilder.append("<br/>");
        }
        final List<String> scheduleTypeList = new LinkedList<String>();
        if (form.isRegularScheduleType() || form.isLotScheduleType() || form.isBatchScheduleType()) {
            scheduleTypeList.add("Schedule Type: ");
        }
        if (form.isRegularScheduleType()) {
            scheduleTypeList.add("Regular");
        }
        if (form.isLotScheduleType()) {
            scheduleTypeList.add("Lot");
        }
        if (form.isBatchScheduleType()) {
            scheduleTypeList.add("Batch");
        }
        final Iterator<String> scheduleTypeIter = scheduleTypeList.iterator();
        while (scheduleTypeIter.hasNext()) {
            stringBuilder.append(scheduleTypeIter.next());
            if (scheduleTypeIter.hasNext()) {
                stringBuilder.append(", ");
            }
        }
        return stringBuilder.toString();
}

1 Comment

Hey thanks for the quick response......I prolly will change it over to use stringBuilder later on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.