String SomeLongString = JavaAPIMethodFor(String [] strings,String delimeter)

时间:2011-11-16 22:16:30

标签: java string implode

String SomeLongString = JavaAPIMethodFor (String[] strings, String delimiter);

或者这也可以起作用:

String SomeLongString = JavaAPIMethodConvertingArrayWithDelimeter (String[] strings, char delimiter)

我想将字符串连接成一个更大的字符串,但这根本没用。我知道我可以使用Arrays.toString(Object someString)将所有数据附加到字符串中,然后调整字符串,删除不需要的字符。但这并不是非常有效,只是为了重建它而构建一些东西。因此循环遍历String []并在每个元素之间添加我的字符​​[s]可能是要走的路:

import static org.junit.Assert.*;

import org.junit.Test;

public class DelimetedString {


    private String delimitedString(String [] test, String delimiter){
        StringBuilder result = new StringBuilder();
        int counter = 0;
        if(test.length > counter){
           result.append(test[counter++]);
           while(counter < test.length){
              result.append(delimiter);
              result.append(test[counter++]);
           }
        }
        return result.toString();
    }

    @Test
    public void test() {
        String [] test = new String[] 
{"cat","dog","mice","cars","trucks","cups","I don't know", "anything!"};
        String delimiter = " |...| ";
        assertEquals("DelimitedString misformed",
        "cat |...| dog |...| mice |...| cars |...| trucks "
            +"|...| cups |...| I don't know |...| anything!",
        delimitedString(test, delimiter));
    }

}

我想要的是在使用标记器之后将字符串放在一起的东西。我放弃了这个想法,因为它可能比它更加繁琐。我选择在较大的String中处理子字符串,我在“答案”中包含了代码。

我要问的是 - java API是否具有与delimitedString函数等效的函数?几个人的答案似乎不是。

2 个答案:

答案 0 :(得分:1)

据我所知,没有内置方法。你可以做的是获取它的子串:

String str = Arrays.toString(arrayHere);
str = str.substring(1, str.lenght() - 1);

答案 1 :(得分:1)

这是我最后拼凑的课程。我想将文本文件拆分为块,然后使用行号通过Servlet发送块以获取相关数据。这是一个在我家中的服务器上运行的应用程序,它允许我跨不同设备读取我的文本文件,并保持与文件相关的元数据。

package net.stevenpeterson.bookreaderlib;

public class SplitStringUtility {

private String subject;

public SplitStringUtility(String subject) {
    this.subject = subject;
}

public String getRow(int rowAddress, int charsPerRow) {
    String result = "";
    if (rowAddress >= 0 && charsPerRow > 0) {
        int startOfSubString = rowAddress * charsPerRow;
        int endOfSubString = startOfSubString + charsPerRow;
        result = getSubString(startOfSubString, endOfSubString);
    }
    return result;
}

private String getSubString(int startOfSubString, int endOfSubString) {
    String result = "";
    if (startOfSubString <= subject.length()) {
        if (endOfSubString <= subject.length()) {
            result = subject.substring(startOfSubString, endOfSubString);
        } else {
            result = subject.substring(startOfSubString);
        }
    }
    return result;
}

}

我测试过:

package net.stevenpeterson.bookreaderlib;

import static org.junit.Assert.*;

import net.stevenpeterson.bookreaderlib.SplitStringUtility;

import org.junit.Test;

public class SplitStringUtilityTest {

    public final String empty = "";
    public final String single ="a";
    public final String twoChars = "ab";
    public final String threeChars = "abc";
    public final String manyChars = "abcdefghijklmnopqrstuvwxyz";

    private SplitStringUtility splitter;

    @Test
    public void splitTestsEmpty() {
        makeSplitter(empty);
        assertEquals("We are trying to get a non-existant string",
            "", 
            splitter.getRow(3,4));
    }


    @Test
    public void splitTestsNegativeRowOrColumn() {
        makeSplitter(manyChars);
        assertEquals("We are trying to get a non-existant string",
            "",
            splitter.getRow(-3,4));

        assertEquals("We are trying to get a non-existant string",
            "", 
            splitter.getRow(-1,-1));

        assertEquals("We are trying to get a non-existant string",
            "", 
            splitter.getRow(1,-1));
    }



    @Test
    public void splitTestsSingleCharacterStrings() {
        makeSplitter(single);

        assertEquals("split the string consisting of 1 character",
            "a",
            splitter.getRow(0,1));

        assertEquals("split the string consisting of 1 character, " +
                "but ask for two characters, " +
                "the string containing only a single char " +
                "should be returned","a", splitter.getRow(0,2));

    }

    @Test
    public void splitTestsTwoChars() {
        makeSplitter(twoChars);
        assertEquals("Row #0 of the ab string in 1 column rows",
            "a", 
            splitter.getRow(0,1));

        assertEquals("Row #1 of the ab string in 1 column rows",
            "b",
            splitter.getRow(1,1));

        assertEquals("Row #0 of the ab string in 2 column rows",
            "ab",
            splitter.getRow(0,2));

        assertEquals("Row #1 of the ab string in 4 column rows " 
            +"should return a blank string",
            "",
            splitter.getRow(1,4));

        assertEquals("Row #0 of the ab string in 4 column rows " 
            +"should return the ab string",
            twoChars, 
            splitter.getRow(0,4));

    }


    @Test
    public void splitTestsManyChars() {
        //split the alphabet into rows of 4 characters, return row 3
        //Expect "mnop"
        makeSplitter(manyChars);
        assertEquals("Row #3 of the alphabet in 4 column rows",
            "mnop",
            splitter.getRow(3,4));

        assertEquals("Row #0 of the alphabet in 4 column rows",
            "abcd", 
            splitter.getRow(0,4));

        assertEquals("Row #0 of the alphabet in 26 column rows",
            manyChars, 
            splitter.getRow(0,26));

        assertEquals("Row #0 of the alphabet in 26 column rows",
            "z",
            splitter.getRow(1,25));

        assertEquals("Row #0 of the alphabet in 27 column rows" 
            + " since the alphabet has 26 characters "
            + "it would be nice to get what we have", manyChars,
           splitter.getRow(0,27));
    }


    private void makeSplitter(String subject){
        splitter = new SplitStringUtility(subject);
    }


}