Ricerca…
introduzione
Una stringa è in effetti una porzione di byte di sola lettura. In Go a string letteral conterrà sempre una rappresentazione UTF-8 valida del suo contenuto.
Sintassi
- variableName: = "Hello World" // dichiara una stringa
- variableName: = `Hello World` // dichiara una stringa letterale non elaborata
- variableName: = "Hello" + "World" // concatena le stringhe
- sottostringa: = "Hello World" [0: 4] // ottiene una parte della stringa
- letter: = "Hello World" [6] // ottiene un carattere della stringa
- fmt.Sprintf ("% s", "Hello World") // formatta una stringa
Tipo di stringa
Il tipo di string consente di memorizzare il testo, che è una serie di caratteri. Esistono diversi modi per creare stringhe. Una stringa letterale viene creata scrivendo il testo tra virgolette.
text := "Hello World"
Poiché le stringhe Go supportano UTF-8, l'esempio precedente è perfettamente valido. Le stringhe contengono byte arbitrari che non significano necessariamente che ogni stringa conterrà UTF-8 valido, ma i valori letterali stringa avranno sempre sequenze UTF-8 valide.
Il valore zero delle stringhe è una stringa vuota "" .
Le stringhe possono essere concatenate usando l'operatore + .
text := "Hello " + "World"
Le stringhe possono anche essere definite usando i backtick `` . Questo crea un letterale stringa grezzo che significa che i caratteri non saranno sfuggiti.
text1 := "Hello\nWorld"
text2 := `Hello
World`
Nell'esempio precedente, text1 sfugge al carattere \n che rappresenta una nuova riga mentre text2 contiene direttamente il carattere della nuova riga. Se confronti text1 == text2 il risultato sarà true .
Tuttavia, text2 := `Hello\nWorld` non sfuggirebbe al carattere \n , il che significa che la stringa contiene il testo Hello\nWorld senza una nuova riga. Sarebbe l'equivalente di digitare text1 := "Hello\\nWorld" .
Formattazione del testo
fmt funzioni di fmt implementa per stampare e formattare il testo usando i verbi di formato. I verbi sono rappresentati con un segno di percentuale.
Verbi generali:
%v // the value in a default format
// when printing structs, the plus flag (%+v) adds field names
%#v // a Go-syntax representation of the value
%T // a Go-syntax representation of the type of the value
%% // a literal percent sign; consumes no value
booleano:
%t // the word true or false
Numero intero:
%b // base 2
%c // the character represented by the corresponding Unicode code point
%d // base 10
%o // base 8
%q // a single-quoted character literal safely escaped with Go syntax.
%x // base 16, with lower-case letters for a-f
%X // base 16, with upper-case letters for A-F
%U // Unicode format: U+1234; same as "U+%04X"
Costituenti a virgola mobile e complessi:
%b // decimalless scientific notation with exponent a power of two,
// in the manner of strconv.FormatFloat with the 'b' format,
// e.g. -123456p-78
%e // scientific notation, e.g. -1.234456e+78
%E // scientific notation, e.g. -1.234456E+78
%f // decimal point but no exponent, e.g. 123.456
%F // synonym for %f
%g // %e for large exponents, %f otherwise
%G // %E for large exponents, %F otherwise
Stringa e fetta di byte (trattate in modo equivalente con questi verbi):
%s // the uninterpreted bytes of the string or slice
%q // a double-quoted string safely escaped with Go syntax
%x // base 16, lower-case, two characters per byte
%X // base 16, upper-case, two characters per byte
Pointer:
%p // base 16 notation, with leading 0x
Utilizzando i verbi, puoi creare stringhe concatenando più tipi:
text1 := fmt.Sprintf("Hello %s", "World")
text2 := fmt.Sprintf("%d + %d = %d", 2, 3, 5)
text3 := fmt.Sprintf("%s, %s (Age: %d)", "Obama", "Barack", 55)
La funzione Sprintf formatta la stringa nel primo parametro sostituendo i verbi con il valore dei valori nei parametri successivi e restituisce il risultato. Come Sprintf , anche la funzione Printf formatta, ma invece di restituire il risultato, stampa la stringa.
pacchetto di stringhe
-
fmt.Println(strings.Contains("foobar", "foo")) // true fmt.Println(strings.Contains("foobar", "baz")) // false -
fmt.Println(strings.HasPrefix("foobar", "foo")) // true fmt.Println(strings.HasPrefix("foobar", "baz")) // false -
fmt.Println(strings.HasSuffix("foobar", "bar")) // true fmt.Println(strings.HasSuffix("foobar", "baz")) // false -
ss := []string{"foo", "bar", "bar"} fmt.Println(strings.Join(ss, ", ")) // foo, bar, baz -
fmt.Println(strings.Replace("foobar", "bar", "baz", 1)) // foobaz -
s := "foo, bar, bar" fmt.Println(strings.Split(s, ", ")) // [foo bar baz] -
fmt.Println(strings.ToLower("FOOBAR")) // foobar -
fmt.Println(strings.ToUpper("foobar")) // FOOBAR -
fmt.Println(strings.TrimSpace(" foobar ")) // foobar
Altro: https://golang.org/pkg/strings/ .