2014-09-05 13 views
7

Qualcuno sa come dividere una stringa in Golang per lunghezza?Dividere la stringa per lunghezza in Golang

Ad esempio per dividere "helloworld" dopo ogni 3 caratteri, quindi dovrebbe idealmente restituire un array di "hel" "low" "orl" "d"?

alternativa una possibile soluzione potrebbe essere quella di aggiungere anche un ritorno a capo dopo ogni 3 caratteri ..

Tutte le idee sono molto apprezzate!

+3

Bene, alcuni programmi potrebbero aiutare qui? Come 's [n: n + 3] +" \ n "'? – Volker

risposta

17

Assicurati di convert tua string in una fetta di runa: vedere "Slice string into letters"

a := []rune(s) 
for i, r := range a { 
    fmt.Printf("i%d r %c\n", i, r) 
    // every 3 i, do something 
} 

a[n:n+3] funzionerà meglio con un essere una fetta di rune.

L'indice aumenterà di uno ogni runa, mentre potrebbe aumentare di più di uno per ogni byte in un slice of string: "世界": i sarebbe 0 e 3: un carattere (runa) può essere formato da più byte.


Ad esempio, considerare s := "世a界世bcd界efg世": 12 rune. (Vedi play.golang.org)

Se si tenta di analizzarlo byte per byte, se ne perderanno (in una frazione di ingenua ogni implementazione 3 caratteri) alcuni dei "indice di modulo 3" (è uguale a 2, 5, 8 e 11), perché l'indice aumenta oltre quei valori:

for i, r := range s { 
    res = res + string(r) 
    fmt.Printf("i %d r %c\n", i, r) 
    if i > 0 && (i+1)%3 == 0 { 
     fmt.Printf("=>(%d) '%v'\n", i, res) 
     res = "" 
    } 
} 

l'output:

i 0 r 世 
i 3 r a <== miss i==2 
i 4 r 界 
i 7 r 世 <== miss i==5 
i 10 r b <== miss i==8 
i 11 r c ===============> would print '世a界世bc', not exactly '3 chars'! 
i 12 r d 
i 13 r 界 
i 16 r e <== miss i==14 
i 17 r f ===============> would print 'd界ef' 
i 18 r g 
i 19 r 世 <== miss the rest of the string 

Ma se si dovesse iterare sulle rune (a := []rune(s)), si dovrebbe ottenere ciò che ci si aspetta, come il indice avrebbe inc Rease una runa alla volta, rendendo più semplice per aggregare esattamente 3 caratteri:

for i, r := range a { 
    res = res + string(r) 
    fmt.Printf("i%d r %c\n", i, r) 
    if i > 0 && (i+1)%3 == 0 { 
     fmt.Printf("=>(%d) '%v'\n", i, res) 
     res = "" 
    } 
} 

uscita:

i 0 r 世 
i 1 r a 
i 2 r 界 ===============> would print '世a界' 
i 3 r 世 
i 4 r b 
i 5 r c ===============> would print '世bc' 
i 6 r d 
i 7 r 界 
i 8 r e ===============> would print 'd界e' 
i 9 r f 
i10 r g 
i11 r 世 ===============> would print 'fg世' 
2

necessaria anche una funzione per fare questo di recente, si veda example usage here

func SplitSubN(s string, n int) []string { 
    sub := "" 
    subs := []string{} 

    runes := bytes.Runes([]byte(s)) 
    l := len(runes) 
    for i, r := range runes { 
     sub = sub + string(r) 
     if (i + 1) % n == 0 { 
      subs = append(subs, sub) 
      sub = "" 
     } else if (i + 1) == l { 
      subs = append(subs, sub) 
     } 
    } 

    return subs 
} 
1

Ecco un altro esempio (puoi provarlo con il numero here):

package main 

import (
    "fmt" 
    "strings" 
) 

func ChunkString(s string, chunkSize int) []string { 
    var chunks []string 
    runes := []rune(s) 

    if len(runes) == 0 { 
     return []string{s} 
    } 

    for i := 0; i < len(runes); i += chunkSize { 
     nn := i + chunkSize 
     if nn > len(runes) { 
      nn = len(runes) 
     } 
     chunks = append(chunks, string(runes[i:nn])) 
    } 
    return chunks 
} 

func main() { 
    fmt.Println(ChunkString("helloworld", 3)) 
    fmt.Println(strings.Join(ChunkString("helloworld", 3), "\n")) 
} 
-1

Una soluzione semplice utilizzando espressioni regolari

re: = regexp.MustCompile ((\S{3})) x: = re.FindAllStringSubmatch ("HelloWorld", -1) fmt.Println (x)

https://play.golang.org/p/mfmaQlSRkHe

+0

non capisco –

+0

Didi lo esegui in The Go Playground?Potresti farmi sapere cosa non hai capito? – rahul