-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
String.go
63 lines (49 loc) · 1.43 KB
/
String.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"strings"
)
func main() {
// Creating a string
str := "Some String"
sentence := "I'm a long sentence made up of many different words"
numbers := []string{"one", "two", "three", "four", "five"}
path := "/home/gabriel/project/"
// Convert string to lowercase
lower := strings.ToLower(str)
fmt.Println(lower)
// Convert string to uppercase
upper := strings.ToUpper(str)
fmt.Println(upper)
// Check if string contains another string
if strings.Contains(str, "some") {
fmt.Println("Yes, the string exists!")
}
// Get/Print character range from string
fmt.Println("Chars 3-10: " + str[3:10])
fmt.Println("First Five: " + str[:5])
// Split a string
words := strings.Split(sentence, " ")
fmt.Printf("%v \n", words)
// Split a string on whitespaces using fields
fields := strings.Fields(sentence)
fmt.Printf("%v \n", fields)
// Join an Array of string
joinStr := strings.Join(numbers, ",")
fmt.Println(joinStr)
// Replace (Takes a number of how many replacements it should do -1 = all of them)
newstr := strings.Replace(str, "Some", "The", -1)
fmt.Println(newstr)
// HasPrefix
isAbsolute := strings.HasPrefix(path, "/")
fmt.Println(isAbsolute)
// Has Suffix
hasTrailingSlash := strings.HasSuffix(path, "/")
fmt.Println(hasTrailingSlash)
// Count characters in string
count := strings.Count(str, "s")
fmt.Println(count)
// Dertermine string length
length := len(str)
fmt.Println(length)
}