-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmystring.go
More file actions
203 lines (166 loc) · 5.8 KB
/
mystring.go
File metadata and controls
203 lines (166 loc) · 5.8 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package gocharge
import (
"fmt"
"strings"
"unicode"
)
// MyString is a type for csharp like string
type MyString string
// ToString converts the MyString to a standard Go string.
// It returns the underlying string value of the MyString type.
func (s *MyString) ToString() string {
return string(*s)
}
// IsNullOrEmpty returns a bool to make sure if it is null or empty
func (s *MyString) IsNullOrEmpty(trimSpace ...bool) bool {
_s := s.ToString()
return IsStringNull(&_s, trimSpace...)
}
// IsNullOrWhiteSpace returns a bool to make sure if it is null or whitespace
func (s *MyString) IsNullOrWhiteSpace() bool {
return s.IsNullOrEmpty(true)
}
func IsStringNull(s *string, trimSpace ...bool) bool {
if s == nil {
return true
}
if len(*s) == 0 {
return true
}
if len(trimSpace) > 1 {
panic("only 1 param should be set")
}
if len(trimSpace) == 1 {
if trimSpace[0] {
if len(strings.TrimSpace(*s)) == 0 {
return true
}
}
}
return false
}
func IsStringNullOrWhiteSpace(s *string) bool {
return IsStringNull(s, true)
}
// Contains determines whether a substring occurs within this string
func (s *MyString) Contains(value string) bool {
return strings.Contains(s.ToString(), value)
}
// StartsWith determines whether the beginning of this string matches the specified string
func (s *MyString) StartsWith(value string) bool {
return strings.HasPrefix(s.ToString(), value)
}
// EndsWith determines whether the end of this string matches the specified string
func (s *MyString) EndsWith(value string) bool {
return strings.HasSuffix(s.ToString(), value)
}
// ToLower returns a copy of this string converted to lowercase
func (s *MyString) ToLower() MyString {
return MyString(strings.ToLower(s.ToString()))
}
// ToUpper returns a copy of this string converted to uppercase
func (s *MyString) ToUpper() MyString {
return MyString(strings.ToUpper(s.ToString()))
}
// Trim removes all leading and trailing white-space characters from the current string
func (s *MyString) Trim(cutset ...string) MyString {
if len(cutset) == 0 {
return MyString(strings.TrimSpace(s.ToString()))
}
return MyString(strings.Trim(s.ToString(), cutset[0]))
}
// TrimStart removes all leading white-space characters from the current string
func (s *MyString) TrimStart(cutset ...string) MyString {
if len(cutset) == 0 {
return MyString(strings.TrimLeftFunc(s.ToString(), unicode.IsSpace))
}
return MyString(strings.TrimLeft(s.ToString(), cutset[0]))
}
// TrimEnd removes all trailing white-space characters from the current string
func (s *MyString) TrimEnd(cutset ...string) MyString {
if len(cutset) == 0 {
return MyString(strings.TrimRightFunc(s.ToString(), unicode.IsSpace))
}
return MyString(strings.TrimRight(s.ToString(), cutset[0]))
}
// Replace replaces all occurrences of a specified string in the current instance with another specified string
func (s *MyString) Replace(oldValue, newValue string) MyString {
return MyString(strings.ReplaceAll(s.ToString(), oldValue, newValue))
}
// Split splits a string into substrings based on the separator
func (s *MyString) Split(separator string) []string {
return strings.Split(s.ToString(), separator)
}
// Substring retrieves a substring from this instance
func (s *MyString) Substring(start int, length ...int) MyString {
str := s.ToString()
if len(length) == 0 {
if start < 0 || start > len(str) {
panic("Index out of range")
}
return MyString(str[start:])
}
end := start + length[0]
if start < 0 || end > len(str) || length[0] < 0 {
panic("Index out of range")
}
return MyString(str[start:end])
}
// IndexOf returns the zero-based index of the first occurrence of the specified string in this instance
func (s *MyString) IndexOf(value string) int {
return strings.Index(s.ToString(), value)
}
// LastIndexOf returns the zero-based index of the last occurrence of the specified string in this instance
func (s *MyString) LastIndexOf(value string) int {
return strings.LastIndex(s.ToString(), value)
}
// PadLeft returns a new string that right-aligns the characters in this instance by padding them on the left with a specified character
func (s *MyString) PadLeft(totalWidth int, paddingChar ...rune) MyString {
str := s.ToString()
if len(str) >= totalWidth {
return *s
}
padding := ' '
if len(paddingChar) > 0 {
padding = paddingChar[0]
}
pad := strings.Repeat(string(padding), totalWidth-len(str))
return MyString(pad + str)
}
// PadRight returns a new string that left-aligns the characters in this string by padding them on the right with a specified character
func (s *MyString) PadRight(totalWidth int, paddingChar ...rune) MyString {
str := s.ToString()
if len(str) >= totalWidth {
return *s
}
padding := ' '
if len(paddingChar) > 0 {
padding = paddingChar[0]
}
pad := strings.Repeat(string(padding), totalWidth-len(str))
return MyString(str + pad)
}
// Length returns the length of the string
func (s *MyString) Length() int {
return len(s.ToString())
}
// Join concatenates the elements of a string array, using the specified separator between each element
func Join(separator string, values []string) MyString {
return MyString(strings.Join(values, separator))
}
// Format formats a string using the specified format string and arguments
func Format(format string, args ...interface{}) MyString {
return MyString(fmt.Sprintf(format, args...))
}
// CompareTo compares this instance with another string and returns an integer that indicates their relative position in the sort order
func (s *MyString) CompareTo(value string) int {
return strings.Compare(s.ToString(), value)
}
// Equals determines whether two string instances are equal
func (s *MyString) Equals(value string) bool {
return s.ToString() == value
}
// Clone creates a new MyString instance with the same value as this instance
func (s *MyString) Clone() MyString {
return MyString(s.ToString())
}