Freitag, 27. November 2009

strings package in golang


package main

import (
"strings";
"fmt";
)

func main() {
data := "bla blu blub";
fmt.Println(data);
// return: bla blu blub

// convert string in bytes
b := strings.Bytes(data);
fmt.Println(b); // print every byte as array
// return: [98 108 97 32 98 108 117 32 98 108 117 98]


// test if the string begin with the prefix
if strings.HasPrefix(data, "bl") {
fmt.Println("data: \"", data, "\" begin with the prefix \"bl\"");
}

// test if the string end with the suffix
if strings.HasSuffix(data, "blub") {
fmt.Println("data: \"", data, "\" end with \"blub\"");
}

// Index returns the index of the first instance of sep in s,
// or -1 if sep is not present in s.
fmt.Println("index: ", strings.Index(data, "blu"));
// return: index: 4

fmt.Println("index: ", strings.Index(data, "b"));
// return: index: 0
fmt.Println("last index: ", strings.LastIndex(data, "b"));
// return: last index: 11
// LastIndex return the last occurency of seeking string

// with Repeat you can repeat ;)
fmt.Println("r: ", strings.Repeat("x", 10));
// return: r: xxxxxxxxxx

// split the string in array of string
as := strings.Split("root:alsdfjl:230:/blub/blub:lalalala", ":", 0);

fmt.Println(as);
// return: [root alsdfjl 230 /blub/blub lalalala]

fmt.Println(strings.Join(as, "|"));
// return: root|alsdfjl|230|/blub/blub|lalalala

fmt.Println(strings.ToLower("UASFalsdfuADSFWE$$"));
// return: uasfalsdfuadsfwe$$

fmt.Println(strings.ToUpper("owenadsfoadsfj"));
// return: OWENADSFOADSFJ

fmt.Println("<", strings.TrimSpace(" asdf sdowe "), ">");
// return: < asdf sdowe >
}

Keine Kommentare:

Kommentar veröffentlichen