构建字符串避免重复的最佳方法

时间:2017-05-24 12:38:42

标签: java string url string-building

我有一个将在URL中使用的硬编码字符串,我想知道构建此字符串的最佳方法是什么,而不必重复单词country, or, and, number?这是一个缩短版本,因为有更多的国家/地区和数字会进入网址。

String url = "&filter=country='UK' or "
                      + "country='FR' or "
                      + "country='CA' or "
                      + "country='US' and "
                      + "number='123' and "
                      + "number='789'";

3 个答案:

答案 0 :(得分:1)

您可以结合使用StringBuilderString格式来构建特定的URL参数化。

以下是一个例子:

// test data
List<String> countries = Arrays.asList("UK", "FR","CA", "US");
List<String> numbers = Arrays.asList("123", "789");

// these can be compile-time constants
String disjunct  = " or ";
String conjunct = " and ";
String countryFormat = "country='%s'";
String numberFormat = "number='%s'";

// result 
StringBuilder result = new StringBuilder("&filter=");

// adding countries
for (String country: countries) {
    result.append(String.format(countryFormat, country)).append(disjunct);
}
// removing last "or"
result.delete(result.lastIndexOf(disjunct), result.length());

// adding first "and"
result.append(conjunct);

// adding numbers
for (String number: numbers) {
    result.append(String.format(numberFormat, number)).append(conjunct);
}
// removing last "and"
result.delete(result.lastIndexOf(conjunct), result.length());

// printing result
System.out.println(result);

<强>输出

&filter=country='UK' or country='FR' or country='CA' or country='US' and number='123' and number='789'

答案 1 :(得分:0)

我建议您创建数组以保存每个差异,并将它们附加到for循环中:

String[] countries = {"'UK' or ", "'FR' or ", "'CA' or ", "'US' and "};

String[] numbers = {"'123' and ", "'789'"};

StringBuilder sb = new StringBuilder("&filter=");

for (String country : countries) {
    sb.append("country=").append(country);
}

for (String number : numbers) {
    sb.append("number=").append(number);
}

String url = sb.toString();

答案 2 :(得分:0)

您还可以创建一个帮助程序类,为您构建字符串。

public class Test {
private Test() {

    // each country for its own
    IFilterBuilder filterBuilder = new FilterBuilder();
    filterBuilder
            .country("UK").or()
            .country("FR").or()
            .country("CA").or()
            .country("US").and()
            .number(123).and()
            .number(789);

    String myFilter = filterBuilder.getResult();
    System.out.println(myFilter);


    // or a short version with a helper method
    IFilterBuilder smartFilterBuilder = new FilterBuilder();
    smartFilterBuilder.inAnyCountry("UK", "FR", "CA", "US").and().withNumbers(123, 789);

    String result = smartFilterBuilder.getResult();
    System.out.println(result);
}

public static void main(String[] args) {
    new Test();
}

interface IFilterBuilderBinder {
    IFilterBuilder or();
    IFilterBuilder and();
}

interface IFilterBuilder {
    IFilterBuilderBinder country(String countryName);
    IFilterBuilderBinder number(int number);
    String getResult();

    IFilterBuilderBinder inAnyCountry(String... country);
    IFilterBuilderBinder withNumbers(int... numbers);
}

private final static class FilterBuilder implements IFilterBuilder, IFilterBuilderBinder {
    private String filter = "&filter=";

    public IFilterBuilderBinder country(String countryName) {
        filter += "country='" + countryName + "'";
        return this;
    }

    @Override
    public IFilterBuilderBinder number(int number) {
        filter += "number='" + number + "'";
        return this;
    }

    public String getResult() {
        return filter;
    }

    @Override
    public IFilterBuilderBinder inAnyCountry(String... country) {
        for (int i = 0; i < country.length; i++) {
            country( country[i]);
            if(i!=country.length-1){
                or();
            }
        }
        return this;
    }

    @Override
    public IFilterBuilderBinder withNumbers(int... numbers) {
        for (int i = 0; i < numbers.length; i++) {
            number(numbers[i]);
            if(i!=numbers.length-1){
                and();
            }
        }
        return this;
    }

    @Override
    public IFilterBuilder or() {
        filter += " or ";
        return this;
    }

    @Override
    public IFilterBuilder and() {
        filter += " and ";
        return this;
    }
}

}