String SomeLongString = JavaAPIMethodFor (String[] 字符串, String 分隔符)

发布于 2024-12-16 16:24:54 字数 1527 浏览 2 评论 0原文

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

或者这也可以工作:

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

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

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));
    }

}

我想要的是在使用分词器后将字符串组合在一起。我放弃了这个想法,因为它可能比它的价值更麻烦。我选择从较大的字符串中处理子字符串,我在“答案”中包含了该代码。

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

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

Or this could work as well:

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

I wanted to join strings into a larger string, but this is simply useless. I am aware that I could append all the data into a string using Arrays.toString(Object someString) then adjust the string, removing unwanted characters. But that's not really efficient, building something, only to rebuild it. So looping through the String[] and adding my character[s] between each element is probably the way to go:

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));
    }

}

What I wanted was something to put together a string after using a tokenizer. I abandoned that Idea, since it's probably more cumbersome then it's worth. I chose to address a Sub-Strings from within the larger String, I included the code for that, in an "answer".

What I was asking is - Does the java API have an equivalent function as the delimitedString function? The answer from several people seems to be no.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-12-23 16:24:54

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

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

As far as I know, there isn't a built in method. What you can do is taking the substring of it:

String str = Arrays.toString(arrayHere);
str = str.substring(1, str.lenght() - 1);
魂归处 2024-12-23 16:24:54

这是我最终整理的一堂课。我想将文本文件分成块,然后使用行号通过 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);
    }


}

Here is a class that I ended up throwing together. I want to break apart a text file into chunks, then send the chunks through a Servlet using the row number to obtain the relevant data. This is a app running on a server in my home that will allow me to read my text files across different devices, and keep meta-data related to a file.

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;
}

}

I tested against:

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);
    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文