Skip to content

Commit

Permalink
Day 43 - Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiobotaro committed Feb 13, 2017
1 parent 71f9b86 commit 215cd0d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ Personal challenge to post every day a note about go.
| 040 | [Task Queue](day40/)
| 041 | [Type Assertion](day41/)
| 042 | [Defer](day42/)
Today 12/02/2017 i will not post code, because I'm travelling.Tomorrow I post 2days :beer:
| 043 | [Strings](day43/)
36 changes: 36 additions & 0 deletions day43/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"strings"
)

func main() {
// Fun with strings functions
// I choose 5 functions that looks funny

// Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning an array of substrings of s or an empty list if s contains only white space.
for _, word := range strings.Fields("a \n string\t with spaces") {
fmt.Printf("word = %+q\n", word)
}

// IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
// first vowel index
i := strings.IndexAny("some random content", "aeiou")
fmt.Printf("i = %+v\n", i)

// Join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.
phrase := strings.Join([]string{"an", "array", "of", "words"}, " ")
fmt.Printf("phrase = %+v\n", phrase)

// Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
newString := strings.Map(func(original rune) rune {
return original + 300
}, "random string")
fmt.Printf("newString = %+v\n", newString)

// TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
fmt.Printf("%q\n", strings.TrimSpace(" \t\t text with trash \n\t \r \n"))

// for more, read the docs: https://golang.org/pkg/strings
}

0 comments on commit 215cd0d

Please sign in to comment.