开发者

Storing in String [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What's the most elegant way to concatenate a list of values with delimiter in Java?

I have 3 different w开发者_Go百科ords listed in property file in 3 different lines. QWERT POLICE MATTER

I want to read that property file and store those 3 words in String, not in aarylist separated by whitespace. The output should look like this String str = { QWERT POLICE MATTER}

Now I used the follwoing code and getting the output without white space in between the words: String str = {QWERTPOLICEMATTER}. What should I do to the code to get the string result with words separated by whitespace.

FileInputStream in = new FileInputStream("abc.properties");
            pro.load(in);
            Enumeration em = pro.keys();
            StringBuffer stringBuffer = new StringBuffer();
            while (em.hasMoreElements()){
               String str = (String)em.nextElement();
               search = (stringBuffer.append(pro.get(str)).toString());


Try:

search = (stringBuffer.append(" ").append(pro.get(str)).toString());


Just append a blank space in your loop. See the extra append below:

while (em.hasMoreElements()){
    String str = (String)em.nextElement();
    search = (stringBuffer.append(pro.get(str)).toString());
    if(em.hasNext()){
        stringBuffer.append(" ")
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜