pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/strutil/example_test.go (about) 1 package strutil 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 func ExampleQ() { 17 var defaultValue = "john" 18 var user = "" 19 20 fmt.Println(Q(user, defaultValue)) 21 22 // Output: 23 // john 24 } 25 26 func ExampleConcat() { 27 fmt.Println(Concat("abc", " ", "123", " ", "ABC")) 28 29 // Output: 30 // abc 123 ABC 31 } 32 33 func ExampleEllipsis() { 34 fmt.Println(Ellipsis("This is too long message to show", 18)) 35 36 // Output: 37 // This is too lon... 38 } 39 40 func ExampleSubstr() { 41 fmt.Println(Substr("Пример 例子 例 მაგალითად", 7, 2)) 42 43 // Output: 44 // 例子 45 } 46 47 func ExampleSubstring() { 48 fmt.Println(Substring("Пример 例子 例 მაგალითად", 7, 9)) 49 50 // Output: 51 // 例子 52 } 53 54 func ExampleExtract() { 55 fmt.Println(Extract("This is funny message", 8, 13)) 56 57 // Output: 58 // funny 59 } 60 61 func ExampleExclude() { 62 fmt.Println(Exclude("This is funny message", " funny")) 63 64 // Output: 65 // This is message 66 } 67 68 func ExampleLen() { 69 fmt.Println(Len("Пример 例子 例 მაგალითად")) 70 71 // Output: 72 // 21 73 } 74 75 func ExampleHead() { 76 fmt.Println(Head("This is funny message", 7)) 77 78 // Output: 79 // This is 80 } 81 82 func ExampleTail() { 83 fmt.Println(Tail("This is funny message", 7)) 84 85 // Output: 86 // message 87 } 88 89 func ExamplePrefixSize() { 90 fmt.Println(PrefixSize("#### Header 4", '#')) 91 92 // Output: 93 // 4 94 } 95 96 func ExampleSuffixSize() { 97 fmt.Println(SuffixSize("Message ", ' ')) 98 99 // Output: 100 // 4 101 } 102 103 func ExampleReplaceAll() { 104 fmt.Println(ReplaceAll("Message", "es", "?")) 105 106 // Output: 107 // M???ag? 108 } 109 110 func ExampleFields() { 111 fmt.Printf("%#v\n", Fields("Bob Alice, 'Mary Key', \"John Dow\"")) 112 113 // Output: 114 // []string{"Bob", "Alice", "Mary Key", "John Dow"} 115 } 116 117 func ExampleReadField() { 118 fmt.Println(ReadField("Bob Alice\tJohn Mary", 2, true, " ", "\t")) 119 fmt.Println(ReadField("Bob:::Mary:John:", 3, false, ":")) 120 121 // Output: 122 // John 123 // Mary 124 } 125 126 func ExampleCopy() { 127 fmt.Println(Copy("abc")) 128 129 // Output: 130 // abc 131 } 132 133 func ExampleBefore() { 134 fmt.Println(Before("john@domain.com", "@")) 135 136 // Output: 137 // john 138 } 139 140 func ExampleAfter() { 141 fmt.Println(After("john@domain.com", "@")) 142 143 // Output: 144 // domain.com 145 } 146 147 func ExampleHasPrefixAny() { 148 fmt.Println(HasPrefixAny("www.domain.com", "dl", "www")) 149 fmt.Println(HasPrefixAny("api.domain.com", "dl", "www")) 150 151 // Output: 152 // true 153 // false 154 } 155 156 func ExampleHasSuffixAny() { 157 fmt.Println(HasSuffixAny("www.domain.com", ".com", ".org", ".net")) 158 fmt.Println(HasSuffixAny("www.domain.info", ".com", ".org", ".net")) 159 160 // Output: 161 // true 162 // false 163 }