Next One

mail@pastecode.io avatar
unknown
java
2 years ago
1.1 kB
1
Indexable
Never
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThousandCharLimit {

    /*  I am given 4000 characters, I want to split it into 1000 characters or less blocks.
        They need to be full sentences so I'll split them by the period (.)
     */

    public static void main(String[] args) {
        String originalString = "He traveled the land in search of worthy foes. I see you like to chew. Maybe you should chew on my fist! The warrior said nothing, for his mouth was full. Then he swallowed. And then he spoke. Enough talk. Let's fight!";

        String[] splitArray = originalString.split("\\. ");

        String temp_string = "";
        List<String> finalList = new ArrayList<>();

        for (String s : splitArray) {
            if (temp_string.length() > 100) {
                finalList.add(temp_string);
                temp_string = "";
            }
            temp_string += s;
        }

        finalList.add(temp_string);

        System.out.println(Arrays.toString(finalList.toArray()));

    }

}