亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在golang中橢圓截斷文本?

如何在golang中橢圓截斷文本?

Go
30秒到達戰場 2022-05-10 16:00:21
我希望能夠干凈地剪切大于一定數量字符的段落,而不會在中間剪切一個單詞。所以例如這個:一個早已確立的事實是,讀者在查看頁面布局時會被頁面的可讀內容分散注意力。使用 Lorem Ipsum 的重點在于它具有或多或少的正態分布字母,而不是使用“這里的內容,這里的內容”,使它看起來像可讀的英語。應該變成:讀者會被可讀的內容分散注意力,這是一個早已確立的事實......這是我想出的功能: func truncateText(s string, max int) string {    if len(s) > max {        r := 0        for i := range s {            r++            if r > max {                return s[:i]            }        }    }    return s}但它只是粗暴地削減了文本。我想知道如何修改(或用更好的解決方案替換它)以橢圓地剪切文本?
查看完整描述

3 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

切片字符串可能會出現問題,因為切片適用于字節,而不是符文。然而,范圍適用于符文:


lastSpaceIx:=-1

len:=0

for i,r:=range str {

  if unicode.IsSpace(r) {

     lastSpaceIx=i

  }

  len++

  if len>=max {

    if lastSpaceIx!=-1 {

        return str[:lastSpaceIx]+"..."

    }

    // If here, string is longer than max, but has no spaces

  }

}

// If here, string is shorter than max


查看完整回答
反對 回復 2022-05-10
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

范圍是完全沒有必要的;就像現在一樣,您的整個功能可能只是:


func truncateText(s string, max int) string {

    return s[:max]

}

這太簡單了,它甚至不應該是一個函數;但當然它也會切斷你說你不想要的單詞。因此,您可以:


func truncateText(s string, max int) string {

    if max > len(s) {

        return s

    }

    return s[:strings.LastIndex(s[:max]," ")]

}

或者,如果您想使用多個字符作為單詞邊界而不僅僅是空格:


func truncateText(s string, max int) string {

    if max > len(s) {

        return s

    }

    return s[:strings.LastIndexAny(s[:max]," .,:;-")]

}


查看完整回答
反對 回復 2022-05-10
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

要根據空格等進行拆分,可以使用 regex :


func splitString(str string) []string {

    re := regexp.MustCompile("[\\s\\n\\t\\r ]+") //split according to \s, \t, \r, \t and whitespace. Edit this regex for other 'conditions'


    split := re.Split(str, -1)

    return split

}


func main() {

    var s = "It is a long\nestablished fact that a reader\nwill\nbe distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."

    var maxLen = 40


    arr := splitString(s)


    totalLen := 0

    finalStr := ``

    for _, each := range arr {

        if (totalLen + len(each) > maxLen) {

            fmt.Print(strings.TrimSpace(finalStr) + `...`)

            break

        }

        totalLen += len(each)

        finalStr += each + ` `


    }

}

//老2


您可以執行以下操作:將字符串拆分為切片并循環遍歷切片,直到字符串的總長度高于最大允許長度。


    var s = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."

    var maxLen = 30


    arr := strings.SplitAfter(s, ` `)


    totalLen := 0

    finalStr := ``

    for _, each := range arr {

        if (totalLen + len(each) > maxLen) {

            fmt.Print(strings.TrimSpace(finalStr) + `...`)

            break

        }

        totalLen += len(each)

        finalStr += each


    }

這是一個早已確立的事實……


//舊的錯誤答案

您必須使用字符串和切片:


    var s = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."


    newS := s[:30 - 3] 

    newS += `...`

    fmt.Print(newS)

結果 :It is a long established fa...


查看完整回答
反對 回復 2022-05-10
  • 3 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號