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

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

替換除最后一次出現以外的所有字符

替換除最后一次出現以外的所有字符

Go
夢里花落0921 2023-02-06 18:57:02
我正在對這樣的字符串執行字符替換:result = strings.ReplaceAll(result, ".", "_")這按預期工作,但我想保留最后一次出現的.而不是替換它,只是讓它獨自一人。有沒有一種奇特的方法可以做到這一點?
查看完整描述

4 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

分成[]string, 然后Join除最后 ;)


func ReplaceAllButLast(data, old, new string) string {

    split := strings.Split(data, old)

    if len(split) < 3 {

        return data

    }

    last := len(split) - 1

    return strings.Join(split[:last], new) + old + split[last]

}

https://go.dev/play/p/j8JJP-p_Abk


func main() {

    println(ReplaceAllButLast("a", ".", "_"))

    println(ReplaceAllButLast("a.b", ".", "_"))

    println(ReplaceAllButLast("a.b.c", ".", "_"))

    println(ReplaceAllButLast("a.b.c.d", ".", "_"))

}

生產的


a

a.b

a_b.c

a_b_c.d

更新


那是個玩笑,只是為了花哨


最好的方法是計算匹配次數并替換Count()-1,第二個ReplaceAll直到最后一個匹配位置并使用Join最慢


func ReplaceAllButLast_Count(data, old, new string) string {

    cnt := strings.Count(data, old)

    if cnt < 2 {

        return data

    }

    return strings.Replace(data, old, new, cnt-1)

}


func ReplaceAllButLast_Replace(data, old, new string) string {

    idx := strings.LastIndex(data, old)

    if idx <= 0 {

        return data

    }


    return strings.ReplaceAll(data[:idx], old, new) + data[idx:]

}


func ReplaceAllButLast_Join(data, old, new string) string {

    split := strings.Split(data, old)

    if len(split) < 3 {

        return data

    }

    last := len(split) - 1

    return strings.Join(split[:last], new) + old + split[last]

}

基準使用a.b.c.d -> a_b_c.d


goos: windows

goarch: amd64

pkg: example.org

cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz

Benchmark_Count-8       16375098            70.05 ns/op        8 B/op          1 allocs/op

Benchmark_Replace-8     11213830           108.5 ns/op        16 B/op          2 allocs/op

Benchmark_Slice-8        5460445           217.6 ns/op        80 B/op          3 allocs/op



查看完整回答
反對 回復 2023-02-06
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

處理這個問題最明顯的方法是結合Replaceand Count:


func ReplaceAllExceptLast(d string, o string, n string) string {

    strings.Replace(d, o, n, strings.Count(d, o) - 1)

}

但是,我認為這不是最佳解決方案。對我來說,最好的選擇是這樣做:


func ReplaceAllExceptLast(d string, o string, n string) string {

    ln := strings.LastIndex(d, o)

    if ln == -1 {

        return d

    }


    return strings.ReplaceAll(d[:ln], o, n) + d[ln:]

}

這是通過獲取要替換的值的最后一次出現的索引,然后對字符串進行全部替換直到該點。例如:


println(ReplaceAllExceptLast("a", ".", "_"))

println(ReplaceAllExceptLast("a.b", ".", "_"))

println(ReplaceAllExceptLast("a.b.c", ".", "_"))

println(ReplaceAllExceptLast("a.b.c.d", ".", "_"))

將產生:


a

a.b

a_b.c

a_b_c.d


查看完整回答
反對 回復 2023-02-06
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

我想到的第一個解決方案是Positive Lookahead正則表達式

[.](?=.*[.])

但是, Golang re2(?=re)不支持


我們可以用Non-capturing group這種方式來實現

  • 首先使用 (?:[.])( Match a single character present in the list below [.]) 來查找所有點索引。

  • 然后用'_'代替最后一個點。

樣本

func replaceStringByIndex(str string, replacement string, index int) string {

    return str[:index] + replacement + str[index+1:]

}


func replaceDotIgnoreLast(str string, replacement string) string {

    pattern, err := regexp.Compile("(?:[.])")

    if err != nil {

        return ""

    }


    submatches := pattern.FindAllStringSubmatchIndex(str, -1)

    for _, submatch := range submatches[:len(submatches)-1] {

        str = replaceStringByIndex(str, replacement, submatch[0])

    }


    return str

}


func main() {

    str := "1.2"

    fmt.Println(replaceDotIgnoreLast(str, "_"))

    str = "1.2.3"

    fmt.Println(replaceDotIgnoreLast(str, "_"))

    str = "1.2.3.4"

    fmt.Println(replaceDotIgnoreLast(str, "_"))

}

結果


1.2

1_2.3

1_2_3.4


查看完整回答
反對 回復 2023-02-06
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

  1. 使用strings.Count來計算要替換的字符串出現的次數

  2. 將 count-1 傳遞給strings.Replace以省略最后一次出現

func ReplaceAllButLast(input string, find string, replace string) string {
    occurrencesCount := strings.Count(input, find)
        return strings.Replace(input, find, replace, occurrencesCount-1)
}



查看完整回答
反對 回復 2023-02-06
  • 4 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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