gpt4 book ai didi

java - 如何根据指定长度格式化带空格的字符串?

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:34 24 4
gpt4 key购买 nike

尝试在 Java 中创建一个方法,根据长度拉伸(stretch)缓冲区的内容(通过放置适当数量的空格)来格式化字符串。因此,根据给定的特定长度,字符串的第一个字符位于第一个索引中,最后一个字符位于实际的最后一个索引本身。

public static String format(String sentence, int length) {
if (sentence.length() >= length) {
return sentence;
}
StringBuilder sb = new StringBuilder();

String[] words = sentence.split("\\s+");

int usedCharacters = 0;

for (String word : words) {
usedCharacters += word.length();
}

int emptyCharacters = length - usedCharacters;
int spaces = emptyCharacters / words.length - 1;

for (String word : words) {
sb.append(word);
for (int i = 0; i <= spaces; i++) {
sb.append(" ");
}
}
return sb.toString();
}

对于这个单元测试,这是可行的:

@Test
public void isCorrectLength() {
String value = StringUtils.format("brown clown", 20);
assert(value.length() == 20);
}

所以,在这里,最大缓冲区大小是:20

使用的字符总数是:10

未使用的字符总数为:10

最终结果(如果您打印字符串)是:

brown     clown

clown 中的“n”在索引 20 处...

但是,以下测试存在边缘情况(导致它中断):

@Test
public void isCorrectLengthWithLongerSentence() {
String value = StringUtils.format("Love programming Java using Eclipse!", 50);
assert(value.length() == 50);
}

缓冲区大小:50

总使用字符数:25未使用字符总数:25

空格:3

最终长度:48

最终结果(如果您打印字符串)是:

Love     programming    Java    using    Eclipse!

为什么最终索引是 48 而不是 50?

感叹号“!”在“Eclipse”之后,应该是 50 而不是 48...

我怀疑是因为我的空间计算被关闭了。

感谢您花时间阅读本文。

最佳答案

对于这个测试

@Test
public void isCorrectLength() {
String value = StringUtils.format("Went to the slope and snowboarded for hours., 103);
assert(value.length() == 103);
}

发生这种情况是因为您正在划分:

int spaces = emptyCharacters / words.length - 1;

这导致 (66/8) - 1) = 7.25,然后你有一个 for 循环,它不考虑额外的 .25 这意味着你不会填充所需的缓冲区长度。

此外,由于您将其声明为 int,因此您不会获得额外的 0.25,因此您应将其更改为 double,并将其他值也转换为 double。

然后您可以计算单词数并检查额外的 0.25 乘以计数器是否达到 1,您添加一个空格并重置计数器。

    double spaces = (double)emptyCharacters / (double)words.length - 1.0;

double extraSpace = spaces % 1;
double counter = 0;
for (String word : words) {
counter++;

sb.append(word);
for (int i = 0; i <= spaces; i++) {
sb.append(" ");
}

if ((counter * extraSpace) >= 1) {
sb.append(" "); // This is the extra space.
counter = 0;
}
}

是这样的。问题在于并非所有单词都可以具有相同数量的空格。为了适应静态缓冲区长度,有些会更多,有些会更少。这也是一个特例,因为余数是0.25,会产生正好2个空格,余数的余数还需要容纳。 (万一它没有达到 1 而你还有一个词呢。)

下面的代码弥补了这一点。

    double spaces = (double)emptyCharacters / (double)words.length - 1.0;

double extraSpace = spaces % 1;
double counter = 0;
int wordIndex = 0;
for (String word : words) {
counter++;
wordIndex++;

sb.append(word);
for (int i = 0; i <= spaces; i++) {
sb.append(" ");
}

if ((counter * extraSpace) >= 1) {
sb.append(" "); // This is the extra space.
counter = 0;
}

if ((wordIndex == words.length - 1) && (counter * extraSpace) > 0) {
sb.append(" "); // This accounts for remainder.
}
}

这在任何方面都算不上优雅,但它适用于之前的测试,例如,对于这个新测试:

@Test
public void isCorrectLength() {
String value = StringUtils.format("We went to the giant slope and snowboarded for hours., 103);
assert(value.length() == 103);
}

关于java - 如何根据指定长度格式化带空格的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com