Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
623e03dd
Commit
623e03dd
authored
3 years ago
by
Cornel Rautenbach
Browse files
Options
Downloads
Patches
Plain Diff
Added string utils
parent
2f3951cb
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
string_utils/string_utils.go
+85
-0
85 additions, 0 deletions
string_utils/string_utils.go
with
86 additions
and
0 deletions
.gitignore
+
1
−
0
View file @
623e03dd
...
@@ -13,3 +13,4 @@
...
@@ -13,3 +13,4 @@
# Dependency directories (remove the comment below to include it)
# Dependency directories (remove the comment below to include it)
# vendor/
# vendor/
.idea
This diff is collapsed.
Click to expand it.
string_utils/string_utils.go
0 → 100644
+
85
−
0
View file @
623e03dd
package
string_utils
import
(
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"regexp"
"strconv"
"strings"
"unicode"
)
// ReplaceNonSpacingMarks removes diacritics e.g. êžů becomes ezu
func
ReplaceNonSpacingMarks
(
str
string
)
string
{
t
:=
transform
.
Chain
(
norm
.
NFD
,
runes
.
Remove
(
runes
.
In
(
unicode
.
Mn
)),
norm
.
NFC
)
// Mn: non-spacing marks
result
,
_
,
_
:=
transform
.
String
(
t
,
str
)
return
result
}
func
RemoveAllWhiteSpaces
(
s
string
)
string
{
return
strings
.
ReplaceAll
(
strings
.
ReplaceAll
(
s
,
" "
,
""
),
"
\t
"
,
""
)
}
func
ReplaceCaseInsensitive
(
string
,
toReplace
,
replaceWith
string
)
string
{
regex
:=
regexp
.
MustCompile
(
"(?i)"
+
strings
.
ToLower
(
toReplace
))
return
regex
.
ReplaceAllString
(
string
,
replaceWith
)
}
// StringTrimQuotes - trims quotes from a string (ie: "foo" will return foo)
func
StringTrimQuotes
(
stringToTrim
string
)
string
{
if
len
(
stringToTrim
)
>=
2
{
if
stringToTrim
[
0
]
==
'"'
&&
stringToTrim
[
len
(
stringToTrim
)
-
1
]
==
'"'
{
return
stringToTrim
[
1
:
len
(
stringToTrim
)
-
1
]
}
}
return
stringToTrim
}
// IsNumericString returns true if the string can be converted to a float without error
func
IsNumericString
(
s
string
)
bool
{
_
,
err
:=
strconv
.
ParseFloat
(
s
,
64
)
return
err
==
nil
}
// Standardise phone numbers with +27 instead of 0 prefix
func
StandardisePhoneNumber
(
number
string
)
string
{
// is the first rune/char of the string a 0
if
[]
rune
(
number
)[
0
]
==
[]
rune
(
"0"
)[
0
]
{
// Add south african country code (hardcoded for now)
number
=
"+27"
+
number
[
1
:
len
(
number
)]
}
return
number
}
func
FormatPhoneNumber
(
phoneNumber
string
)
string
{
if
len
(
phoneNumber
)
>
7
{
return
phoneNumber
}
// Format as 076 453 2188
phoneNumber
=
InsertInto
(
phoneNumber
,
3
,
" "
)
phoneNumber
=
InsertInto
(
phoneNumber
,
7
,
" "
)
return
phoneNumber
}
func
InsertInto
(
s
string
,
index
int
,
characters
string
)
string
{
return
s
[
:
index
]
+
characters
+
s
[
index
:
]
}
func
IsAlphaNumeric
(
str
string
)
bool
{
regex
:=
regexp
.
MustCompile
(
"^[a-zA-Z0-9]*$"
)
return
regex
.
MatchString
(
str
)
}
func
Equal
(
a
string
,
b
string
)
bool
{
return
strings
.
TrimSpace
(
strings
.
ToLower
(
a
))
==
strings
.
TrimSpace
(
strings
.
ToLower
(
b
))
}
func
UnwrapString
(
s
*
string
)
string
{
if
s
==
nil
{
return
""
}
return
*
s
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment