第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何每n個(gè)字符剪切一個(gè)字符串?但只有在有空格時(shí)才會(huì)削減

如何每n個(gè)字符剪切一個(gè)字符串?但只有在有空格時(shí)才會(huì)削減

寶慕林4294392 2022-06-04 09:35:31
我試圖將一個(gè)長(zhǎng)字符串切割成該字符串的行,行的長(zhǎng)度由函數(shù)決定。該功能不會(huì)剪切中間的單詞。我嘗試了很多方法來(lái)使用子字符串等,但我不擅長(zhǎng)字符串操作。我在網(wǎng)上發(fā)現(xiàn)了一個(gè)類似的問(wèn)題,但它是在 JavaScript 中,并且一些我無(wú)法完全翻譯成 Java 的代碼(可能是因?yàn)槲覍?duì)它沒(méi)有經(jīng)驗(yàn)......)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;}例如,我有一個(gè)字符串“你好世界蘋(píng)果橙葡萄汁意大利面醬牛奶”,我想每 34 個(gè)字符切割一次(考慮到上述所有要求)所以我打電話descriptionFormatter(string, 34);想要的結(jié)果是一個(gè)字符串?dāng)?shù)組/列表:你好世界蘋(píng)果橙葡萄汁意大利面醬牛奶實(shí)際結(jié)果:你好世界蘋(píng)果橙葡萄汁我已經(jīng)幾乎讓它工作了,但有時(shí)它會(huì)在最后跳過(guò)剩余的單詞并在第一個(gè)單詞之前放置空格。我如何使它按我的意圖運(yùn)行?
查看完整描述

3 回答

?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

您可以嘗試string使用兩個(gè)索引遍歷輸入beginIndex,endIndex并在執(zhí)行過(guò)程substrings中從輸入string中獲?。?/p>


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

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


    // trim the input string to avoid empty strings at the end

    str = str.trim();


    int beginIndex = 0;  

    int endIndex = amt; 


    final int length = str.length();

    while(endIndex < length) {

        // if we landed on something other than space

        // increase the end index for the substring

        // until we hit a space

        while(endIndex < length && str.charAt(endIndex) != ' ') {

            ++endIndex;

        }

        result.add(str.substring(beginIndex, endIndex).trim());


        beginIndex = endIndex;

        endIndex += amt;

    }

    // Add the last string if any left

    if(beginIndex < length) {

        result.add(str.substring(beginIndex).trim());

    }

    return result;

}


public static void main(String[] args) {

    String str = "hello world apple orange grapes juice spagehtti sauce milk";

    descriptionFormatter(str, 34).forEach(System.out::println);

}

輸出:


hello world apple orange grapes juice

spagehtti sauce milk


查看完整回答
反對(duì) 回復(fù) 2022-06-04
?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

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

        StringBuilder strOfMaxLen = new StringBuilder();

        StringBuilder strExceedsMaxLen = new StringBuilder();

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


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


            String piece = splitted[i];

            int pieceLen = piece.length();


            if (strOfMaxLen.length()+pieceLen < amt) {

                if (strOfMaxLen.length() != 0) {

                    strOfMaxLen.append(" ");

                }

                strOfMaxLen.append(piece);

            } else {

                if (strExceedsMaxLen.length() != 0) {

                    strExceedsMaxLen.append(" ");

                }

                strExceedsMaxLen.append(piece);

            }

        }

        stringPieces.add(strOfMaxLen.toString());

        stringPieces.add(strExceedsMaxLen.toString());


        return stringPieces;

    }


查看完整回答
反對(duì) 回復(fù) 2022-06-04
?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

嘗試這樣做



static List<String> split(String s, int size) { 

  return split(s.toCharArray(), size);

}


static List<String> split(char[] s, int size) {

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

  StringBuilder sb = new StringBuilder();


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

    if (i % size == 0) {

      if(s[i] == ' ') {

        strs.add(sb.toString());

        sb = new StringBuilder();

      }else {

        StringBuilder newStringBuilder = new StringBuilder();

        int length = sb.length();

        while (length > 0 && sb.charAt(length - 1) != ' ') {

          newStringBuilder.insert(0, sb.charAt(length -  1));

          sb.deleteCharAt(length - 1);

          --length;

        }

        if(sb.length() > 0) strs.add(sb.toString());

        sb = newStringBuilder;

      }

    }

    sb.append(s[i]);

  }

  if (sb.length() > 0) {

    strs.add(sb.toString());

  }

  return strs;

}


查看完整回答
反對(duì) 回復(fù) 2022-06-04
  • 3 回答
  • 0 關(guān)注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)