Convert slice of string to a readable string eg: ["one", "two", "three"] -> "one, two and three"
(str []string)
| 92 | // Convert slice of string to a readable string |
| 93 | // eg: ["one", "two", "three"] -> "one, two and three" |
| 94 | func SliceToReadableString(str []string) string { |
| 95 | if len(str) == 0 { |
| 96 | return "" |
| 97 | } |
| 98 | if len(str) == 1 { |
| 99 | return str[0] |
| 100 | } |
| 101 | if len(str) == 2 { |
| 102 | return fmt.Sprintf("%s and %s", str[0], str[1]) |
| 103 | } |
| 104 | readableStr := "" |
| 105 | if len(str) > 2 { |
| 106 | return fmt.Sprintf("%s%s", |
| 107 | strings.Join(str[:len(str)-1], ", "), |
| 108 | fmt.Sprintf(" and %s", str[len(str)-1])) |
| 109 | } |
| 110 | |
| 111 | return readableStr |
| 112 | } |
no outgoing calls