如何每n个字符剪切一个字符串?但只有在有空格时才会削减

我试图将一个长字符串切割成该字符串的行,行的长度由函数决定。该功能不会剪切中间的单词。


我尝试了很多方法来使用子字符串等,但我不擅长字符串操作。我在网上发现了一个类似的问题,但它是在 JavaScript 中,并且一些我无法完全翻译成 Java 的代码(可能是因为我对它没有经验......)


public static List<String> descriptionFormatter(String string, int amt)

{

    String[] splitted = string.split(" ");

    String part = "";

    List<String> finalDesc = new ArrayList<String>();

    for(int i = 0 ; i < splitted.length; i++)

    {

        part = part + " " + splitted[i];

        if(part.length() >= amt)

        {

            finalDesc.add(part);

            part = "";

        }


    }

    return finalDesc;

}

例如,我有一个字符串“你好世界苹果橙葡萄汁意大利面酱牛奶”,我想每 34 个字符切割一次(考虑到上述所有要求)所以我打电话


descriptionFormatter(string, 34);

想要的结果是一个字符串数组/列表:

  1. 你好世界苹果橙葡萄汁

  2. 意大利面酱牛奶

实际结果:

  1. 你好世界苹果橙葡萄汁

我已经几乎让它工作了,但有时它会在最后跳过剩余的单词并在第一个单词之前放置空格。我如何使它按我的意图运行?


宝慕林4294392
浏览 121回答 3
3回答

慕田峪7331174

您可以尝试string使用两个索引遍历输入beginIndex,endIndex并在执行过程substrings中从输入string中获取:public static List<String> descriptionFormatter(String str, int amt) {&nbsp; &nbsp; List<String> result = new ArrayList<>();&nbsp; &nbsp; // trim the input string to avoid empty strings at the end&nbsp; &nbsp; str = str.trim();&nbsp; &nbsp; int beginIndex = 0;&nbsp;&nbsp;&nbsp; &nbsp; int endIndex = amt;&nbsp;&nbsp; &nbsp; final int length = str.length();&nbsp; &nbsp; while(endIndex < length) {&nbsp; &nbsp; &nbsp; &nbsp; // if we landed on something other than space&nbsp; &nbsp; &nbsp; &nbsp; // increase the end index for the substring&nbsp; &nbsp; &nbsp; &nbsp; // until we hit a space&nbsp; &nbsp; &nbsp; &nbsp; while(endIndex < length && str.charAt(endIndex) != ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++endIndex;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; result.add(str.substring(beginIndex, endIndex).trim());&nbsp; &nbsp; &nbsp; &nbsp; beginIndex = endIndex;&nbsp; &nbsp; &nbsp; &nbsp; endIndex += amt;&nbsp; &nbsp; }&nbsp; &nbsp; // Add the last string if any left&nbsp; &nbsp; if(beginIndex < length) {&nbsp; &nbsp; &nbsp; &nbsp; result.add(str.substring(beginIndex).trim());&nbsp; &nbsp; }&nbsp; &nbsp; return result;}public static void main(String[] args) {&nbsp; &nbsp; String str = "hello world apple orange grapes juice spagehtti sauce milk";&nbsp; &nbsp; descriptionFormatter(str, 34).forEach(System.out::println);}输出:hello world apple orange grapes juicespagehtti sauce milk

慕哥9229398

&nbsp; &nbsp; public static List<String> descriptionFormatter(String string, int amt) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> stringPieces = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder strOfMaxLen = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder strExceedsMaxLen = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; String[] splitted = string.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0 ; i < splitted.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String piece = splitted[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pieceLen = piece.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strOfMaxLen.length()+pieceLen < amt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strOfMaxLen.length() != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strOfMaxLen.append(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strOfMaxLen.append(piece);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strExceedsMaxLen.length() != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strExceedsMaxLen.append(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strExceedsMaxLen.append(piece);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; stringPieces.add(strOfMaxLen.toString());&nbsp; &nbsp; &nbsp; &nbsp; stringPieces.add(strExceedsMaxLen.toString());&nbsp; &nbsp; &nbsp; &nbsp; return stringPieces;&nbsp; &nbsp; }

不负相思意

尝试这样做static List<String> split(String s, int size) {&nbsp;&nbsp; return split(s.toCharArray(), size);}static List<String> split(char[] s, int size) {&nbsp; List<String> strs = new ArrayList<>();&nbsp; StringBuilder sb = new StringBuilder();&nbsp; for (int i = 0; i < s.length; ++i) {&nbsp; &nbsp; if (i % size == 0) {&nbsp; &nbsp; &nbsp; if(s[i] == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; strs.add(sb.toString());&nbsp; &nbsp; &nbsp; &nbsp; sb = new StringBuilder();&nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder newStringBuilder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; int length = sb.length();&nbsp; &nbsp; &nbsp; &nbsp; while (length > 0 && sb.charAt(length - 1) != ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newStringBuilder.insert(0, sb.charAt(length -&nbsp; 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.deleteCharAt(length - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --length;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(sb.length() > 0) strs.add(sb.toString());&nbsp; &nbsp; &nbsp; &nbsp; sb = newStringBuilder;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; sb.append(s[i]);&nbsp; }&nbsp; if (sb.length() > 0) {&nbsp; &nbsp; strs.add(sb.toString());&nbsp; }&nbsp; return strs;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java