在给定特定开始和结束位置的情况下构建字符串的好方法是什么?

发布于 2024-08-29 07:23:29 字数 673 浏览 12 评论 0原文

(java 1.5)

我需要分段构建一个字符串。我得到了一组(子)字符串,每个字符串都有它们在最终字符串中所属位置的起点和终点。想知道是否有一些规范的方法可以做到这一点。这不是家庭作业,我可以使用任何可授权的 OSS,例如 jakarta commons-lang StringUtils 等。

我的公司有一个使用 CharBuffer 的解决方案,我很乐意保持原样(并添加一些单元测试,没有(?!)),但代码相当丑陋,我想要一些更容易阅读的东西。

正如我所说,这不是家庭作业,我不需要完整的解决方案,只需要一些指向库或 java 类的指针,它们可能会给我一些见解。 String.Format 似乎不太正确...

我必须尊重输入太长和太短等。子字符串将按照它们出现的顺序覆盖(如果重叠)。

作为输入的示例,我可能有类似的内容:

String:start:end

FO:0:3 (字符串比字段短)

BAR:4:5 (字符串比字段大)

BLEH:5:9 (字符串覆盖前一个字段)

我想最终得到

FO  BBLEH  
01234567890

(编辑:致所有人 - StringBuilder (特别是“预分配到已知长度,然后使用 .replace()”主题)似乎是我正在考虑的。感谢所有建议的人!)

(java 1.5)

I have a need to build up a String, in pieces. I'm given a set of (sub)strings, each with a start and end point of where they belong in the final string. Was wondering if there were some canonical way of doing this. This isn't homework, and I can use any licensable OSS, such as jakarta commons-lang StringUtils etc.

My company has a solution using a CharBuffer, and I'm content to leave it as is (and add some unit tests, of which there are none (?!)) but the code is fairly hideous and I would like something easier to read.

As I said this isn't homework, and I don't need a complete solution, just some pointers to libraries or java classes that might give me some insight. The String.Format didn't seem QUITE right...

I would have to honor inputs too long and too short, etc. Substrings would be overlaid in the order they appear (in case of overlap).

As an example of input, I might have something like:

String:start:end

FO:0:3 (string shorter than field)

BAR:4:5 (String larger than field)

BLEH:5:9 (String overlays previous field)

I'd want to end up with

FO  BBLEH  
01234567890

(Edit: To all - StringBuilder (and specifically, the "pre-allocate to a known length, then use .replace()" theme) seems to be what I'm thinking of. Thanks to all who suggested it!)

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

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

发布评论

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

评论(4

流云如水 2024-09-05 07:23:29
StringBuilder output = new StringBuilder();
// for each input element
{
    while (output.length() < start)
    {
        output.append(' ');
    }
    output.replace(start, end, string);
}

您还可以在插入任何字符串之前确定输出的最终大小。您可以第一次遍历输入元素以找到最大的一端。这将是输出的最终大小。

char[] spaces = new char[size];
Arrays.fill(spaces, ' ');
output.append(spaces);
StringBuilder output = new StringBuilder();
// for each input element
{
    while (output.length() < start)
    {
        output.append(' ');
    }
    output.replace(start, end, string);
}

You could also establish the final size of output before inserting any string into it. You could make a first pass through the input elements to find the largest end. This will be the final size of output.

char[] spaces = new char[size];
Arrays.fill(spaces, ' ');
output.append(spaces);
难以启齿的温柔 2024-09-05 07:23:29

StringBuilder 可以吗?

StringBuilder sb = new StringBuilder();
sb.setLength(20);
sb.replace(0, 3, "FO");
sb.replace(4, 5, "BAR");
sb.replace(5, 9, "BLEH");
System.out.println("[" + sb.toString().replace('\0', ' ') + "]");
// prints "[FO  BBLEH            ]"

Will StringBuilder do?

StringBuilder sb = new StringBuilder();
sb.setLength(20);
sb.replace(0, 3, "FO");
sb.replace(4, 5, "BAR");
sb.replace(5, 9, "BLEH");
System.out.println("[" + sb.toString().replace('\0', ' ') + "]");
// prints "[FO  BBLEH            ]"
万劫不复 2024-09-05 07:23:29

如果我正确理解您的要求,您应该能够使用标准 java.lang.StringBuilder 来完成此操作:

public class StringAssembler
{
    private final StringBuilder builder = new StringBuilder();

    public void addPiece(String input, int start, int end)
    {
        final String actualInput = input.substring(0, end-start+1);
        builder.insert(start, actualInput);
    }

    public String getFullString()
    {
        return builder.toString();
    }
}

特别是,我不认为 end 参数是严格必要,因为它所能做的就是更改输入字符串的长度,因此我的 addPiece 方法中有两个步骤。

请注意,这尚未经过测试,并且在边缘情况下可能不会做正确的事情,但它应该为您提供一些起点。

If I understand your requirements correctly, you should be able to do this with the standard java.lang.StringBuilder:

public class StringAssembler
{
    private final StringBuilder builder = new StringBuilder();

    public void addPiece(String input, int start, int end)
    {
        final String actualInput = input.substring(0, end-start+1);
        builder.insert(start, actualInput);
    }

    public String getFullString()
    {
        return builder.toString();
    }
}

In particular, I don't think that the end parameter is strictly necessary, in that all it can do is change the length of the input string, hence the two steps in my addPiece method.

Note that this is not tested, and probably doesn't do the right thing in edge cases, but it should give you something to start from.

简美 2024-09-05 07:23:29

您可以使用 StringUtils.rightPad(str, size) 添加必要数量的空格。您可以使用以下命令删除不需要的字符:

if (str.length() > size) {
   str = str.substring(size);
}

You can use StringUtils.rightPad(str, size) to add the necessary number of spaces. And you can use the following to strip the unneeded characters:

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