How to append single quotes into both side of stringBuilder object in java

大憨熊 提交于 2020-02-07 21:45:54

问题


I would like to add single quotes into an array of string. If I has 5 values.(1,2,3,4,5). I need like this '1','2','3','4','5'

I have tried some like below but I am getting error

public class Main()
{
        StringBuilder values=new StringBuilder();           

            values=getValues(1);

            System.out.println(values.toString());

            public StringBuilder getValues(int productid){
                    StringBuilder Variable_1=new StringBuilder();       
                    Variable_1=get(productid);                  
                    return Variable_1.deleteCharAt(Variable_1.length()-1);
                }

    public static StringBuilder get(String product)
    {
    /Some quoatoes to retrive value from db/
    while (rs.next()) {
                    number = rs.getString("org");
                    orgNames.append("'");
                    orgNames.append(number);
                    orgNames.append("'");
                    orgNames.append(",");

                }
    return orgNames;
    }
}  

I am getting exception as follow

  java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:766)
        at java.lang.StringBuilder.deleteCharAt(StringBuilder.java:280)
        at calc.getValues(process.java:8)

回答1:


in the last iteration Variable_1.length()-1 returns index out of bound because Variable_1.length() is zero.

Check

if(Variable_1.length()>0)

before deleteCharAt




回答2:


I am not quite sure what your code does but I've written a function that'll take an array of Strings as input and surround each element with quotes.

String[] appendQuotes(String[] array)
{
    List<String> result = new ArrayList<>();
    for(String str : array)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("'");
        sb.append(str);
        sb.append("'");
        result.add(sb.toString());
    }
    return result.toArray();
}



回答3:


There is already StringUtils.wrap method in Apache Commons Lang 3 which prepends and appends char/string to other string, sample code using Java8 features would look like this:

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.Assertions;
import org.testng.annotations.*;

import java.util.stream.Stream;

public class StringArrayWrapTest {

    public static final String SINGLE_QUOTE_SIGN = "'";

    @Test
    public void stringArrayWrapTest() {
        //given
        String[] arrayOfStrings = {"1", "2", "3", "4", "5"};

        //when
        String[] arrayOfStringsWithQuotes = (String[]) Stream.of(arrayOfStrings)
                .map((stringFromArray -> StringUtils.wrap(stringFromArray, SINGLE_QUOTE_SIGN)))
                .toArray(size -> new String[size]);
        //then
        Assertions.assertThat(arrayOfStringsWithQuotes)
                .containsExactly(new String[]{"'1'", "'2'", "'3'", "'4'", "'5'"});
    }
}



回答4:


The same thing could be done via Spring Framework :

import org.springframework.util.StringUtils;

String wrapWithSingleQuotes(String sampleString) {
        return StringUtils.quote(sampleString);
    }


来源:https://stackoverflow.com/questions/30465323/how-to-append-single-quotes-into-both-side-of-stringbuilder-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!