将一个字符串分解为String [],使每个元素是最大的100个字符和空格结束 [英] Split a String to a String[] so that each element is maximum 100 characters and ends in a space

查看:183
本文介绍了将一个字符串分解为String [],使每个元素是最大的100个字符和空格结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做到这一点?我想这样的事情,但我似乎并没有能够进一步得到任何

 公共无效说话(字符串文本)
{
    的String [] = textArray text.split();
    的System.out.println(文本);    StringBuilder的建设者=新的StringBuilder();    INT索引= 0;        而(builder.length()< 99)
        {
            builder.append(textArray [指数] +);
            指数++;
        }    的System.out.println(builder.toString());
}

编辑:
 我需要每一个字符串,字符串单独是只有100个字符长。所以我需要在阵列中的每个字符串或例如一个Arralist。


解决方案

 的ArrayList<串GT;行=新的ArrayList<>();
INT索引= 0;
而(指数< textArray.length()){
    StringBuilder的建设者=新的StringBuilder();
    如果(textArray [指数]。长度()> = 99){
        build.append(textArray [指数]);
        指数++;
    }其他{
        而(builder.length()< 99 - (textArray [指数]。长度())){
            builder.append(textArray [指数] +);
            指数++;
            如果((指数<!textArray.length()){
                打破;
            }
    }
    lines.add(builder.toString());
}

,而循环会做什么它已经在做您的code,建立一个单一的线,长不到100字,每个字后面加上一个空格

然后,当我们会去超过100个字符,追加新的行字符的字符串,然后继续。


作为一个说明,此位code的:

 如果(textArray [指数]。长度()> = 99){
    build.append(textArray [指数]);
    指数++;
}

将处理字,长度超过99个字符(无空格)的(应待罕见的)情况。由于写的,它会就行保持这个词完全在一起,并可能导致行超过100个字符。如果你想不同的行为,再看看此位code的。

但有超过99个字符的话也许应该是pretty罕见,这可能是一个完整的非问题。

How do i do this? I tried something like this but I do not seem to be able to get any further.

public void speak(String text)
{
    String[] textArray = text.split(" ");
    System.out.println(text);

    StringBuilder builder = new StringBuilder();

    int index = 0;

        while(builder.length() < 99)
        {
            builder.append(textArray[index] + " ");
            index++;
        } 

    System.out.println(builder.toString());
}

EDIT: I need every String to be seperate String that is only a 100 chars long. So i will need each String in an Array or an Arralist for example.

解决方案

ArrayList<String> lines = new ArrayList<>();
int index = 0;
while(index < textArray.length()) {
    StringBuilder builder = new StringBuilder();
    if(textArray[index].length() >= 99) {
        build.append(textArray[index]);
        index++;
    } else {
        while(builder.length() < 99 - (textArray[index].length())) {
            builder.append(textArray[index] + " ");
            index++;
            if(!(index < textArray.length()) {
                break;
            }
    }
    lines.add(builder.toString());
}

The inner while loop will do what it's already doing in your code, build a single line, less than 100 characters long, adding a space after each word.

Then, when we would go over 100 characters, append a new line character to the string, and continue on.


As a note, this bit of code:

if(textArray[index].length() >= 99) {
    build.append(textArray[index]);
    index++;
}

will handle the (should-be-rare) case of a word which is longer than 99 characters (no spaces). As written, it will keep this word completely together on the line, and may result in a line longer than 100 characters. If you want different behavior, then look to this bit of code.

But words with more than 99 characters should probably be pretty rare and this may be a complete non-issue.

这篇关于将一个字符串分解为String [],使每个元素是最大的100个字符和空格结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆